Conditions in which Kafka Consumer (Group) triggers a rebalance
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. One key feature of Kafka is its ability to allow consumers to read data in consumer groups, where each consumer within a group reads from exclusive partitions of each topic, thus enabling high parallelism. A vital aspect of managing consumer groups is handling rebalances effectively. A rebalance is initiated in various scenarios due to the inherent distributed nature of Kafka. Below we explore the conditions that trigger a rebalance in a Kafka consumer group, providing technical explanations and examples where relevant.
Kafka Consumer Rebalance: Introduction
In Kafka, rebalancing involves redistributing the partitions across the consumers in a consumer group. This is crucial when consumers are either added to or removed from the group, or when subscriptions change. The rebalance process ensures that the load is fairly distributed among the consumer instances and each partition is consumed by only one consumer at a time within a consumer group.
Conditions Triggering Rebalance
Here are some key situations where Kafka triggers a rebalance:
- New Consumer Joins a Group: When a new consumer joins an existing consumer group, Kafka rebalances the group to assign partitions to the new member.
- Existing Consumer Leaves the Group: If a consumer fails or leaves a group (voluntarily or due to failure), the partitions previously assigned to it are redistributed among remaining group members.
- Topic Partitions Change: If partitions are added to a topic being consumed by the group, or if the topic is deleted, a rebalance is needed to redistribute or revoke the partitions.
- Consumer Group Subscription Change: If a consumer's subscription pattern changes (e.g., subscribing or unsubscribing to more topics), the group will rebalance to update the consumers' assignments according to their new subscriptions.
- Offset Reset: If a consumer fails to read an offset or the offset is out of range, this can also trigger a rebalance.
- Broker Changes: Changes on the broker-side such as broker failure or a new broker coming online might also necessitate a rebalance.
Example Scenarios
- New Consumer Joins: Suppose consumer group 'A' has 2 consumers (Consumer 1 and Consumer 2) and 4 partitions (P1, P2, P3, P4). If a new consumer (Consumer 3) joins, Kafka may rebalance to assign one or two partitions to the new consumer for load balancing.
- Consumer Leaves: If Consumer 2 in the previous example disconnects, partitions P3 and P4 would need to be reassigned to Consumer 1 and Consumer 3.
Impact of Rebalances
Rebalances are necessary for optimal performance and fault tolerance, but they are not cost-free. They can lead to temporary microservices unavailable or read duplicates depending on the message offset reset policies in place.
Handling Rebalance Correctly
To handle rebalances effectively:
- Ensure all consumer processes handle rebalance listeners properly, committing offsets if necessary before partition reassignment.
- Configure suitable session timeout and rebalance timeout settings that match your use case's performance characteristics.
Summary Table
| Condition | Description | Example Impact |
| New Consumer Joins | Partitions need redistributing to include new consumer | Even load distribution; potential short read downtime |
| Existing Consumer Leaves | Partitions of the exiting consumer need reassignment | Redistribution of loads; can cause increased load on remaining |
| Topic Partitions Change | Redistribution required for new / removed partitions | Adjustment to higher or lower capacity; potential short delay |
| Consumer Group Subscription Change | Consumers change their topic subscription | Potential shift in topic focus; redistribution required |
| Offset Reset | Failures in reading(offset out of range) | Immediate rebalance to maintain consistent state |
| Broker Changes | Changes such as new broker or broker failure | May prompt rebalances for affected partitions |
Understanding and managing Kafka's rebalance process is fundamental to leveraging the full capabilities and efficiency of Kafka consumer groups. By configuring wisely and handling rebalance events with foresight, systems can minimize disruption and maintain continuous, scalable processing.

