Spring Kafka- When is exactly Consumer.poll() called behind the hood?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Spring Kafka is a project that applies core Spring concepts to the development of Kafka-based messaging solutions. It provides a high-level abstraction for Kafka-based messaging solutions and simplifies the use of Kafka APIs for sending and receiving messages.
Kafka Consumer and Polling
In Kafka, a consumer retrieves messages from Kafka servers by calling the poll() method of the Kafka consumer API. This method fetches any available messages from the server(s) for all the topics and partitions that the consumer is subscribed to. The poll() method is pivotal in controlling how and when consumers retrieve messages.
Integration in Spring Kafka
Spring Kafka manages the lifecycle of the Kafka consumer in its listener container. This listener container handles the setup of the Kafka consumer, the receiving of messages, and the delegation of those messages to a listener (usually a method annotated with @KafkaListener). The core component responsible for these operations is the KafkaMessageListenerContainer.
In Spring Kafka, you don’t call the poll() method directly. Instead, this method is invoked internally by Spring's listener container. Here's a simplified view of how this happens:
- Initialization: Once a
KafkaListenerContaineris created and started, it establishes a connection with Kafka brokers and joins the consumer group. - Poll Loop: The
KafkaMessageListenerContainerenters a polling loop, where it continually calls thepoll()method on the Kafka consumer at a specified interval. - Message Handling: Each time
poll()is called, it returns a batch of messages (if available). Spring Kafka then dispatches these messages to the appropriate message listener.
Configuration
The behavior of the poll() method, such as how long it waits for messages, and how much data it tries to fetch, can be controlled by several consumer configuration settings:
fetch.min.bytes: This sets the minimum amount of data that the server should return for a fetch request. If not enough data is available, the request will wait until sufficient data is accumulated.fetch.max.wait.ms: This sets the maximum time the server will block before answering the fetch request if there isn't sufficient data to meet thefetch.min.bytesrequirement.max.poll.records: This limits the number of records returned in a single call to poll.
Example Configuration in Application.yml
Special Considerations
- Long Polling: If
fetch.min.bytesis high compared to the size of messages being produced to the topic, a call topoll()might block for a long duration (up tofetch.max.wait.ms), which could delay message processing. - Heartbeats: Kafka consumers need to send heartbeats to the Kafka brokers periodically to maintain membership in their consumer groups. If
poll()does not get called within a specific interval (set bysession.timeout.ms), the consumer can be considered dead by the brokers and it might be kicked out of the group.
Summary Table
| Property | Description | Default Value | Importance |
max.poll.records | Maximum number of records returned in a single call to poll. | 500 | High |
fetch.min.bytes | Minimum amount of data the server should return for a fetch request. | 1 | Medium |
fetch.max.wait.ms | Maximum time the server will block before answering the fetch request. | 500 | Medium |
auto.offset.reset | What to do when there is no initial offset in Kafka or if the current offset does not exist anymore. | latest | Medium |
enable-auto-commit | Whether the consumer's offset is periodically committed in the background. | true | High |
Utilizing Spring Kafka abstracts much of the complexity associated with direct Kafka API interaction, allowing developers to focus more on business logic rather than messaging infrastructure details. Understanding when and how Consumer.poll() is invoked can help in tuning your consumer configurations for optimal performance.
Related reading
- Spring Kafka - Consume last N messages for partitions(s) for any topic
- Spring Kafka - Event sourcing - Example of how to query some entity state using Kafka + KafkaStreams API
- Spring Kafka - How to reset offset to latest with a group id?
- spring kafka - set consumer log level manually (spring 5)
- Spring kafka 2.3.1 not supported in spring boot V2.1.9.RELEASE
- Spring Kafka Always rebalance after 5 min even i pause consumer
- Spring Kafka and exactly once delivery guarantee
- Spring kafka and Kafka Cluster

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.