Course map

Topic 108 / Core

Consumer groups, partitioning and competing consumers: consumer lag, partition rebalancing

Chapter 7.1 · Asynchronous Foundations

What and why

Competing consumers (→ 107) share work but say nothing about order or how that work is actually divided up. A partition is an ordered sub-log a topic is split into; a consumer group is a set of consumers that divides a topic's partitions among its members so each partition is read by exactly one group member at a time — horizontal scale without losing order within a partition. Builds on: → 107.

Diagram

Topic (4 partitions)         Consumer group (2 members)
P0 ─┐                        C1 owns P0, P1
P1 ─┼─ assigned to group ──► C2 owns P2, P3
P2 ─┤
P3 ─┘
consumer lag(Pn) = latest offset(Pn) − committed offset(Pn)

How it works

  1. The producer picks a partition per message, usually by hashing a key — the same key always lands on the same partition, giving ordering per key (→ 114).
  2. Partitions are assigned among the group's members; parallelism is capped by partition count — 10 partitions split among at most 10 productive consumers.
  3. Each consumer tracks and commits its own offset per partition it owns.
  4. Consumer lag — the gap between a partition's latest offset and the group's committed offset — is the primary health metric: rising lag means consumers are falling behind producers.
  5. Partition rebalancing happens when a consumer joins, leaves, or is presumed dead (missed heartbeat): the group reassigns partitions among the remaining members, briefly pausing consumption, and can redeliver messages that were processed but not yet committed.

Practical example

(illustrative). 8 partitions receive 8,000 messages/min. Four consumers at 900/min each give 3,600/min of capacity, so lag grows by 8,000 − 3,600 = 4,400/min. Adding four more consumers (8 total, one per partition) raises capacity to 8 × 900 = 7,200/min — lag growth drops to 8,000 − 7,200 = 800/min but is still positive; closing the remaining gap needs cheaper per-message processing or more partitions, not more consumers past the partition count.

When to use it

  • Use when ordered, parallel, replayable consumption of a stream is needed.
  • Avoid when per-key order doesn't matter — a plain competing-consumers queue (→ 107) with no partitions is simpler and has no rebalancing pauses.
  • Failure scenarios: a hot partition (one key dominating traffic) bottlenecks a single consumer while others idle, and adding consumers beyond the partition count does nothing; flaky consumers mistaken for dead ones cause repeated pause-and-reprocess cycles.
  • Simpler alternative: a single-partition queue, if the topic's whole throughput fits on one consumer.

Practice

(numeric and mechanism — drills with an answer key; 20–30 min, lab Kafka). Create a 4-partition topic; run two consumers in one group and note which partitions each owns; add a third consumer and observe the rebalance and new ownership; kill one consumer mid-processing and time how long until its partitions are reassigned. From your measured per-consumer throughput, calculate the minimum consumer count needed to keep lag flat at a stated arrival rate, then verify it. Done when the calculation matches what you measure within a small margin.

Shared lab setup

Key takeaway

Partitions bound parallelism and preserve per-key order; consumer lag tells you whether you're keeping up. Done when you can: compute the minimum consumer count to hold lag flat, and explain what a rebalance does to in-flight work. Used next in → 111 (Kafka's partitioned log realizes this model), → 114 (ordering depends on the partition key).

References

Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide