Quick revision
The short answer
Spring finds every @KafkaListener method with a bean post-processor that @EnableKafka registers (Spring Boot does this for us) and turns each one into an endpoint. A ConcurrentKafkaListenerContainerFactory, which Boot creates as kafkaListenerContainerFactory from the spring.kafka.* properties, builds a ConcurrentMessageListenerContainer for each endpoint. That container starts one KafkaMessageListenerContainer per unit of concurrency, each with its own KafkaConsumer and thread because KafkaConsumer is not thread-safe; each child polls, calls our method and commits offsets according to the AckMode. All containers are kept in the KafkaListenerEndpointRegistry, where we can find them by id to stop, start, pause or resume.
What to remember
In this answer, we followed a @KafkaListener from annotation to running consumers. The bean post-processor that @EnableKafka registers, and Spring Boot applies for us, turns each method into an endpoint; the ConcurrentKafkaListenerContainerFactory built from spring.kafka.* properties creates a ConcurrentMessageListenerContainer for it; that container starts one KafkaMessageListenerContainer per concurrency level, each with its own KafkaConsumer and thread because the consumer is not thread-safe; each child polls, invokes our method and commits by AckMode; and the KafkaListenerEndpointRegistry lets us find containers by id to stop, start, pause or resume them.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring finds every @KafkaListener method with a bean post-processor that @EnableKafka registers (Spring Boot does this for us) and turns each one into an endpoint. A ConcurrentKafkaListenerContainerFactory, which Boot creates as kafkaListenerContainerFactory from the spring.kafka.* properties, builds a ConcurrentMessageListenerContainer for each endpoint. That container starts one KafkaMessageListenerContainer per unit of concurrency, each with its own KafkaConsumer and thread because KafkaConsumer is not thread-safe; each child polls, calls our method and commits offsets according to the AckMode. All containers are kept in the KafkaListenerEndpointRegistry, where we can find them by id to stop, start, pause or resume.
Why this question is asked
A @KafkaListener method looks like a simple callback, but there is a whole runtime behind it: consumers, threads, polling loops and offset commits. Knowing that machinery is what lets us answer practical questions such as why concurrency above the partition count does nothing, why our listener runs on a thread named orderListener-0-C-1, or how to pause a listener during an outage. Interviewers ask it to separate people who use the annotation from people who understand it. In this answer, we will follow the path from annotation to running consumer: the post-processor, the factory, the concurrent container and its children, the poll loop with commits, and the registry.
1From Annotation to Endpoint
When the application context starts, Spring needs to discover listener methods. @EnableKafka registers a bean post-processor, KafkaListenerAnnotationBeanPostProcessor, a component that inspects every bean after it is created. Spring Boot's Kafka auto-configuration applies @EnableKafka for us, which is why a Boot application never declares it.
For each method annotated with @KafkaListener, the post-processor builds an endpoint, an object (a MethodKafkaListenerEndpoint) that describes the listener: the bean and method to invoke, the topics or patterns, the group id, the listener id, the concurrency and any other attributes from the annotation. It hands the endpoint to a registrar, which will ask a container factory to build a container for it.
- @KafkaListener methodour code
- KafkaListenerAnnotationBeanPostProcessorfinds the method, builds an endpoint
- ConcurrentKafkaListenerContainerFactoryturns the endpoint into a container
- ConcurrentMessageListenerContainerone per endpoint, held in the registry
- KafkaMessageListenerContainer x None per concurrency level, own thread
- KafkaConsumer x None per child, polls its assigned partitions
6 layers, from the top down. Step through them one at a time.
2The Container Factory Turns Endpoints Into Containers
A listener container factory knows how to build containers with a common configuration. By default the endpoint uses the bean named kafkaListenerContainerFactory, and Spring Boot auto-configures one, a ConcurrentKafkaListenerContainerFactory, from the spring.kafka.consumer.* and spring.kafka.listener.* properties: the consumer factory with bootstrap servers and deserializers, the AckMode, concurrency, and any CommonErrorHandler, message converter or transaction manager beans it finds.
For each endpoint, the factory creates a ConcurrentMessageListenerContainer and applies its settings, with the annotation's own attributes, such as concurrency or groupId, overriding the factory defaults. We can define more factories, for example one for batch listeners, and choose one with the containerFactory attribute.
3ConcurrentMessageListenerContainer and Its Children
The ConcurrentMessageListenerContainer does not consume anything itself. When it starts, it creates N child KafkaMessageListenerContainers, where N is the concurrency, and starts each one. Each child creates its own KafkaConsumer and runs its poll loop on its own thread.
The reason for this design is that KafkaConsumer is not thread-safe: only one thread may use a consumer instance at a time. So the only way to consume in parallel is several consumers, each with a single thread. All of them join the same consumer group, and Kafka's group protocol spreads the topic's partitions across them. With 6 partitions and concurrency 3, each child gets 2 partitions. With concurrency 8 and 6 partitions, two children get no partitions and sit idle, so concurrency above the partition count is wasted.
Notice the thread names: orderListener-0, -1 and -2 are the children, each with its own consumer. Records from one partition always arrive on the same child's thread, which is how per-partition order is preserved even with concurrency.
4Inside Each Child: Poll, Invoke, Commit
Each KafkaMessageListenerContainer runs a loop. It calls consumer.poll(), which returns up to max.poll.records records (500 by default) from its assigned partitions. For a record listener, it invokes our method once per record, through an adapter that converts the record into the method's parameters, such as the payload, headers or the whole ConsumerRecord. If the method throws, the container hands the failure to the error handler, DefaultErrorHandler by default.
Then it commits offsets according to the AckMode. Spring Kafka disables the client's auto-commit by default and commits itself. With the default BATCH, it commits once all records from the poll have been processed. RECORD commits after each record, TIME and COUNT commit periodically, and MANUAL or MANUAL_IMMEDIATE leave it to our code through an Acknowledgment parameter. Then the loop polls again.
7 messages in order. Play them, or step through them one at a time.
5The Registry: Finding and Controlling Containers
Containers built for @KafkaListener endpoints are not ordinary beans in the context. They are registered in the KafkaListenerEndpointRegistry, keyed by the listener id, which is why giving each listener an explicit id is good practice. The registry starts the containers when the context starts, unless autoStartup is false, and stops them on shutdown.
We can inject the registry and control a container at runtime. This is how we pause a listener while a downstream system is down, stop one before resetting its offsets, or start a listener that was created with autoStartup false after a warm-up.
Notice that pause is different from stop. A paused container keeps polling, so its consumers stay in the group and keep their partitions, but the poll returns no records. A stopped container closes its consumers and leaves the group, which triggers a rebalance.
6Follow-up questions
What changes when you set batch = "true" on the listener?
The child container passes the whole result of each poll to our method at once, as a List of payloads or ConsumerRecords, instead of calling it per record. The batch size is up to max.poll.records. With the default BATCH AckMode, offsets are committed after the method returns for the whole list. Error handling changes too: to tell DefaultErrorHandler which record failed, we throw a BatchListenerFailedException with the record's index, so it commits the records before it and retries from the failed one; any other exception makes it retry the whole batch. Batch listeners suit bulk work such as one database insert per batch.
Where does the group id come from?
From the annotation's groupId attribute if set, otherwise from the listener id when idIsGroup is true (the default) and an id is given, otherwise from the consumer factory's group.id, typically spring.kafka.consumer.group-id. Because the id becomes the group id by default, renaming a listener id can silently start a new consumer group, so set groupId explicitly for important listeners.
Can a listener use virtual threads?
The container's threads come from a task executor that we can configure on the container properties, and recent Spring Kafka versions can run the consumer threads on virtual threads; check your version. It does not change the model: each child still has one consumer used by one thread at a time.
7Conclusion
In this answer, we followed a @KafkaListener from annotation to running consumers. The bean post-processor that @EnableKafka registers, and Spring Boot applies for us, turns each method into an endpoint; the ConcurrentKafkaListenerContainerFactory built from spring.kafka.* properties creates a ConcurrentMessageListenerContainer for it; that container starts one KafkaMessageListenerContainer per concurrency level, each with its own KafkaConsumer and thread because the consumer is not thread-safe; each child polls, invokes our method and commits by AckMode; and the KafkaListenerEndpointRegistry lets us find containers by id to stop, start, pause or resume them.
Open with
At startup, @EnableKafka, which Spring Boot applies automatically, registers KafkaListenerAnnotationBeanPostProcessor. It inspects every bean, and for each @KafkaListener method it builds an endpoint describing the bean, method, topics, group and options.
Then, if they want more
A listener container factory turns each endpoint into a container. Boot auto-configures a ConcurrentKafkaListenerContainerFactory called kafkaListenerContainerFactory from the spring.kafka properties, with the consumer factory, AckMode, concurrency, error handler and so on, and annotation attributes override those defaults. For each endpoint it creates a ConcurrentMessageListenerContainer.
That container doesn't consume itself; it starts one child KafkaMessageListenerContainer per concurrency level, and each child has its own KafkaConsumer and its own thread, because KafkaConsumer isn't thread-safe.
The children join the same group, so Kafka spreads the partitions across them, which means concurrency above the partition count leaves children idle, and each partition is always processed by one thread, preserving order.
Each child runs a loop: poll, invoke my method for each record through an adapter that converts arguments, send failures to the error handler, then commit offsets according to the AckMode, which is BATCH by default, meaning after all records of the poll.
Finally, all these containers are registered in KafkaListenerEndpointRegistry by listener id, so I can look one up to stop, start, pause or resume it, for example pausing during a downstream outage.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What happens behind a @KafkaListener method in Spring for Apache Kafka? Explain the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/97b3ebd4f2/