Watch a Kafka consumer group rebalance.
Add a consumer, stop one, crash one. See which partitions change owner, who has to stop reading while it happens, and how much lag and replay each choice costs.
- C12 part.
- C22 part.
- C32 part.
- 0C1
- 1C1
- 2C2
- 3C2
- 4C3
- 5C3
Each bar is one partition: its length is the lag (records written and not yet processed) and the tag at the right is the consumer that owns it. 100 records a second arrive on every partition.
| Assignor | Moved last time | Stopped (partition-s) | Peak lag | Replayed |
|---|---|---|---|---|
| – | 0 | 0 | 0 | |
| – | 0 | 0 | 0 | |
| – | 0 | 0 | 0 | |
| – | 0 | 0 | 0 |
Moved: partitions that changed owner in the last rebalance. Stopped: partition-seconds nobody read because of rebalances. Replayed: records processed twice after a crash.
With two or more, Range gives the first consumers the extra partition of every topic.
How long a crashed consumer goes unnoticed. Kafka's default has been 45 s since version 3.0.
RangeAssignor: Per topic, consecutive partitions to each consumer. Everyone stops during a rebalance. Add a consumer, stop one or crash one, then compare the four rows of the table: every assignor is replaying the same events.
- Nothing has happened yet. Add, stop or crash a consumer.
What you are looking at in the Kafka consumer group rebalancing lab
In Kafka, the consumers of a consumer group share the partitions of the topics they read: each partition has exactly one reader in the group. When the membership changes, because a consumer joins, shuts down or crashes, the group rebalances: it decides again who owns what. Each bar in the lab is a partition. Its length is its lag, the records written and not yet processed, and the tag at its right is its owner.
Two separate choices decide how much a rebalance hurts. The assignor decides which partitions move. Range and round robin recompute the split from scratch, so partitions change owner that did not need to; the sticky assignors keep everything they can. The protocol decides who stops. Under the eager protocol every consumer gives up every partition before the new plan arrives, so the whole group stops reading. Under the cooperative protocol only the partitions that move are taken away, and the rest keep flowing.
All four assignors run on the same events at once, and the table compares them. The numbers in the lab are Kafka's own where it has them: the consumer's default assignors are Range and then CooperativeSticky, offsets are committed every 5 seconds by default, and a silent consumer is removed after the session timeout (45 seconds by default since Kafka 3.0; the lab starts at 10 so you do not have to wait). The model simplifies a rebalance to a fixed pause and a cooperative rebalance to one pause on the moving partitions; in a real cluster it is two short rounds.
Things to try in the Kafka consumer group rebalancing lab
- Press + Add consumer with Range selected. Every bar turns amber and grows: the whole group has stopped. Then read the table: the cooperative row stopped a fraction of the partition-seconds.
- Select Cooperative sticky and add another consumer. Only the partitions that move are stopped; the others never notice.
- Crash a consumer. Nothing happens for the whole session timeout: its partitions have no reader and the group does not know. Then look at the Replayed column: the records it processed after its last commit are processed again.
- Now press Stop on a consumer instead. It commits and says goodbye, so the rebalance starts at once and nothing is replayed.
- Set Topics to 2 and Partitions per topic to 3, then reset to three consumers and compare Range with Round robin. Range hands the extra partition of both topics to the same consumer.
- Keep adding consumers until there are more than partitions. The extra ones own nothing: a partition has one reader per group.
- Lower what one consumer can process to 250 and crash one. The survivors cannot absorb its partitions, and lag grows without limit. Rebalancing moves work; it does not add capacity.
Why Kafka consumer group rebalancing matters in a system design interview
“Use Kafka with a consumer group” is where an interviewer starts asking about failure. Good answers cover four things. A partition has one reader per group, so the partition count is the ceiling on parallelism. A crashed consumer is noticed only after the session timeout, and a consumer that takes too long between polls (max.poll.interval.ms, 5 minutes by default) is removed as well. After a crash the new owner starts from the last committed offset, so anything processed after that commit runs twice: delivery is at-least-once and the handler must be idempotent. And a rebalance is a pause, so frequent ones show up as lag.
Then the ways to make rebalances cheaper: the cooperative sticky assignor so only moving partitions pause; static membership (group.instance.id) so a consumer that restarts within the session timeout gets its partitions back with no rebalance, which is what you want for rolling deploys; and, from Kafka 4.0, the new consumer group protocol (group.protocol=consumer), where the broker computes assignments and reconciles each member incrementally. The classic protocol is still the default, so name the one you mean.
The logs, streams and Kafka lesson has the rest: partitions and ordering, both commit orders under crashes, which replication settings can lose acknowledged records, compaction and stream processing.