What is the difference between Sticky Assignor and Cooperative Sticky Assignor?π
StickyAssignor vs CooperativeStickyAssignorπ
1. Conceptual Overviewπ
Kafka uses assignors to decide which consumer in a group will read from which partitions. When group membership changes (a consumer joins or leaves), Kafka performs a rebalance, redistributing partitions among consumers.
Two common assignors are:
| Assignor | Type | Goal | Rebalance Style |
|---|---|---|---|
StickyAssignor |
Eager | Minimize partition movement (keep assignments stable) | Eager (stop-the-world) |
CooperativeStickyAssignor |
Cooperative | Same goal, but with smoother rebalances | Incremental (cooperative) |
2. How They Workπ
a) StickyAssignorπ
- Purpose: Keeps partition assignments as stable as possible across rebalances.
-
Rebalance type: Eager.
-
All consumers in the group must stop processing.
- Every partition is revoked from all consumers.
- After that, partitions are reassigned β possibly to the same consumers as before, but only after all were stopped.
- Drawback: Causes noticeable pauses in consumption because itβs a stop-the-world rebalance.
Example: Assume three consumers: C1, C2, and C3, each consuming two partitions. If C3 leaves, Kafka revokes all partitions from all consumers, pauses processing, and then redistributes all six partitions. Even if C1 and C2 get back the same partitions, there was a full pause.
b) CooperativeStickyAssignorπ
- Purpose: Same stickiness as
StickyAssignor, but rebalances happen more smoothly. -
Rebalance type: Incremental or cooperative.
-
Only the partitions that must move are revoked.
- Other partitions stay assigned, so consumers can keep processing most of their workload.
- Benefit: Dramatically reduces pause time and improves overall availability during rebalances.
Example: With the same setup (C1, C2, C3), if C3 leaves, Kafka only reassigns the two partitions that C3 owned. C1 and C2 continue processing their existing partitions without interruption.
3. Analogyπ
Imagine three kids sharing toy cars.
StickyAssignor: When one kid leaves, the teacher says: βAll of you put your cars back in the box. Now weβll redistribute them.β Everyone must stop playing, even if they get back the same cars.
CooperativeStickyAssignor: When one kid leaves, the teacher says: βOnly take the cars from the kid who left. Everyone else can keep theirs.β The game continues almost uninterrupted.
4. Configurationπ
You choose the assignor using the partition.assignment.strategy property in your consumer configuration.
# Eager rebalancing
partition.assignment.strategy=org.apache.kafka.clients.consumer.StickyAssignor
# Incremental rebalancing (recommended)
partition.assignment.strategy=org.apache.kafka.clients.consumer.CooperativeStickyAssignor
Modern Kafka clients (version 2.4 and later) support CooperativeStickyAssignor,
and it is the preferred choice because it minimizes rebalance interruptions.
5. Summary Tableπ
| Feature | StickyAssignor | CooperativeStickyAssignor |
|---|---|---|
| Goal | Keep assignments stable | Same goal |
| Rebalance Type | Eager (stop-the-world) | Incremental (cooperative) |
| Partition Revocation | All partitions | Only those that need reassignment |
| Processing Pause | High | Minimal |
| Introduced In | Kafka 0.11 | Kafka 2.4 |
| Recommended For | Legacy setups | Most modern deployments |