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.
- KEDA: an operator that scales workloads on external metrics by creating and driving an HPA. Read more: 10.7 Durable and worker images and charts
- graceful shutdown: stop accepting, let in-flight work finish within a bound, then exit 0. Read more: 1.9 Async Rust and tokio: futures, tasks, channels, cancellation, hyper in Programming and Algorithms
- Consumer group: a set of consumers that divide a topic’s partitions among themselves; different groups each see every message. Read more: 5.3 Coordination and technology choice
- Idempotency key:
"<workflow_id>/<activity_id>", stable across attempts and runs, so an activity’s effect can be applied once. Read more: 7.4 The task protocol and workers - Transactional outbox: writing business state and the outgoing event in the same database transaction, with a separate relay publishing from the outbox table, so state and event commit atomically with no dual-write inconsistency. Read more: Chapter 7: Durable Orchestration and Workers
- At-least-once: work is never dropped but may be processed more than once. Read more: Chapter 13: Case Study: Exactly-Once Event API
- Idempotency: the property that applying an operation more than once has the same effect as applying it once. Read more: Chapter 3: RPC and Protocols
- Cooperative rebalancing: an incremental rebalance protocol that revokes only the partitions that must move while other members keep consuming, unlike eager rebalancing, which stops the whole group. Read more: Chapter 7: Durable Orchestration and Workers
- Dead letter: an activity whose retries are exhausted, parked in the DLQ with
ActivityDeadLettereduntil an operator redrives it. Read more: 7.5 Activities
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.mstriggers 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 code
Section titled “The code”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.
The lab
Section titled “The lab”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.
Summary
Section titled “Summary”- 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.
Further reading
Section titled “Further reading”- Apache Kafka documentation, especially the Consumer and Consumer Group Protocol sections.
- Confluent, Consumer group protocol, for rebalancing in depth.
- Martin Kleppmann, Designing Data-Intensive Applications, chapter 11, “Stream Processing”.
- Enterprise Integration Patterns: Competing Consumers, Dead Letter Channel, Idempotent Receiver.
- KIP-429: Incremental Cooperative Rebalancing.
- microservices.io, Transactional Outbox.