Course contents

07. Messaging and Event-Driven Systems Lesson 6 of 10

Consumer Groups

How a consumer group shares partitions, why extra consumers sit idle, what a rebalance costs when a consumer dies, and how separate groups each read everything.

15 minIntermediate8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A group splits a topic’s partitions among its members; each partition has exactly one owner.
  • Useful members are capped by the partition count; the rest are idle standbys.
  • A crashed consumer’s partitions wait for the session timeout, then a rebalance hands them to others.
  • Eager rebalances pause the whole group; cooperative rebalances move only the affected partitions.
  • Every group reads the whole topic and tracks its own committed offsets.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

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.ms because a batch is slow get kicked out, rejoin, and trigger rebalance after rebalance. Lower max.poll.records or 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.

The lesson as textEvery step of the simulation, written out.
  1. 1. A group with one consumer

    The orders topic has three partitions, each receiving 30 records per second. The billing service reads it as a consumer group named billing. Right now the group has one member, Billing 1, so the group assigns it all three partitions.

    One consumer processes about 43 records per second, split between its three partitions. That is less than the 90 arriving, so lag grows on every partition.

    Scene: Producers send 90 records per second, spread evenly over three partitions. The billing group has one consumer, Billing 1, which owns all three partitions and cannot keep up: every partition’s backlog grows.

    Key idea: A consumer group shares a topic’s partitions among its members. With one member, it gets them all.

  2. 2. A second member joins

    Billing 2 starts with the same group.id. The group rebalances: partitions are redistributed so each is owned by exactly one member. Three partitions over two consumers cannot be even: Billing 1 keeps P0 and P1, Billing 2 gets P2.

    P2 now has a consumer to itself and stays near empty. Billing 1 still splits its time between two partitions, a little too slowly for 60 records per second, so P0 and P1 still lag. Partition counts that divide evenly by the consumer count spread work better.

    Scene: Billing 2 joins the group and takes P2. Billing 1 keeps P0 and P1. P2 stays near empty; P0 and P1 still build a backlog slowly.

    Key idea: Each partition has exactly one owner in the group. Uneven division leaves some members with more work.

  3. 3. One consumer per partition

    A third member, Billing 3, joins. The rebalance moves P1 from Billing 1 to Billing 3; P0 and P2 stay where they were (a sticky assignment moves as little as possible).

    Now each partition has its own consumer, about 43 records per second against 30 arriving. Every partition’s lag stays near zero. This is the most parallel this group can be: three partitions, three useful consumers.

    Scene: Billing 3 joins and takes P1. Each of the three partitions now has its own consumer, and all backlogs stay near empty.

    Key idea: A group is fully parallel when it has one consumer per partition.

  4. 4. A fourth consumer has nothing to do

    To be safe, the team starts a fourth consumer, Billing 4. It joins the group, but there are only three partitions and each already has its owner. Billing 4 gets no partition and reads nothing.

    It is not useless: it is a warm standby that can take over a partition if a member fails. But it adds no throughput. To scale a group beyond three consumers, the topic needs more partitions.

    Scene: Billing 4 joins the group but receives no partition. It processes nothing; the other three consumers carry on as before.

    Key idea: Consumers beyond the partition count sit idle. The partition count caps a group’s parallelism.

  5. 5. A consumer dies: stop-the-world rebalance

    Half a second in, Billing 3 crashes. Nobody reads P1 now, and its lag starts to grow.

    The group notices only when Billing 3’s heartbeats stop for the session timeout (45 s by default in Kafka; compressed to 1.5 s here). Then it rebalances with the classic eager protocol: every member gives up all its partitions and waits for a new assignment. For a second, nobody reads anything and every partition builds lag.

    At 3 s the new assignment arrives: Billing 4 takes P1 and the others resume. The record Billing 3 was working on had not been committed, so it is delivered again to Billing 4. Everything drains; nothing is lost.

    Scene: Billing 3 crashes at 0.5 s and P1’s backlog grows. At 2 s the group rebalances and all consumers pause for one second, so every partition builds a backlog. At 3 s Billing 4 owns P1, the others resume, and all backlogs drain.

    Key idea: A dead consumer’s partitions wait until the session timeout, and an eager rebalance pauses the whole group.

  6. 6. Cooperative rebalancing: only the orphan waits

    Same failure, but the group uses incremental cooperative rebalancing (the CooperativeStickyAssignor, or the new consumer group protocol from KIP-848, generally available since Kafka 4.0, where the broker computes assignments).

    Only partitions that actually change owner are revoked. Billing 1 and Billing 2 never stop: P0 and P2 stay near empty the whole time. P1 still waits for failure detection, then Billing 4 picks it up at 2 s and drains it.

    Detection time remains the main cost. Shorter session timeouts detect crashes faster but evict consumers that only paused (a long GC, a slow batch). Static membership (group.instance.id) lets a restarting consumer rejoin without any rebalance.

    Scene: Billing 3 crashes at 0.5 s. Billing 1 and Billing 2 keep processing P0 and P2 without a pause. P1’s backlog grows until Billing 4 takes it over at 2 s, then drains.

    Key idea: Cooperative rebalancing moves only the affected partitions, so a failure no longer pauses the whole group.

  7. 7. A second group reads everything again

    The analytics team needs the same orders. They start their own consumers with group.id = analytics.

    Groups are independent: each group gets every record of every partition, and inside each group the partitions are shared among that group’s members. Billing’s three consumers and Analytics’ three consumers each see all 90 records per second.

    The broker stores the log once. Each group just keeps its own committed offset per partition (here drawn as one lag queue per group and partition). A slow or stopped group falls behind on its own offsets without affecting the other.

    Scene: Two consumer groups read the topic. Billing (Billing 1, 4 and 2) and Analytics (three consumers) each receive every record from all three partitions; within each group, each partition has one consumer.

    Key idea: Every group reads the whole topic; members of one group split it. Groups are tracked by their own offsets.

  8. 8. Sizing and running a group

    A group shares partitions among its members, one owner per partition. Useful members ≤ partitions; extras are standbys. Each group reads everything and tracks its own offsets.

    Rebalances are the operational cost: detection waits for the session timeout, and eager protocols pause the group. Prefer cooperative or the KIP-848 protocol, static membership for rolling restarts, and processing that finishes well within max.poll.interval.ms so a slow batch is not mistaken for a dead consumer. Commit offsets after processing, and make processing idempotent: a rebalance can redeliver records whose offsets were not yet committed.

    Scene: Two consumer groups, Billing and Analytics, each with three consumers, read all three partitions. All backlogs stay near empty.

    Key idea: Partitions cap a group; rebalances cost a pause; offsets after processing plus idempotency make failures safe.