Consumer Groups
Partition assignment, scaling consumers, and the KIP-848 rebalance protocol
A consumer group is a set of consumers that share the work of reading a topic. Kafka assigns each partition to exactly one member of the group, so records are processed once per group, in order per partition. Different groups are independent: billing and analytics each read every record. The book's Consumers chapter covers the classic protocol; this lesson uses the new one from Kafka 4.
Assignment and scaling
labs/consumer_groups.py starts three consumers in group billing on a
6-partition topic, one after another, then stops one:
after consumer-a joined:
consumer-a: partitions [0, 1, 2, 3, 4, 5]
after consumer-b joined:
consumer-a: partitions [0, 1, 2]
consumer-b: partitions [3, 4, 5]
after consumer-c joined:
consumer-a: partitions [0, 1]
consumer-b: partitions [3, 4]
consumer-c: partitions [2, 5]
after consumer-b left:
consumer-a: partitions [0, 1, 4]
consumer-c: partitions [2, 3, 5]Three rules follow:
- Parallelism is capped by partitions. A 7th consumer on a 6-partition topic sits idle. Choose partition counts for the consumer parallelism you'll need.
- Ordering is per partition, so it holds even as partitions move: only one consumer owns a partition at a time.
- Membership changes move partitions, which is a rebalance.
Rebalancing, old and new
The classic protocol had the group's members coordinate among themselves: on any change, every consumer stopped, gave up its partitions, rejoined, and waited for a new plan (the "stop-the-world" rebalance). Cooperative-sticky assignment later reduced the damage, but the client still drove the process.
The new protocol (KIP-848, group.protocol=consumer, GA in Kafka 4)
moves the logic to the broker's group coordinator. Each consumer sends
heartbeats; the coordinator computes the target assignment and moves
partitions incrementally, revoking only what has to move. Consumers
that keep their partitions never stop. In the output above, when
consumer-c joined, consumer-a kept 0 and 1 and consumer-b kept 3 and 4;
only 2 and 5 moved.
Consumer({
"bootstrap.servers": BOOTSTRAP,
"group.id": "billing",
"group.protocol": "consumer", # KIP-848
"auto.offset.reset": "earliest",
})
kafka-consumer-groups.sh $B --describe --group billing --state
GROUP COORDINATOR (ID) ASSIGNMENT-STRATEGY STATE #MEMBERS
billing kafka-2:19092 (2) uniform Stable 3Snapshots taken right after a membership change can show a partition assigned to nobody. While developing the lab, an early version printed assignments after 4 seconds and caught partition 2 mid-move. Incremental rebalancing means the assignment converges over a few heartbeats rather than in one jump. Don't treat a momentarily unassigned partition as a bug.
What triggers a rebalance
- A consumer joins or leaves (
close()). - A consumer stops heartbeating, for example it crashed or the network is partitioned. After the session timeout (45 s by default), it's removed.
- A consumer takes too long between
poll()calls (max.poll.interval.ms, 5 minutes). It's considered stuck and kicked out, even though it's alive. - Partitions are added to a subscribed topic.
The max.poll.interval.ms case is the classic production rebalance
loop: a consumer fetches 500 records, processing takes 6 minutes, it's
removed, its partitions go to someone else, it comes back and rejoins,
and the cycle repeats while lag grows. Fix it by processing fewer
records per poll (max.poll.records), making processing faster, or
moving slow work off the poll thread.
Static membership
A consumer restart (a deploy) normally looks like "leave, then a new member
joins," which moves partitions twice. Setting group.instance.id to a
stable value per instance (like the pod name) makes the restart invisible:
the coordinator holds that member's partitions until the session timeout
instead of rebalancing immediately.
Rebalance callbacks
Your code can react to assignment changes, for example to commit offsets or flush state for partitions being taken away:
def on_revoke(consumer, partitions):
consumer.commit(asynchronous=False) # don't let the next owner redo our work
consumer.subscribe(["payments"], on_assign=on_assign, on_revoke=on_revoke)Lab
- Run
consumer_groups.py. Then describebillingwith--memberswhile it's running (in another terminal) to see the coordinator's view. - Change the topic to 2 partitions and run it again. What do the third consumer's assignments show?
- Remove
"group.protocol": "consumer"(back to classic) and compare how partitions move.