Kafka lag and consumer failures
On a real broker: lag growing on every partition, a consumer with zero partitions, a poll interval that failed every commit, and a poison pill stuck at offset 100.
Consumer lag is the number of messages that have been written to a partition and not yet processed by a consumer group. It is the single most useful number for a Kafka-based system, because it measures the thing users eventually feel — how far behind reality the system is — and because its shape usually tells you which of a handful of failures you are looking at.
This lesson runs those failures on a real Kafka broker (a single-node apache/kafka container) with a three-partition topic, and reads each one with kafka-consumer-groups.sh, the tool that ships with Kafka.
Reading lag
For every partition a group consumes, the broker knows two offsets: the log-end offset, where the next message will be written, and the group's committed offset, where it will resume. Lag is the difference. kafka-consumer-groups.sh --describe prints all three.
Three shapes cover most incidents:
| lag shape | usually means |
|---|---|
| rising steadily on every partition | the group is slower than the producers — capacity |
| rising on one partition, zero elsewhere | that partition is stuck — a poison message, or a hot key |
| a sawtooth that resets and climbs again, with duplicate processing | the group keeps rebalancing and redoing work |
And one non-lag symptom: a consumer that is running, healthy, and doing nothing.
The consumer that is simply too slow
A producer writes 300 messages a second. The consumer group invoice-writer spends 10 ms on each message — a database write, say — so it can handle about 100 a second. Two snapshots, fifteen seconds apart:
--- t=10s
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
orders 0 4225 4951 726
orders 1 2037 2621 584
orders 2 3238 4437 1199
--- t=25s
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
orders 0 4824 6396 1572
orders 1 2479 4364 1885
orders 2 3604 6249 2645Total lag went from 2,509 to 6,102 in fifteen seconds. The committed offsets advanced by 1,407 in that time — about 94 messages a second processed, against 300 arriving. Lag grows on every partition, at roughly the difference between the two rates, and it will keep growing until one of them changes.
Two numbers make lag actionable:
- Lag in time, not messages. 6,000 messages is nothing for a topic doing 50,000 a second and hours for one doing two. Divide by the consumption rate, or alert on the age of the oldest unprocessed message, as the backpressure lesson recommends for any queue.
- Is it still growing? A burst that the group is draining is fine. Lag that grows through a quiet period is a capacity problem that will not fix itself.
Adding consumers, and the one that does nothing
The obvious fix for a slow group is more consumers. Here is a group with four consumers on a topic with three partitions:
GROUP CONSUMER-ID HOST CLIENT-ID #PARTITIONS
email-sender email-sender-4-c086117c-c421-43d2-9aab-c5c3b716fdee /127.0.0.1 email-sender-4 0
email-sender email-sender-2-8c59d571-bf33-4197-aa41-e8d2d0ed8194 /127.0.0.1 email-sender-2 1
email-sender email-sender-1-e604420e-65f0-4656-a22e-cff98ec5e843 /127.0.0.1 email-sender-1 1
email-sender email-sender-3-006834b8-9a21-4c34-98d8-4a7344e9fbed /127.0.0.1 email-sender-3 1email-sender-4 has zero partitions. Within a consumer group, each partition is assigned to exactly one consumer, so the partition count is the ceiling on parallelism. The fourth instance is running, connected, passing its health checks, and consuming nothing — while the dashboard counts it as capacity.
When the group is genuinely too slow:
- Make each message cheaper first: batch the database writes, remove a synchronous call, fix the slow query behind the handler.
- Process in parallel within a consumer, with care for ordering — per-key ordering can be kept by routing each key to the same worker.
- Increase the partition count so more consumers can share the work — knowing that it cannot be reduced later, and that it changes which partition a key maps to, so ordering per key is only guaranteed for messages produced after the change.
The rebalance loop
A consumer must call poll() at least every max.poll.interval.ms. If it does not, the group coordinator assumes it has died, removes it from the group, and gives its partitions to someone else. Here the interval is set to 5 seconds, each poll returns up to 500 records, and each record takes 20 ms — ten seconds per batch:
round 1: 500 records in 11.2 s, commit FAILED: 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.
round 2: 500 records in 11.1 s, commit FAILED: 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.
round 3: 500 records in 11.1 s, commit FAILED: 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.
records processed more than once: 500Every batch was processed and no commit ever succeeded. Each time the consumer rejoined, it started again from the last committed offset — the same records — so it did the same work forever, and 500 records were processed more than once. From outside, the consumer looks busy, CPU is high, lag does not move, and every downstream effect of those records happens repeatedly.
The fixes are all about keeping each poll's work inside the interval:
- Lower
max.poll.recordsso a batch fits comfortably inside the interval. - Raise
max.poll.interval.msif long processing is genuinely expected — which also slows the detection of consumers that really have died. - Move slow work off the poll thread, pausing the partitions while it runs, if a batch cannot be made fast.
And handlers must be idempotent, because a rebalance at the wrong moment reprocesses records even when everything is configured well. The idempotency lesson in the distributed systems course covers that.
The poison pill
One message cannot be processed — here, an order whose quantity is the string "two". The handler throws, and the consumer does what many hand-written loops do: seek back to the same offset and try again. The group's partitions afterwards:
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG
stock-events 0 100 1000 900
stock-events 1 1000 1000 0
stock-events 2 1000 1000 0partition 0 offset 100: For input string: "two" — retrying
failed attempts on the same record: 30291Partitions 1 and 2 are fully processed. Partition 0 has been stuck at offset 100 since the bad record arrived, with 900 good messages behind it, and the consumer tried the same record 30,291 times in 15 seconds — a hot loop that also hammers whatever the handler calls before it fails.
That is the one-partition shape from the table, and it is diagnostic: find the committed offset on the stuck partition, and read that single record with the console consumer (--partition 0 --offset 100 --max-messages 1) to see what it contains.
The fix is to decide, in code, what happens to a message that fails for a reason retrying will not change:
- Retry a limited number of times with backoff, for failures that may be transient.
- Then send it to a dead-letter topic with the error attached, commit past it, and alert. Spring Kafka's
DefaultErrorHandlerwith aDeadLetterPublishingRecovererdoes exactly this, and it treats deserialization and conversion failures as not retryable by default. - Never retry forever on the same record. A partition stuck behind one message is an outage for every key on that partition.
The retries and dead-letter topics lesson in the Kafka course covers the Spring configuration.
A checklist for a lag alert
- Which partitions? All of them — capacity or a slow dependency. One — a poison pill or a hot key.
- Are committed offsets moving? Slowly — too slow. Not at all with the consumer busy — rebalance loop or retry loop. Not at all with the consumer idle — it is not assigned, or not running.
- Group state and membership.
--describe --membersfor consumers with zero partitions; frequent rebalances in the consumer logs. - What changed? A deploy of the consumer, a slower downstream dependency, a change in the producer's message format.
Under the hood: the rebalance protocol, and what a committed offset actually is
The rebalance loop in this lesson is caused by a poll() that arrives too late, but understanding why that removes the consumer means looking at the protocol underneath. Every consumer group has a group coordinator — one of the brokers, chosen deterministically per group — that tracks membership and drives rebalances through a small state machine of its own. When a consumer joins (or a rebalance is triggered by a new member, a departing one, or a missed heartbeat/poll deadline), the coordinator runs two phases: JoinGroup, where every member sends its subscription and the coordinator picks one member as the group leader and hands it the full member list; and SyncGroup, where the leader computes the partition assignment (using whichever partition.assignment.strategy is configured) and sends it back through the coordinator to every member, including itself. Only after SyncGroup completes does each consumer know which partitions it owns.
Historically this whole protocol is an eager rebalance: every member gives up all its partitions at the start of a rebalance and gets a (possibly different) assignment back at the end, meaning there is a brief window where no partition in the group is being consumed at all — a global stop-the-world pause. Kafka's cooperative sticky assignor (KIP-429) changed this: a rebalance only reassigns the specific partitions that actually need to move (say, away from a consumer that just left), and every other consumer keeps consuming its existing partitions uninterrupted through the rebalance. That distinction matters directly for the lesson's rebalance-loop scenario: with the old eager protocol, one consumer being repeatedly kicked out for missing max.poll.interval.ms stalls the entire group's consumption during each rebalance; with cooperative rebalancing, the disruption is scoped closer to just the partitions the struggling consumer was assigned.
What a "committed offset" actually is is worth being precise about too: it is not metadata bolted onto the topic — it is itself a message, produced by the consumer to Kafka's own internal __consumer_offsets topic, keyed by (group, topic, partition), with the offset as the value. That is why offset commits have the same durability and ordering guarantees as any other Kafka write, and why "the commit failed" in this lesson's rebalance-loop trace is a genuine write failure to that internal topic (rejected because the consumer's group membership had already expired), not a client-side bookkeeping error — which is exactly why every one of those failed commits left the group's true position on orders unchanged, forcing the next assignment to resume from the same already-processed offset.
Try it yourself
Trace what a stuck JoinGroup/SyncGroup round means for lag
A four-consumer group is rebalancing every 20 seconds because one consumer keeps missing its poll deadline, gets evicted, then rejoins, triggering another rebalance. Using the eager rebalance protocol (not cooperative), during each rebalance's JoinGroup/SyncGroup round, none of the four consumers are actively consuming — say this pause is 2 seconds each time.
Over a 10-minute window, roughly how much consuming time is lost to rebalance pauses alone, separate from any lag caused by reprocessing? What does switching to cooperative rebalancing change about this specific number?
Working through the lost time
10 minutes is 600 seconds; a rebalance every 20 seconds means roughly 30 rebalances in that window, each pausing all four consumers for about 2 seconds under the eager protocol — 60 seconds of the group doing zero consuming across 600 seconds, or 10% of the window with all four partitions' worth of throughput lost, on top of whatever reprocessing the evicted consumer's failed commits caused. Switching to cooperative rebalancing does not fix the underlying problem (the consumer is still missing its poll deadline and still gets evicted and rejoins), so the eviction/rejoin cycle and its reprocessing still happen — but the 2-second global pause shrinks to affecting only the partitions actually being reassigned (the evicted consumer's own partitions), while the other three consumers keep consuming uninterrupted through each rebalance. The fix for the reprocessing is still lowering max.poll.records or moving slow work off the poll thread, as the lesson says; cooperative rebalancing only bounds the collateral damage to the rest of the group while that root cause is fixed.
Misconceptions
- "Adding partitions is a safe way to add more consumer capacity at any time." It increases the ceiling on parallelism, but Kafka's default partitioner maps a key to a partition using a hash of the key modulo the partition count — changing the partition count changes that mapping for every key, so messages for the same key produced before and after the change can land on different partitions, breaking the per-key ordering guarantee the lesson notes consumers often rely on. It is safe to add partitions when ordering does not matter or when the change is planned and communicated; it is not something to do casually mid-incident to relieve lag.
- "A consumer group's lag is purely a Kafka-side number, independent of what the consumer does with each record." Lag only measures offsets — how far behind the log the group's position is. A consumer happily advancing its offset while silently failing to actually process each record correctly (writing to a dead downstream database, say) shows zero lag and is still fully broken; lag tells you about throughput, not correctness.
Sources
- Apache Kafka documentation, "Consumer Configs" (
max.poll.interval.ms,max.poll.records,partition.assignment.strategy). - KIP-429, "Kafka Consumer Incremental Rebalance Protocol" (cooperative sticky assignment).
- Neha Narkhede, Gwen Shapira and Todd Palino, Kafka: The Definitive Guide — consumer group coordination and offset management.