Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

4 · Consumer groups in production

In one sentence: a group judges each member by two clocks, heartbeats (is the process alive?, session.timeout.ms) and polls (is it making progress?, max.poll.interval.ms), and you judge the group by one number, lag: how far its bookmarks are behind the end of each partition.

The problem it solves

At 3 a.m. billing’s lag alert fires. The members are “running”, but is one of them dead, stuck in a slow batch, or just too slow for the traffic? Each has a different fix.

The idea: alive, and making progress

A member is one process doing two things, and the group checks each:

 member process
 ├── heartbeat thread ── "still here", every heartbeat.interval.ms ──►  coordinator
 │                        silent for session.timeout.ms      = dead
 └── your poll loop   ── poll(), process the batch, poll() ...
                          no poll() for max.poll.interval.ms = stuck

In the classic protocol the heartbeats come from a background thread (kafka-coordinator-heartbeat-thread in the client’s log), so a member busy in a long batch still heartbeats. Defaults: heartbeats every 3 s, session.timeout.ms=45000, max.poll.interval.ms=300000 (5 minutes), max.poll.records=500.

Dead: session.timeout.ms

Three members, a producer running, then one is killed with docker kill (three or four runs each):

The killed memberIts partitions went to the others after
classic, session.timeout.ms=100009.8 to 12.2 s
classic, the default 45 s43.3 to 43.8 s
classic, static (group.instance.id), session.timeout.ms=3000029.8 to 30.8 s
new protocol (group.protocol=consumer)40.7 to 44.4 s for one partition, 44.4 to 46.9 s for the other

Each is the session timeout, give or take a few seconds. The static member stays on the member list until its timeout runs out: 12 s after the kill, --describe --members still listed it with its 2 partitions, unread: the price of lesson 2’s rebalance-free restart.

The new protocol’s session timeout belongs to the broker, and the client refuses its own:

Exception in thread "main" org.apache.kafka.common.config.ConfigException: session.timeout.ms cannot be set when group.protocol=CONSUMER

A lower timeout moves a dead member’s partitions sooner, but a healthy member silent for longer than it (a long garbage-collection pause, a network blip) is thrown out too.

Stuck: max.poll.interval.ms

Member m1 processes each record in 30 ms, so a batch of 500 takes 15 s, with max.poll.interval.ms=10000. A second member, m2, joins:

03:46:07.028 m1 polled 500
...
03:46:17.062 [kafka-coordinator-heartbeat-thread | pi-2] WARN org.apache.kafka.clients.consumer.internals.ConsumerCoordinator - [Consumer clientId=m1, groupId=pi-2] consumer poll timeout has expired. ...
...
03:46:22.203 m1 stopped: org.apache.kafka.clients.consumer.CommitFailedException: Offset commit cannot be completed since the consumer is not part of an active group for auto partition assignment; it is likely that the consumer was kicked out of the group.

About 10 s after its poll (9.99 to 10.05 s, three runs), the client itself sent LeaveGroup; m2 got all six partitions 27 to 55 ms later:

03:46:17.102 m2 assigned [0, 1, 2, 3, 4, 5]  now [0, 1, 2, 3, 4, 5]

When m1 finished its batch, its commit failed: CommitFailedException means “you were thrown out for not polling”. Its 500 records were processed and never committed, so they will be processed again. A version that catches the exception and polls again rejoins within a second, and the group rebalances again. (A static member over max.poll.interval.ms keeps its partitions until the session timeout instead, consumer configs.)

The fix: smaller batches

The time between two polls is about max.poll.records × the time per record: 500 × 30 ms = 15 s above. Raise max.poll.interval.ms to more than the slowest batch, or lower max.poll.records.

Smaller batches also shorten eager rebalances, which wait for the slow member (lesson 2). At 10 ms per record a batch is 5 s at 500 and 0.5 s at 50; another member sat empty for:

Slow member’s max.poll.recordsEmpty time, 11 runs
50010 to 4,282 ms
50233 to 419 ms

It varies from run to run, and stays under one batch.

Reading lag

kafka-consumer-groups --describe shows, per partition, the bookmark (CURRENT-OFFSET), the end of the partition (LOG-END-OFFSET), their difference (LAG), and who owns it. A member that processes at most 100 records a second, with 200 a second produced:

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID                             HOST            CLIENT-ID
lag-3           orders          0          20313           20429           116             c1-59b927c6-70d9-41d0-93e4-74b31f805207 /172.21.0.5     c1
...
lag-3           orders          5          20310           20426           116             c1-59b927c6-70d9-41d0-93e4-74b31f805207 /172.21.0.5     c1
MembersTotal LAG, one runChange, three runs
1699, then 7,181 60 s later+96 to +108 a second
38,207, then 5,173 33 s later−80 to −118 a second
3, producer stopped2,895, then 0 within 15 sthe same in each run

Lag counts records, not time. That run’s highest, 8,849, is 44 s of production at 200 a second, and about 88 s to zero at 100 a second. Steady lag is a group keeping up; lag growing everywhere, a group too slow for its input.

With no members, --describe says the group “has no active members”, shows - as the owner, and still lists the bookmarks and the lag.

Group states

--describe --state shows the group’s state. These runs saw:

ProtocolStates seenMeaning
classicStable, PreparingRebalance, StablePreparingRebalance: waiting for all members to rejoin; then CompletingRebalance (too short to catch): waiting for the assignment
consumerStable, Reconciling, Stable, AssigningAssigning: a new target assignment is being computed; Reconciling: members are moving to it (about 5 s when w3 joined)
bothEmptyno members; the bookmarks are kept

Meanings: KIP-345 and KIP-848; the full list: GroupState.

A debugging checklist

What you seeLook atUsually
Lag growing on every partition, group Stablethe members’ processing ratetoo slow for the input: speed up processing, or add members (up to the partition count)
“consumer poll timeout has expired”, CommitFailedExceptiontime per batcha batch slower than max.poll.interval.ms: lower max.poll.records
State keeps going to PreparingRebalancemembers joining and leavingthe poll timeout above, or members restarting: static membership (lesson 2)
Partitions unread for about 45 s after a crashsession.timeout.msits default, 45 s
Records processed twice after a deploywhere you commitcommits missing in onPartitionsRevoked (lesson 3)

Try it on your computer

The capstone, next, has you tune a consumer that falls past max.poll.interval.ms, and read its lag while it catches up.

Go deeper: the new protocol's timeouts live on the broker

In a consumer group the broker sets the heartbeat interval and the session timeout for every member: group.consumer.heartbeat.interval.ms=5000 and group.consumer.session.timeout.ms=45000, within the bounds group.consumer.min.session.timeout.ms=45000 and group.consumer.max.session.timeout.ms=60000. So out of the box, a dead member’s partitions wait about 45 s (40.7 to 46.9 s above).

The slow member above, in a consumer group, got the poll timeout warning 14.7 to 14.9 s after each poll (three runs), its commit failed with “unknown member ID”, and it rejoined 0.1 s later, with partitions 0-2 in 7 of its 8 rejoins. It never committed: batch after batch, about 15 s apart.

One group can have its own values, set as group configs (consumer.session.timeout.ms, consumer.heartbeat.interval.ms, group configs) with kafka-configs --entity-type groups; this course doesn’t run that. max.poll.interval.ms and max.poll.records stay client settings in both protocols.

Common misunderstandings

  • “Zero lag means nothing is lost.” Lag compares the bookmark with the end of the partition. A member that commits before processing (lesson 3) shows zero lag over orders it never processed.

Check yourself

1. Each record takes 50 ms and max.poll.records is 500, with the other defaults. Safe? And when the database behind it slows to 1 s per record? At 50 ms a batch takes 25 s, well under the 5 minutes of max.poll.interval.ms. At 1 s per record it takes 500 s: the member is thrown out mid-batch, its commit fails, and the batch is processed again. Lower max.poll.records, or raise max.poll.interval.ms past the slowest batch you expect.
2. A group's lag is 6,000 and falls by about 100 a second. Is it healthy? It is catching up: about 60 s to zero if the rate holds. The worrying case is the opposite trend, lag that grows on every partition.
3. A static member's machine dies, with session.timeout.ms=30000. What happens to its partitions? Nothing for about 30 s: the member is still listed and its partitions are unread (29.8 to 30.8 s in the runs above). Then they go to the others.

Next: Practical 1 · Capstone: a consumer you can trust.