Skip to content

7.13 Kafka consumer groups

This side quest is a hands-on lab for the other common worker shape, a pool of consumers reading a partitioned log. A worker is a process that pulls work and does it; this lab is about the operations: how many workers run in parallel, what happens on failure, and how you size and scale the pool. The concepts are explained in Partitioned consumers in this chapter’s opening, and the messaging dynamics behind them (queue versus log, push versus pull, delivery semantics) are in Messaging and Distributed Queueing in Systems Engineering. The lab also uses KEDA, SIGTERM handling, and graceful shutdown from Containers, Kubernetes, and Workloads. It is optional and sits outside the main line of the book.

Concepts from other sections (9)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

Learning objectives

  • Show that partition count is the parallelism ceiling: within one consumer group a partition is owned by exactly one consumer, so a fifth worker on a four-partition topic sits idle.
  • Observe rebalancing, and explain why eager reassignment stalls the whole group and why a slow handler that misses max.poll.interval.ms triggers a false rebalance and reprocessing.
  • Make redelivery safe with idempotency keys, the transactional outbox, and commit-after-durable-work.
  • Use lag as the master signal for alerting and KEDA scaling, and explain why at the partition cap you add partitions, not pods.

The directory holds the same consumer in three languages: consumer-go.go, consumer-rust.rs, and consumer-python.py. Each implements one pattern: at-least-once consumption with an idempotency guard, cooperative rebalancing, explicit offset commits after the handler returns, and a clean leave from the group on SIGTERM. Read them side by side; the differences are only in each client library’s API. The diagram kafka-consumption.svg (source kafka-consumption.d2) shows the partition-to-consumer topology.

Run a local Kafka or Redpanda via Docker. Produce 1000 messages to an orders topic, then start consumers one at a time and watch the partitions rebalance across them as each joins.

Next, add one consumer more than the partition count and confirm that it sits idle. This is the topology diagram from Partitioned consumers, made real: no partition is left for the extra member to own, so adding it buys nothing.

Finally, test redelivery. Kill a worker mid-batch, before it commits, and confirm that its messages are redelivered to another member of the group. Then add an idempotency guard and confirm the duplicate is absorbed: the second delivery sees the key already claimed, skips the work, and lets the offset advance.

  • Partition count is the parallelism ceiling; workers beyond it sit idle.
  • Rebalancing is what bites you: use cooperative rebalancing, keep handlers inside max.poll.interval.ms, and leave the group cleanly on SIGTERM.
  • At-least-once means designing for duplicates: idempotency keys, the transactional outbox, and committing only after durable work.
  • Lag drives scaling and alerts; at the partition cap, add partitions, not pods.