Learn Labs
Consumers

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.

consumer-xgroup: analyticsP0P1P2P3consumer-agroup: billingconsumer-bgroup: billingwithin a group each partition has one owner; different groups each read everything

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]
b joinsc joinsb leavesconsumer-a0-50,1,20,10,1,4consumer-b3,4,53,4consumer-c2,52,3,5lab time →

Three rules follow:

  1. 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.
  2. Ordering is per partition, so it holds even as partitions move: only one consumer owns a partition at a time.
  3. 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  3

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.

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

  1. Run consumer_groups.py. Then describe billing with --members while it's running (in another terminal) to see the coordinator's view.
  2. Change the topic to 2 partitions and run it again. What do the third consumer's assignments show?
  3. Remove "group.protocol": "consumer" (back to classic) and compare how partitions move.

On this page