Spring Kafka don't respect max.poll.records with strange behavior
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is an open-source stream-processing software platform developed by the Apache Software Foundation, written in Scala and Java. It aims at providing a high-throughput, low-latency platform for handling real-time data feeds. Its integration with Spring enables developers to leverage Spring's features to build robust Kafka-based applications effectively.
Understanding max.poll.records in Spring Kafka
In Kafka, the max.poll.records configuration controls the maximum number of records returned in a single call to poll(). When using Spring Kafka, this setting is essential for managing how much data your consumer retrieves at each polling interval, which can significantly impact application performance and reliability.
Issue: Ignoring max.poll.records
Some users have reported that Spring Kafka does not always respect the max.poll.records settings. This manifests as consumer behavior where the consumer retrieves more records than specified by max.poll.records. This unexpected behavior can lead to issues such as overwhelmed consumers, increased latencies, and out-of-memory errors.
Causes and Analysis
There could be multiple reasons why max.poll.records might be seemingly ignored in a Spring Kafka application:
- Concurrency and Container Settings: In Spring Kafka, the
ConcurrentKafkaListenerContainerFactoryis typically used to configure the Kafka listeners. If theconcurrencysetting in this factory is set higher than 1, multiple threads (Kafka consumers) will be created. Each thread can poll up tomax.poll.records, potentially leading to a situation where the aggregated number of records processed exceeds the expected number. - Polling Interval Misconfigurations: The Kafka consumer’s
poll()method is also dependent on themax.poll.interval.msconfiguration, which controls the maximum time between poll invocations before considering the consumer dead. Misconfigurations here can lead to unexpected polling behavior. - Kafka Version and Client Bugs: Different Kafka broker and client versions have had various bugs that could affect how consumer properties like
max.poll.recordsare handled. It's essential to ensure compatibility and check for known issues in specific versions.
Technical Examples and Scenarios
Imagine a scenario where a Spring Boot application configured to use Spring Kafka has set max.poll.records to 100. However, the application logs indicate that sometimes 200 or more records are being processed within a short period, contrary to expectations. This could be an issue with concurrency. If the application has the ConcurrentKafkaListenerContainerFactory’s concurrency set to 2, each consumer could potentially fetch 100 records, leading to a total of 200 records.
Resolution Strategies
To address issues where Spring Kafka might not respect max.poll.records, consider the following strategies:
- Review and Adjust Concurrency Settings: Ensure that the concurrency settings align with your
max.poll.recordsto avoid fetching more records than anticipated. - Update and Patch Kafka: Keep your Kafka client and broker versions up-to-date, and apply any necessary patches that might fix known issues with consumer configurations.
- Max Poll Interval Tuning: Adjust
max.poll.interval.mscarefully to ensure that it does not contribute to unexpected polling behavior.
Summary Table
| Issue Description | Possible Causes | Impact | Resolution Strategy |
Spring Kafka doesn't respect max.poll.records | High concurrency settings; Kafka client/broker bugs | Increased load and potential memory issues | Adjust concurrency, update Kafka, tune max.poll.interval.ms |
Conclusion
The unexpected ignoring of max.poll.records in Spring Kafka can lead to significant application issues. By understanding the interaction between concurrency settings and Kafka's polling mechanism, and keeping software up-to-date, developers can mitigate these problems and optimize their Kafka consumers for reliable, efficient operation.
Related reading
- Spring Kafka error handling - v1.1.x
- Spring Kafka error This error handler cannot process 'SerializationException's directly; please consider configuring an 'ErrorHandlingDeserializer
- Spring Kafka get assigned partitions
- Spring Kafka Idempotence Producer configuration
- Spring Kafka integration test Error while writing to highwatermark file
- Spring Kafka is Acknowledgement.acknowledge thread safe?
- Spring Kafka JsonDesirialization MessageConversionException failed to resolve class name Class not found
- Spring kafka @KafkaListener is not being invoked

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.