What it is
A consumer group is a set of consumers that share the work of reading a topic. They all use the same group.id. The group coordinator (a broker) assigns each partition of the topic to exactly one member, and the group commits one offset per partition: how far it has processed. Different groups reading the same topic are independent of each other.
Why it exists
Partitions let a topic be read in parallel, but someone has to decide which consumer reads which partition, notice when a consumer disappears, and remember where each partition was left off. Consumer groups do this, so you can scale a service by starting more instances and survive crashes without losing your place in the log. Separate groups give pub/sub: every service that needs the stream reads it with its own group.
How it works
Assignment. When members join or leave, the group rebalances: partitions are distributed among the current members by an assignor (range, round robin, sticky, cooperative sticky, or, with the KIP-848 protocol, by the broker). A member may own several partitions; a partition never has two owners in one group. That keeps per-partition order and means members beyond the partition count get nothing.
Liveness. Members send heartbeats. If none arrive for session.timeout.ms (45 s by default since Kafka 3.0), the coordinator evicts the member. Separately, a consumer that does not call poll() within max.poll.interval.ms (5 minutes by default) leaves the group, even if its heartbeat thread is alive, because it is evidently stuck. A clean shutdown leaves the group at once.
Rebalancing. With the classic eager protocol, every member revokes all its partitions, the group agrees on a new assignment, and members resume: a pause for everyone. Incremental cooperative rebalancing (the CooperativeStickyAssignor, Kafka 2.4+) revokes only the partitions that move. The new consumer group protocol (KIP-848, generally available in Kafka 4.0) moves assignment to the broker and reconciles members incrementally, without a global synchronisation barrier.
Offsets and redelivery. Consumers commit offsets to the internal __consumer_offsets topic, automatically on an interval or explicitly after processing. When a partition changes owner, the new owner starts from the last committed offset. Anything processed but not committed is processed again: delivery is at-least-once unless you use transactions end to end.
Static membership. Setting group.instance.id gives a member a stable identity. If it restarts within the session timeout, it gets its old partitions back without a rebalance, which makes rolling deployments much calmer.
When to use it
- Scaling a consuming service horizontally: start instances with the same
group.id. - Giving each downstream service its own full copy of a stream: one group per service.
- Any consumer that must resume where it left off after a restart.
When not to
- When you need more parallelism than partitions and order does not matter: a work queue, Kafka share groups (KIP-932, in the 4.x line) or a parallel consumer library distributes per message.
- When a single process must see all partitions anyway (a small admin tool): manual assignment (
assign()) without a group avoids rebalances. - When membership changes constantly (very short-lived consumers): every join and leave costs a rebalance.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Short session timeout | Faster takeover of a dead consumer’s partitions | Healthy members evicted by GC pauses or network blips, extra rebalances |
| Long session timeout | Fewer false evictions | Partitions of a crashed member sit unread longer |
| Cooperative / KIP-848 protocol | No group-wide pause | Newer behaviour to learn; several rounds for some changes |
| Static membership | Restarts without rebalances | A truly dead member is only replaced after the session timeout |
| Commit after processing | No lost records on crash | Duplicates after a rebalance; processing must be idempotent |
| Auto-commit on an interval | Simple | Can commit records not yet processed (loss) or re-process a window (duplicates) |
Alternatives
- Manual assignment: the application picks partitions itself (Kafka Streams standby tasks, custom schedulers). Full control, no automatic failover.
- Work-queue brokers (RabbitMQ, SQS) with competing consumers: no partition ceiling and per-message acks, but no replay and weaker ordering.
- Share groups (Kafka 4.x): queue-like cooperative consumption of a topic with per-record acknowledgement.
Failure modes
- Rebalance storms: consumers that exceed
max.poll.interval.msbecause a batch is slow get kicked out, rejoin, and trigger rebalance after rebalance. Lowermax.poll.recordsor make processing faster. - Idle members mistaken for capacity: dashboards show 12 healthy consumers while 4 have no partitions.
- Duplicate side effects after a rebalance, from records processed but not committed.
- Lag on one partition only: its owner is slow or stuck; per-partition lag shows it, per-group averages hide it.
- Committing too early: auto-commit can mark records done before a crash loses them.
In production
Alert on per-partition consumer lag (records and time), on rebalance rate, and on members without assignments. Use a cooperative assignor or the KIP-848 protocol, and static membership for services that deploy often. Commit offsets after processing (or in the same transaction as the output, with Kafka transactions), and make handlers idempotent. Size the group to the partition count, and keep the partition count a multiple of typical group sizes so assignments are even.
Common misconceptions
- "Each record goes to the least-busy consumer." Distribution is per partition, not per record.
- "More consumers always help." Not past the partition count.
- "A crashed consumer is replaced immediately." Only after the session timeout, unless it left cleanly.
- "Two groups compete for records." Groups are independent; each reads everything.
- "Kafka delivers exactly once to consumer groups." By default it is at-least-once; exactly-once needs transactions or idempotent processing.
Interview angle
Expect to be asked how you would scale a lagging consumer: the right answer starts with "consumers are capped by partitions", then considers more partitions (planned, because keys move) or cheaper processing. Explain what happens when a consumer dies (session timeout, rebalance, redelivery from the last committed offset) and why handlers must be idempotent. Mentioning cooperative rebalancing or static membership shows production experience.
Model assumptions
The simulation in this lesson is a simplified model, not a benchmark. Producers send 90 records per second at random (Poisson), without keys, spread evenly over three partitions. Each partition is a queue; a consumer is connected to the partitions it owns. A consumer works on one record at a time, about 20 ms (lognormal, cv 0.3) plus a 3 ms fetch hop, roughly 43 records per second; when it owns N partitions, each gets 1/N of its time, modelled as N × 20 ms per record on each. Failure detection and rebalances are scripted timed actions: the session timeout is compressed to 1.5 s, and a paused consumer is modelled as having no processing slots. An unacknowledged record is redelivered after 1.5 s. A second group is modelled by copying each partition’s records into one queue per group; the queue’s depth stands for that group’s lag. Kafka stores the log once and keeps offsets per group. In the challenge each partition holds at most 100 unprocessed records and rejects the rest. Not modelled: offset commit timing, duplicate processing after a rebalance, broker failures and the rebalance protocol’s messages. Numbers show behaviour and orders of magnitude only.