Kafka Streams rebalancing latency spikes on high throughput kafka-streams services
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka Streams is a client library for building applications and microservices, where the input and output data are stored in Kafka clusters. It allows for processing streams of data in real-time. With high throughput services, managing Kafka Streams effectively is crucial but challenging, especially around balancing and rebalancing operations which can cause latency spikes. This article will delve into the causes and impacts of these latency spikes during rebalance events and propose methods to mitigate them.
Understanding Rebalancing in Kafka Streams
Rebalancing is a procedure by which partitions are assigned to streams within a consumer group. This is necessary when a new task needs to be started, a new application instance is added, or when existing instances fail or depart. While necessary for maintaining fault tolerance and scalability, rebalancing can introduce latency spikes due to several reasons:
- Stopping and Starting Tasks: When a rebalancing occurs, active tasks must be stopped, and state must be either saved or transferred to other nodes. Once the rebalance finishes, tasks may resume or start on new nodes. During this period, processing is effectively paused, which increases latency.
- Statestore Replication: Kafka Streams uses state stores for storing intermediate processing data. During rebalancing, these state stores may need to be recreated or moved across instances. The restoration of state from the internal topic can be time-consuming depending on the state size.
- Consumer Group Coordination: The rebalancing process is managed by the Kafka coordinator. The process involves various steps including member joining, syncing, and stability checks which all add up to the rebalancing time. High throughput and large numbers of topics/partitions can exacerbate this.
Examples and Technical Insights
Consider a Kafka Streams application that processes 100,000 messages per second under normal operations. During a rebalancing event, such as the addition of a new Streams instance, all tasks need to pause, causing message processing to halt until the rebalance concludes.
Here’s what happens step-by-step:
- Join Group: Each Streams instance sends a request to join the group.
- Sync Group: The leader collects and redistributes state information, assigning tasks to each instance.
- Restoration: Instances restore state from internal Kafka topics for stateful operations.
Given these steps, the rebalance interval and resulting latency spike depend largely on the syncing and restoration phases, where state size and network speed play significant roles.
Mitigating Rebalance Latency
Multiple strategies can help reduce the impact of rebalancing:
- Incremental Cooperative Rebalancing: As introduced in Kafka 2.4, this approach allows tasks to migrate gradually without requiring a full stop and restart of all tasks. This significantly reduces processing pauses.
- Scaling Up Gradually: Adding more instances to a Kafka Streams application gradually can help minimize the rebalancing impact at any given time.
- Fine-tuning Configurations: Adjusting session timeout and heartbeat interval can help manage how quickly Kafka detects failed instances and triggers rebalances, thereby reducing unnecessary rebalances.
- Sticky Assignment: This technique minimizes the movement of task assignments across instances across successive rebalances.
- Optimizing State Store Sizes: Smaller state sizes synchronize faster. Techniques such as compacting topics, aggressive cleanup policies, or using in-memory state stores (where appropriate) can help reduce synchronization time.
Summary Table
| Factor | Impact on Latency | Mitigation Strategy |
| Task Stop/Start | High | Incremental Cooperative Rebalancing |
| Statestore Replication Size | High | Optimize State Store Size, Use In-memory Stores |
| Consumer Group Coordination | Medium | Fine-tune session timeout and heartbeat intervals |
| Network Speed | Medium to High | Improve infrastructure and network capabilities |
| Number of Partitions/Topics | High | Scale Gradually, Sticky Assignment |
Conclusion
Kafka Streams rebalancing is essential for achieving scalability and fault tolerance in distributed streaming applications. However, it also presents challenges in latency, particularly during high throughput scenarios. By understanding the mechanisms of rebalancing and implementing strategies to mitigate its impact, developers can maintain smooth and efficient stream processing even in dynamic, large-scale environments.
Related reading
- kafka streams session window retention duration
- Kafka streams shutting down and don't run
- Kafka Streams Sort Within Processing Time Window
- Kafka Streams (Suppress) Closing a TimeWindow by timeout
- Kafka Streams Testing java.util.NoSuchElementException Uninitialized topic output_topic_name
- Kafka Streams thread number
- Kafka Streams use case
- Kafka streams use cases for add global store

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.