2 · Who gets which partition: assignment and rebalancing
In one sentence: one broker, the group’s coordinator, keeps the list of members; whenever it changes, the group rebalances: partitions are taken back and handed out again, either all at once (eager), only the ones that move (cooperative), or member by member, driven by the broker (the new protocol, KIP-848).
The problem it solves
Billing’s members change all the time: deploys restart them, a sale adds some, a machine dies at 3 a.m. Each change moves partitions between members, under two rules: never two owners (an order billed twice) and never unowned for long (its orders wait).
Somebody must decide the new share-out, take partitions from their old owners, and only then give them to the new ones, while the rest of the group keeps working.
The coordinator
The somebody is a broker. Every group has a coordinator: the broker that leads the partition of
__consumer_offsets where the group’s bookmarks go. The partition is picked from the group’s name: the Java hash
of group.id, made positive, modulo the topic’s 50 partitions. On this course’s cluster:
| Group | abs(hash) % 50 | Leader of that partition | Coordinator (--describe --state) |
|---|---|---|---|
| watchers | 17 | broker 3 | kcg-3:9092 (3) |
| billing | 9 | broker 2 | kcg-2:9092 (2) |
| audit | 5 | broker 1 | kcg-1:9092 (1) |
Each member finds its coordinator, then sends it heartbeats, “I’m still here” messages, every
heartbeat.interval.ms (3 s by default).
The classic protocol: join, then sync
The classic protocol is still the Java client’s default in 4.4 (group.protocol = classic in its startup
log). When the member list changes, the coordinator answers each member’s next heartbeat with “rebalance in
progress”, and then:
- Join. Each member sends a JoinGroup request. The coordinator waits until every known member has (re)joined.
- The leader assigns. The coordinator picks one member as the group’s leader and sends it the member list. The leader runs an assignor, a function in the client that maps partitions to members.
- Sync. The leader sends the result back; the coordinator gives each member its partitions.
A member only does steps 1 and 3 from inside poll(). Which assignor runs is the consumer setting
partition.assignment.strategy; the default is [RangeAssignor, CooperativeStickyAssignor], and the group uses
the first one every member lists: range.
Four members, then one leaves
Four members joined, then w2 left:
| Assignor | 4 members | After w2 left | Partitions that changed owner besides w2’s |
|---|---|---|---|
| range | w1 0,1 · w2 2,3 · w3 4 · w4 5 | w1 0,1 · w3 2,3 · w4 4,5 | 1 (partition 4) |
| round-robin | w1 0,4 · w2 1,5 · w3 2 · w4 3 | w1 0,3 · w3 1,4 · w4 2,5 | 3 (0 kept, 2, 3, 4 moved) |
| cooperative-sticky | w1 0,1 · w2 3,4 · w3 2 · w4 5 | w1 0,1 · w3 2,3 · w4 4,5 | 0 |
Range and round-robin start afresh; sticky keeps what each member has where it can.
Eager: everybody stops
Range and round-robin are eager: in the join step every member gives up all its partitions. When w3 joined two members:
05:25:20.005 w1 read 50 from [0, 1, 2]
05:25:20.090 w1 revoked [0, 1, 2] now []
05:25:20.101 w1 assigned [0, 1] now [0, 1]
9 to 14 ms in three runs: short, as both members poll every 100 ms. But the barrier waits for
the slowest member. Make the second member slow (10 ms a record, with a backlog: 500 records
and 5 s per poll()) and w1 sits empty until it rejoins:
03:51:06.566 w1 revoked [3, 4, 5] now []
03:51:07.047 w1 read 55 from [3, 4, 5]
03:51:08.052 w1 read 0 from []
03:51:09.055 w1 read 0 from []
03:51:10.004 w1 assigned [2, 3] now [2, 3]
(A read line counts the second before it.) In 26 runs w1 read nothing for 10 to 4,420 ms: the
“everything pauses” of a rebalance, up to one of the slow member’s 5 s batches (lesson 4).
Cooperative: only what moves
With CooperativeStickyAssignor a member gives up only the partitions that move, and keeps reading the others:
06:34:05.161 w1 revoked [5] now [1, 3]
06:34:05.161 w1 assigned [] now [1, 3]
06:34:06.005 w1 read 36 from [1, 3, 5]
06:34:07.016 w1 read 34 from [1, 3]
The price is a second rebalance: a revoked partition goes to w3 only in a follow-up round, once w1 has let go
(KIP-429).
It came one full heartbeat interval after the revoke: 2,989 to 3,027 ms at the default 3 s (16 runs),
1,001 to 1,012 ms with heartbeat.interval.ms=1000 (three runs). With the slow member’s 5 s batches, the moved
partition waited 10.1 to 15.2 s.
The new protocol: the broker assigns
group.protocol=consumer (KIP-848, on in brokers since Kafka 4.0) removes the barrier. The coordinator
computes the assignment itself (uniform by default, or range) and tells each member, in its own heartbeat
responses, what to give up and what to take.
w3 got its partitions 5,010 to 5,060 ms after joining (14 runs), about one of this protocol’s 5 s heartbeat intervals. A moved partition reached it 0.6 to 4.0 s after its revoke, different in each run; the partitions that stayed were read without a gap.
Static membership: a restart that isn’t a change
In a rolling deploy each restart is a leave and a join: two rebalances per member. Give each member a fixed name,
group.instance.id, and the coordinator keeps its partitions for it until session.timeout.ms runs out
(KIP-345).
w2, killed with docker kill and started again with the same id:
06:19:19.569 w2 assigned [2, 3] now [2, 3]
06:19:20.038 w2 read 439 from [2, 3]
The same two partitions, and no revoked or assigned line on w1 or w3; partitions 2 and 3 were unread until w2
was back. The other side: a static member doesn’t leave even when it stops cleanly. w3, stopped normally, kept
its partitions 30.1 to 31.1 s longer (three runs, session.timeout.ms=30000).
Try it on your computer
Practical 0 · Your cluster and a group you can watch:
write the Watcher behind these outputs, and watch each kind of rebalance on your own cluster.
Go deeper: what the broker knows in the new protocol
In a consumer group the coordinator holds each member’s assignment and an epoch, a counter that goes up
with every new target assignment. --describe --members --verbose shows both, and here they agree:
GROUP CONSUMER-ID HOST CLIENT-ID #PARTITIONS CURRENT-EPOCH CURRENT-ASSIGNMENT TARGET-EPOCH TARGET-ASSIGNMENT
asg-new 9QpM4yOjS42XK5VcWyiUhw /172.19.0.8 w4 2 6 orders:1,3 6 orders:1,3
asg-new zrK2UsJVQLyVL9X74XV2gw /172.19.0.7 w3 2 6 orders:2,5 6 orders:2,5
asg-new DybJvTKkQUucjujUJKCXzA /172.19.0.5 w1 2 6 orders:0,4 6 orders:0,4
During a rebalance a member’s current epoch lags the target until it has revoked and taken what it should. For a
classic group the epoch and target columns are -: only the current assignment is listed, the one the leader member
computed and the coordinator passed on.
The timings belong to the broker too (kafka-configs --describe --all): group.consumer.heartbeat.interval.ms=5000
and group.consumer.session.timeout.ms=45000. uniform spreads partitions evenly and sticks to current owners;
when w2 left asg-new, only w2’s two partitions moved. Docs:
the consumer rebalance protocol in Kafka 4.4,
KIP-848.
Go deeper: moving a running group to another assignor or protocol
The new protocol’s settings live on the broker, and a 4.4 client refuses the old ones:
Exception in thread "main" org.apache.kafka.common.config.ConfigException: partition.assignment.strategy cannot be set when group.protocol=CONSUMER
In a classic group every member must agree. A member whose only assignor is one the group doesn’t use can’t join:
06:37:52.577 w3 stopped: org.apache.kafka.common.errors.InconsistentGroupProtocolException: The group member's supported protocols are incompatible with those of existing members or first group member tried to join with empty protocol type or empty protocol list.
That is why the default lists two assignors: a member that lists both can join a range group or a cooperative one,
so you move from eager to cooperative in two rolling restarts (first add CooperativeStickyAssignor to the list,
then remove range).
Classic to consumer needs no second step: the brokers convert a group between the two protocols, an empty one
directly and a running one during a rolling restart where members switch one by one
(group.consumer.migration.policy=bidirectional by default).
Common misunderstandings
- “Static membership makes restarts free.” It avoids the rebalance, not the wait: the restarted member’s partitions are unread until it is back, and a member that really died keeps its partitions until the session timeout.
Check yourself
1. An eager group of 10 members; one of them processes a batch for 40 s between polls. A new member joins. How long can the other nine read nothing?
Up to about 40 s: each gives up all its partitions in the join step, and the coordinator waits for every member to rejoin, which the busy one only does at its nextpoll(). With cooperative rebalancing the nine keep
reading what they keep.
2. You switch a running group from range to CooperativeStickyAssignor by restarting members one at a time with only the new setting. What does the first restarted member see?
An InconsistentGroupProtocolException: the others use range only, so it can't join. List both
assignors in the first rolling restart, then drop range in a second.
3. Two groups, billing and billing-v2. Same coordinator?
Only by chance. The coordinator is the leader of partition abs(hash(group.id)) % 50 of
__consumer_offsets, so different names usually land on different partitions, and often on different
brokers.
Next: Practical 0 · Your cluster and a group you can watch, then lesson 3, on what a commit really guarantees.