Spring Kafka Always rebalance after 5 min even i pause consumer
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When implementing message-driven systems using Apache Kafka with the Spring Kafka library, understanding how consumers manage partitions and what influences rebalancing is crucial. A common issue faced by developers is observing that Kafka always seems to trigger a rebalance of consumers in a consumer group approximately every 5 minutes. This can happen even when consumers are paused. In this article, we will explore why this happens and how to manage or mitigate such behavior.
Understanding Consumer Rebalance
Consumer rebalancing is a process where the partitions assigned to each consumer in a consumer group are reassigned or redistributed. This can happen for several reasons:
- A new consumer joins the group.
- An existing consumer leaves the group or is considered dead.
- The set of topics or partitions changes.
- The topic subscriptions are modified.
Rebalancing ensures that all partitions are being consumed and that the load of processing is evenly distributed across the available consumers in the group.
Heartbeats and Session Timeouts
Kafka uses a mechanism called heartbeats to keep track of alive consumers within a consumer group. Consumers send heartbeats at a regular interval to inform the group coordinator (one of the Kafka brokers) that they are alive and well.
Key configurations related to this mechanism are:
session.timeout.ms: If the coordinator does not receive a heartbeat for the duration of this timeout, it considers the consumer dead and triggers a rebalance.heartbeat.interval.ms: Determines how frequently heartbeats are sent to the coordinator.
If session.timeout.ms is too low, even minor GC pauses or temporary network issues could cause frequent rebalances.
Consumer Pausing and Heartbeats
Pausing a consumer in Spring Kafka does not stop it from sending heartbeats. It only stops the consumer from polling messages from its partitions. This means that even a paused consumer should maintain its membership in the consumer group and not trigger a rebalance due to heartbeats not being sent. However, if some other events, like repeated connection losses to the broker are unnoticed, these could cause rebalances inadvertently.
Configuration Review
Setting appropriate values for session.timeout.ms and heartbeat.interval.ms according to your environment's characteristics is critical. Here's a brief overview of their impact:
| Configuration | Default Value | Description |
session.timeout.ms | 10000 ms | Maximum allowed time between heartbeats |
heartbeat.interval.ms | 3000 ms | Frequency at which heartbeats are sent |
It's generally recommended to keep the heartbeat interval at one-third of the session timeout to allow the consumer to recover from transient failures.
Debugging Unexpected Rebalances
To diagnose unexpected rebalances:
- Enable Detailed Logging: Configure logging for the consumer to report detailed information on its state and interaction with the Kafka cluster.
- Monitor Consumer Metrics: Kafka provides JMX metrics that can help monitor consumer health, like
heartbeat-rate,join-rate, andsync-rate. - Network Issues: Check for network problems between your consumers and the Kafka cluster, as these can disrupt the regular flow of heartbeats.
Conclusion
While Kafka's design tries to ensure high availability and fault tolerance, understanding the underlying mechanics of consumer heartbeat, session management, and the impact of pausing can minimize undesired rebalances. Always tune your timeout settings to suit your network and application behavior, and monitor your consumers to keep your Kafka system efficient and robust.
By maintaining this balance and knowing how to configure and monitor your system, you can reduce the frequency of unnecessary rebalances and improve the overall performance of your Kafka-based messaging system.
Related reading
- Spring Kafka and exactly once delivery guarantee
- Spring kafka and Kafka Cluster
- Spring Kafka and Kafka Streams
- Spring Kafka asynchronous send calls block
- Spring Kafka Auto Commit Offset In Case of Failures
- Spring Kafka configure number of partitions for topic
- Spring kafka consumer lag metric is always 0
- Spring Kafka Consumer Unable to Rejoin after LeaveGroup Request

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.