Lesson 07 of 17 · How Kafka works
Consumers and consumer groups
How consumers read: the poll loop, where offsets are stored and when to commit them, what auto.offset.reset does for new groups, how groups assign partitions and rebalance when members come and go (classic, cooperative and the new KIP-848 protocol), static membership, the timeouts that kick slow consumers out, and how to read and reduce consumer lag.
The poll loop
A consumer is a loop:
subscribe("payments")
loop:
records = poll(timeout) # fetch a batch (up to max.poll.records)
for r in records: process(r)
commit offsets # "I have finished up to here"
poll() also does the housekeeping: joining the group, receiving partition assignments and fetching data. If a consumer stops calling poll() for longer than max.poll.interval.ms (5 minutes by default), the group assumes it is stuck and gives its partitions to someone else.
Offsets: where you are
Committed offsets are stored inside Kafka, in an internal topic called __consumer_offsets, per group, topic and partition. The committed offset is the next record to read.
When you commit decides your delivery guarantee:
| Commit… | If the consumer crashes | Guarantee |
|---|---|---|
| Before processing | Records in the batch are skipped | At-most-once |
| After processing | Records since the last commit are processed again | At-least-once (the usual choice) |
| In the same transaction as the output | Neither | Exactly-once (lesson 10) |
Auto-commit (enable.auto.commit=true, the default) commits in the background every 5 seconds during poll(). It is fine for simple cases, but for important data turn it off and commit explicitly after processing. Either way, design consumers to cope with seeing a record twice.
Where does a new group start?
auto.offset.reset applies only when a group has no committed offset (or it points to data already deleted):
latest(default): start at the end; only new records.earliest: start at the oldest retained record.none: fail, so a human decides.
A common surprise: a new analytics consumer starts with latest and "misses" all existing data.
How a group shares partitions
One broker acts as the group coordinator. When members join or leave, the group rebalances: partitions are reassigned.
Topic payments: P0 P1 P2 group "fraud"
1 member: C1 ← P0 P1 P2
2 members: C1 ← P0 P1 C2 ← P2
C1 crashes: C2 ← P0 P1 P2 (after session.timeout.ms)
Real output from the lab cluster with two console consumers in the group fraud:
$ kafka-consumer-groups.sh --bootstrap-server kafka-1:9092 --describe --group fraud --members
GROUP CONSUMER-ID HOST CLIENT-ID #PARTITIONS
fraud console-consumer-268450c6-5096-44fd-98e5-35e4f2d7df8c /172.19.0.3 console-consumer 2
fraud console-consumer-44bd6fa2-022c-45af-b600-6428b46d4d3d /172.19.0.4 console-consumer 1
$ kafka-consumer-groups.sh --bootstrap-server kafka-1:9092 --describe --group fraud --state
GROUP COORDINATOR (ID) ASSIGNMENT-STRATEGY STATE #MEMBERS
fraud kafka-1:9092 (1) range Stable 2
Rebalance protocols
| Protocol | How it behaves |
|---|---|
| Classic, eager (range, round-robin) | Every member stops and gives up all partitions, then gets a new set: a short "stop the world" |
| Classic, cooperative-sticky | Only the partitions that must move are revoked; others keep working |
| New consumer protocol (KIP-848) | The broker computes assignments and moves partitions incrementally; no group-wide pause. Production-ready in Kafka 4.0; clients opt in with group.protocol=consumer |
Static membership
In Kubernetes, pods restart often. Each restart normally means "member left, member joined" and two rebalances. Set group.instance.id to a stable name (for example the StatefulSet pod name): a member that comes back within session.timeout.ms keeps its partitions with no rebalance.
A group of friends shares out the chores (partitions). When a new friend arrives, the old way was everyone drops their chores and the list is redone from scratch. The cooperative way is "only move the chores that need moving", so most people keep working. Static membership is a friend saying "I'm just popping out, keep my chores for me", so nothing is reshuffled when they step out for a minute.
Timeouts that remove members
| Setting | Default | Meaning |
|---|---|---|
heartbeat.interval.ms |
3 s | How often a background thread says "I'm alive" |
session.timeout.ms |
45 s | No heartbeat for this long → member is dead |
max.poll.interval.ms |
5 min | No poll() for this long → member is stuck |
max.poll.records |
500 | Records per poll; lower it if processing is slow |
Slow processing is the classic cause of rebalance storms: a member exceeds max.poll.interval.ms, is removed, its partitions move, it rejoins, and the cycle repeats while lag grows.
Consumer lag
Lag = log-end offset − committed offset, per partition. It tells you how far behind a group is.
| Pattern | Likely cause | What to do |
|---|---|---|
| Lag grows on all partitions | Consumers too slow, or a producer spike | Add consumers (up to the partition count), speed up processing, batch writes downstream |
| Lag grows on one partition | Hot key, or a stuck record on that partition | Fix the key design; find the record that fails |
| Lag flat but high | Consumers keep up with new data but never caught up | Temporary extra consumers, or accept and watch |
| Lag jumps and members change | Rebalance storm | Check max.poll.interval.ms, use cooperative or KIP-848, static membership |
Alert on lag in time ("5 minutes behind") as well as records: 10,000 records may be one second or one hour depending on the topic.
Try it: groups in action
- On the lab cluster, create a topic with 4 partitions and start 2 console consumers in one group; check
--members. - Start a third and a fourth; then a fifth. When does a member get zero partitions?
- Stop one consumer with Ctrl+C and time how long until its partitions move (
--describerepeatedly). - Produce 50,000 records with the perf tool while only one consumer runs, and watch lag fall as you add consumers.
- Start a consumer in a brand-new group without
--from-beginning, then with it, and explain the difference usingauto.offset.reset.
Going deeper
- For parallelism beyond the partition count, process records from one partition on several threads while keeping per-key order (for example Confluent's parallel consumer pattern), or add partitions with a planned key migration.
- Kafka 4.x also adds share groups (KIP-932, "queues for Kafka"), where several consumers can work on the same partition with per-record acknowledgement, like a queue. Check your version's status before relying on it.
- Use consumer lag exporters (Kafka Exporter, Burrow, or your managed service's metrics) rather than running the CLI from cron.
Recap
- Consumers poll, process and commit offsets, which live in
__consumer_offsets. - Commit after processing for at-least-once; make processing safe to repeat.
auto.offset.resetdecides where a new group starts:latestby default.- Groups rebalance when members change; prefer cooperative or KIP-848, and static membership on Kubernetes.
- Watch lag, and keep processing time well under
max.poll.interval.ms.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.