Apache Kafka is widely used for building event-driven applications, distributed data pipelines, and real-time processing systems. It allows multiple consumer instances to process messages from a topic in parallel while maintaining partition-level ordering.
As an application grows, teams often increase the number of consumers to handle higher message volumes. They may also restart instances during deployments, replace unhealthy containers, or scale services automatically.
These changes can trigger a Kafka consumer group rebalance.
Rebalancing is an important part of Kafka's distributed architecture. It ensures that partitions are assigned to active consumers and that workload is distributed across the group.
However, rebalancing can temporarily interrupt message consumption. During certain types of rebalancing, consumers may need to release their current partition assignments and wait for the group to receive new assignments.
If rebalancing happens frequently, the application may experience processing pauses, increasing consumer lag and delaying downstream operations.
This article explains how Kafka consumer rebalancing works, why it causes processing pauses, what triggers repeated rebalances, and how to reduce their impact through better consumer configuration and deployment practices.
What Is Kafka Consumer Rebalancing?
Kafka stores messages in topics, and each topic is divided into partitions.
Partitions allow Kafka to distribute data across brokers and enable multiple consumers to process messages in parallel.
For example, consider an application that processes order events.
Topic: orders
Partition 0
Partition 1
Partition 2
Partition 3
Partition 4
Partition 5A consumer group contains multiple consumer instances that work together to process these partitions.
Consider three consumers:
Consumer Group: order-processing-group
Consumer 1 ---> Partition 0, Partition 1
Consumer 2 ---> Partition 2, Partition 3
Consumer 3 ---> Partition 4, Partition 5Each partition is assigned to one consumer within the group at a time.
Now suppose Consumer 3 stops unexpectedly.
Kafka must redistribute its partitions among the remaining consumers.
The new assignment might look like this:
Consumer Group: order-processing-group
Consumer 1 ---> Partition 0, Partition 1, Partition 4
Consumer 2 ---> Partition 2, Partition 3, Partition 5This reassignment process is called consumer rebalancing.
Kafka uses consumer group coordination to manage membership and partition ownership.
The purpose is to ensure that active consumers receive the appropriate partitions without assigning the same partition to multiple consumers in the same group at the same time.
Why Does Rebalancing Cause Processing Pauses?
Rebalancing can interrupt normal consumption because Kafka must coordinate changes in partition ownership.
The impact depends on the rebalance protocol and assignment strategy being used.
Full-stop rebalancing
With the traditional eager rebalance approach, consumers participating in a rebalance release their current partition assignments before receiving new assignments.
For example:
Before Rebalance
Consumer 1 ---> P0, P1
Consumer 2 ---> P2, P3
Consumer 3 ---> P4, P5
Consumer 3 stops
|
v
Rebalance Starts
Consumer 1 ---> No partitions
Consumer 2 ---> No partitions
|
v
New Assignment
Consumer 1 ---> P0, P1, P4
Consumer 2 ---> P2, P3, P5During the coordination process, consumers may temporarily stop processing records.
This can create a processing pause even when the remaining consumers are healthy.
The duration depends on factors such as group size, coordinator communication, consumer response times, and assignment complexity.
Incremental rebalancing
Cooperative rebalancing reduces unnecessary partition movement.
Instead of requiring every consumer to release all its partitions, Kafka can move only the partitions that need reassignment.
For example:
Before Rebalance
Consumer 1 ---> P0, P1
Consumer 2 ---> P2, P3
Consumer 3 ---> P4, P5
Consumer 3 stops
|
v
Only Required Partitions Move
Consumer 1 ---> P0, P1, P4
Consumer 2 ---> P2, P3, P5Existing assignments that do not need to change can remain in place.
This reduces the disruption associated with rebalancing.
However, cooperative rebalancing does not eliminate every processing pause. Consumers still need to revoke partitions that are being transferred and coordinate the new ownership.
Common Reasons Kafka Triggers a Rebalance
Understanding why rebalancing happens is more useful than simply increasing timeout values.
Several events can cause a consumer group to rebalance.
A consumer joins the group
When a new consumer instance joins an existing group, Kafka may redistribute partitions to include the new member.
Suppose a topic has six partitions and two consumers:
Consumer 1 ---> P0, P1, P2
Consumer 2 ---> P3, P4, P5A third consumer joins:
Consumer 1 ---> P0, P1
Consumer 2 ---> P2, P3
Consumer 3 ---> P4, P5The workload can now be distributed across three consumers.
However, adding consumers does not always improve throughput. The number of useful consumers is constrained by the number of partitions.
If a topic has six partitions, adding ten consumers to the same group does not allow all ten consumers to process that topic simultaneously. Some consumers will remain without assigned partitions.
A consumer leaves the group
A consumer may leave because of:
Application shutdown.
Container termination.
Deployment replacement.
Process crashes.
Network interruptions.
Health-check failures.
When a consumer leaves, Kafka must determine which remaining members should receive its partitions.
A controlled shutdown can help consumers release assignments cleanly.
However, an unexpected termination may require the group coordinator to detect that the member is no longer active.
A consumer fails to call poll() on time
This is one of the most important causes of unexpected rebalancing.
Kafka consumers must continue calling poll() to make progress in the consumer group protocol.
The max.poll.interval.ms setting defines the maximum allowed delay between poll() calls when using consumer group management.
If the consumer does not call poll() within this interval, Kafka considers it unable to make progress and may initiate a rebalance.
For example, consider a consumer with the following configuration:
max.poll.interval.ms=300000This allows up to five minutes between poll() calls.
Now imagine that the consumer retrieves 500 records and processes them sequentially.
If processing takes six minutes, the next poll() call occurs too late.
Kafka may consider the consumer failed, even if the application process is still running.
This is a common problem when message processing takes longer than expected.
Session timeout and heartbeat failures
Kafka consumers also use heartbeats to maintain group membership.
Under the classic group protocol, the consumer sends heartbeats to the group coordinator.
If the coordinator does not receive heartbeats within the configured session timeout, it can remove the consumer from the group.
For example:
session.timeout.ms=45000
heartbeat.interval.ms=15000These settings allow a 45-second session timeout with heartbeats sent every 15 seconds.
The exact permitted values depend on broker configuration and the consumer protocol.
A consumer may miss heartbeats because of:
Long garbage collection pauses.
Network interruptions.
CPU starvation.
Application process suspension.
Unresponsive consumer threads.
If the consumer is removed from the group, its partitions become available for reassignment.
An important distinction is that heartbeats and poll() serve different purposes.
Heartbeats help maintain group membership. poll() is also responsible for advancing the consumer's interaction with the application and group coordination.
A consumer can continue sending heartbeats while application processing is delayed, but exceeding max.poll.interval.ms can still cause it to leave the group.
Topic partition changes
A change in topic partition count can also trigger reassignment.
For example, an application may initially use four partitions:
orders
P0
P1
P2
P3Later, an administrator increases the partition count to eight.
orders
P0
P1
P2
P3
P4
P5
P6
P7The consumer group may need to rebalance to account for the additional partitions.
Topic subscriptions using patterns can also trigger re-evaluation when matching topics are added.
Subscription changes
If consumers change their topic subscriptions, Kafka may need to recalculate partition ownership.
For example, one consumer may initially subscribe to:
ordersLater, the application changes its subscription to:
orders
paymentsThe group assignment may need to change based on the updated subscription.
Avoid changing subscriptions unnecessarily in a long-running consumer application.
Understanding Kafka Partition Assignment Strategies
Kafka uses partition assignment strategies to decide which consumer receives each partition.
The assignment strategy affects how partitions are distributed and how much ownership changes during rebalancing.
RangeAssignor
RangeAssignor distributes partitions in ranges for each subscribed topic.
For example, consider six partitions and three consumers:
Consumer 1 ---> P0, P1
Consumer 2 ---> P2, P3
Consumer 3 ---> P4, P5This approach is straightforward, but it can produce uneven distribution across multiple topics depending on their partition counts and subscriptions.
RoundRobinAssignor
RoundRobinAssignor distributes partitions across consumers in a round-robin manner.
For example:
Consumer 1 ---> P0, P3
Consumer 2 ---> P1, P4
Consumer 3 ---> P2, P5It can produce a more even distribution when consumers subscribe to the same set of topics.
StickyAssignor
StickyAssignor attempts to balance partition assignments while preserving as many existing assignments as possible.
This reduces unnecessary partition movement when membership changes.
It is useful for applications where frequent partition movement can increase processing disruption.
CooperativeStickyAssignor
CooperativeStickyAssignor combines sticky assignment with cooperative rebalancing.
Instead of requiring all consumers to release every partition, it allows partitions to move incrementally.
For many applications using the classic consumer group protocol, it is a useful option for reducing rebalance disruption.
Comparing assignment strategies
Strategy | Assignment behavior | Rebalance characteristics |
|---|---|---|
RangeAssignor | Assigns partitions in ranges | Can cause broader reassignment |
RoundRobinAssignor | Distributes partitions in round-robin order | Can improve distribution across shared subscriptions |
StickyAssignor | Preserves existing assignments where possible | Reduces unnecessary partition movement |
CooperativeStickyAssignor | Preserves assignments and supports incremental movement | Reduces full-stop reassignment |
For existing applications, review client and broker compatibility before changing assignment strategies.
Do not assume that changing the assignor alone will solve rebalances caused by slow processing or unstable consumer membership.
Reducing Processing Pauses With Cooperative Rebalancing
Cooperative rebalancing is one of the most effective ways to reduce unnecessary processing interruptions in consumer groups using the classic protocol.
In Java Kafka clients, you can configure CooperativeStickyAssignor:
partition.assignment.strategy=org.apache.kafka.clients.consumer.CooperativeStickyAssignorThe cooperative approach allows consumers to retain partitions that do not need to move.
When ownership must change, the affected partitions are revoked and reassigned through incremental coordination.
This reduces the number of partitions that must stop processing during a rebalance.
However, cooperative rebalancing has important compatibility requirements.
All consumers participating in a group must support the selected protocol and assignment behavior.
Join the conversation! Your thoughts help the community grow.