Difference between heartbeat.interval.ms and session.timeout.ms in Kafka consumer config
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 LinkedIn team and written in Scala and Java. A key component of Kafka is its ability to handle high-throughput, low-latency processing of large streams of data. Consumers in Kafka are those processes that subscribe to topics and process the feed of published messages. Two important configurations essential for robust consumer operation are heartbeat.interval.ms and session.timeout.ms. Understanding the differences and relationships between these two configurations is crucial for optimizing Kafka performance and reliability.
Understanding heartbeat.interval.ms
heartbeat.interval.ms is a configuration setting for the Kafka consumer that specifies the expected time interval between heartbeats to the consumer coordinator when using Kafka's group management facility. Heartbeats are used to ensure that the consumer's session is alive and to facilitate proper rebalancing of consumers in a group. The primary purpose of this setting is to maintain membership in a consumer group and signal the broker that the consumer is still active.
The interval helps determine if a consumer is still connected and functioning without needing to wait for the longer timeout specified by session.timeout.ms. A shorter heartbeat interval helps in quicker detection of failures and faster rebalancing.
Understanding session.timeout.ms
session.timeout.ms is another significant setting for Kafka consumers that marks the time duration after which a consumer can be considered dead if no heartbeat has been received. This setting is crucial as it directly impacts the robustness and scalability of consumer operations. If a consumer fails to send a heartbeat within the session.timeout.ms duration, it is presumed dead, and the group coordinator will initiate rebalancing of the consumer group to distribute partitions among the available consumers.
This timeout is particularly important when consumers process long-running tasks or when the system is under high load, as it prevents premature rebalancing which might occur if session.timeout.ms were too short under such circumstances.
Relationship and Impact
It is imperative to maintain a proper ratio between heartbeat.interval.ms and session.timeout.ms. Typically, heartbeat.interval.ms should be set to one-third of session.timeout.ms, which allows the consumer to send a maximum of three heartbeats within the session timeout period, ensuring timely detection of consumer failures.
Configuration Example
| Configuration | Default Value | Recommended Practice | Description |
heartbeat.interval.ms | 3000 ms | One-third of session.timeout.ms | Determines how frequently the Kafka Clients sends heartbeats to the broker. Ensures it's active. |
session.timeout.ms | 10000 ms | Based on system load and nature of tasks | Used by the broker to detect if the consumer has failed. Larger values are preferable in systems with high loads or large tasks. |
Best Practices
- Proper Balancing: Setting
heartbeat.interval.mstoo low could lead to unnecessary network traffic and quick timeout in highly-loaded systems, while setting it too high could delay rebalancing during consumer failures. - Sensible Defaults: Begin with the defaults provided by Kafka, then adjust based on the workload characteristics and consumer stability observations.
Conclusion
Understanding and correctly setting heartbeat.interval.ms and session.timeout.ms is crucial for maintaining the stability and performance of Kafka consumer groups. By keeping these values in optimal proportion, Kafka admins and application developers can ensure smooth and efficient stream processing, even under varying system loads. Adjusting these settings appropriately can lead to significantly different behaviors in consumer robustness and rebalancing characteristics, thereby directly impacting application performance.
Related reading
- Difference between idempotence and exactly-once in Kafka Stream
- Difference between Kafka and ActiveMQ
- Difference between kafka and nifi
- Difference between Kafka async and Kafka sync acks=0?
- Difference between KTable and local store
- Difference between 'metric.reporters' and 'kafka.metrics.reporters' properties in Kafka
- Difference between request.timeout.ms and timeout.ms properties of Kafka producer
- Difference between retention configuration offsets.retention.minutes and log.retention.minutes

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.