Cooperative vs eager rebalancing in Kafka - Performance Comparison
When Kafka consumers rebalance, the way partitions are reassigned affects how long the process takes.
We want to understand how the time to rebalance grows as the number of consumers and partitions increases.
Analyze the time complexity of these simplified rebalance steps.
// Eager rebalance example
onRebalance() {
revokeAllPartitions(); // revoke all partitions from this consumer
assignPartitions(); // assign partitions to this consumer
}
// Cooperative rebalance example
onRebalance() {
revokePartitionsIncrementally(); // revoke partitions gradually
assignPartitionsIncrementally(); // assign partitions gradually
}
This code shows two ways Kafka rebalances partitions among consumers: all at once (eager) or step-by-step (cooperative).
Look at what repeats during rebalance.
- Primary operation: Reassigning partitions to consumers.
- How many times: For eager, all partitions are revoked and reassigned in one big step. For cooperative, partitions are revoked and assigned in smaller batches over multiple steps.
As the number of partitions and consumers grows, the work to rebalance changes.
| Input Size (partitions) | Approx. Operations (eager) | Approx. Operations (cooperative) |
|---|---|---|
| 10 | ~10 revoke + 10 assign = 20 | ~10 revoke + 10 assign in steps = 20 |
| 100 | ~100 revoke + 100 assign = 200 | ~100 revoke + 100 assign in smaller steps = 200 |
| 1000 | ~1000 revoke + 1000 assign = 2000 | ~1000 revoke + 1000 assign in smaller steps = 2000 |
Pattern observation: Both methods do similar total work, but cooperative spreads it out, avoiding big pauses.
Time Complexity: O(n)
This means the time to rebalance grows linearly with the number of partitions involved.
[X] Wrong: "Cooperative rebalancing is slower because it does more steps."
[OK] Correct: Cooperative rebalancing spreads work over time but does not increase total operations; it reduces pause time and improves smoothness.
Understanding how Kafka rebalancing scales helps you explain system behavior and design choices clearly, a valuable skill in many real-world roles.
"What if the number of consumers grows faster than partitions? How would that affect the rebalance time complexity?"
Practice
cooperative rebalancing in Kafka consumer groups?Solution
Step 1: Understand cooperative rebalancing behavior
Cooperative rebalancing allows consumers to leave partitions gradually, which reduces the pause time during rebalancing.Step 2: Compare with eager rebalancing
Eager rebalancing reassigns all partitions at once, causing longer pauses, unlike cooperative rebalancing.Final Answer:
It reduces consumer pause time by reassigning partitions gradually. -> Option AQuick Check:
Cooperative rebalancing = gradual partition reassignment [OK]
- Confusing eager with cooperative rebalancing
- Thinking cooperative rebalancing eliminates coordination
- Assuming rebalancing increases partitions
Solution
Step 1: Identify the correct property for assignment strategy
Kafka usespartition.assignment.strategyto set the rebalance strategy class.Step 2: Recognize the cooperative assignor class
The cooperative rebalancing strategy is implemented byorg.apache.kafka.clients.consumer.CooperativeStickyAssignor.Final Answer:
partition.assignment.strategy = 'org.apache.kafka.clients.consumer.CooperativeStickyAssignor' -> Option DQuick Check:
Cooperative assignor class = partition.assignment.strategy = 'org.apache.kafka.clients.consumer.CooperativeStickyAssignor' [OK]
- Using incorrect property names
- Confusing eager and cooperative strategy names
- Assuming a boolean flag enables cooperative rebalancing
Solution
Step 1: Understand eager rebalancing behavior on consumer leave
In eager rebalancing, when any consumer leaves, all partitions are reassigned immediately to maintain balance.Step 2: Recognize the impact on consumer pause
This immediate reassignment causes a pause for all consumers during the rebalance.Final Answer:
All partitions in the group are reassigned immediately, causing a pause. -> Option AQuick Check:
Eager rebalancing = full immediate reassignment [OK]
- Thinking only leaving consumer's partitions reassign
- Assuming no pause occurs
- Believing reassignment waits for timeout
Solution
Step 1: Check the assignor configuration
If long pauses occur despite expecting cooperative rebalancing, the consumers might be using the eager assignor by mistake.Step 2: Understand the impact of eager assignor
Eager assignor causes all partitions to reassign at once, leading to longer pauses.Final Answer:
Consumers are using the eager assignor instead of cooperative assignor. -> Option CQuick Check:
Long pauses + cooperative expected = eager assignor used [OK]
- Assuming gradual reassignment causes long pauses
- Ignoring assignor configuration
- Blaming rebalance timeout without checking assignor
Solution
Step 1: Identify the goal
The goal is to minimize downtime during frequent consumer group changes.Step 2: Choose the appropriate rebalance strategy
Cooperative rebalancing reduces pause time by reassigning partitions gradually, which suits frequent joins/leaves.Step 3: Select the correct configuration
UsingCooperativeStickyAssignorenables cooperative rebalancing in Kafka consumers.Final Answer:
Use cooperative rebalancing with CooperativeStickyAssignor to reduce pause time. -> Option BQuick Check:
Minimize downtime = cooperative rebalancing + correct assignor [OK]
- Choosing eager rebalancing for low downtime
- Disabling rebalancing causing stale assignments
- Misconfiguring session timeout instead of strategy
Ready to prove it?
Pass the test and this pattern moves unknown
