Spring Kafka
max.poll.records
Kafka configurations
Kafka troubleshooting
Spring framework

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.

Practice system design

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:

  1. Concurrency and Container Settings: In Spring Kafka, the ConcurrentKafkaListenerContainerFactory is typically used to configure the Kafka listeners. If the concurrency setting in this factory is set higher than 1, multiple threads (Kafka consumers) will be created. Each thread can poll up to max.poll.records, potentially leading to a situation where the aggregated number of records processed exceeds the expected number.
  2. Polling Interval Misconfigurations: The Kafka consumer’s poll() method is also dependent on the max.poll.interval.ms configuration, which controls the maximum time between poll invocations before considering the consumer dead. Misconfigurations here can lead to unexpected polling behavior.
  3. Kafka Version and Client Bugs: Different Kafka broker and client versions have had various bugs that could affect how consumer properties like max.poll.records are 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.records to 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.ms carefully to ensure that it does not contribute to unexpected polling behavior.

Summary Table

Issue DescriptionPossible CausesImpactResolution Strategy
Spring Kafka doesn't respect max.poll.recordsHigh concurrency settings; Kafka client/broker bugsIncreased load and potential memory issuesAdjust 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.