Video in progress
Kafka consumer lag has been growing for hours. What do you check and how do you fix it?
First I establish the cause: did the events increase, or did processing slow down. Then I look at processing time, rebalances, the number of partitions and consumers, and external calls. The usual fixes are fixing the slow spot, batching, and adding partitions and consumers.
📖 Answer
I look in this order.
-
Where is the lag?
kafka-consumer-groups.sh --describe --group billing— lag per partition. Is it growing evenly across all of them or on one partition? If it's one partition, the key distribution is uneven and a single key accounts for half of the stream. -
Incoming rate and processing speed. Metrics: messages per second on the producer side, and the time to process one message on the consumer side. It was 5 ms yesterday and 80 ms today — so something inside the consumer got slow: a DB query, an external API, a lock.
-
Rebalances. If you often see "Revoking partitions" in the log, the consumer isn't getting to its next
pollwithinmax.poll.interval.ms. The group takes the partitions away from it, gives them to another one, the work starts over — and the lag grows even more. The cause is often a largemax.poll.recordswith each message slow. -
The parallelism limit. If there are more consumers than partitions, the extras sit idle. 12 pods won't speed up a topic with 6 partitions.
Fix options:
- fix the slow part: N+1 queries, a missing index, a separate HTTP call per message;
- batching: write the 500 messages you polled with a single
INSERT ... VALUES (...), (...); - increase the number of partitions and add consumers. Note: when partitions are added, the key → partition mapping changes, and for new messages the order within a single key is temporarily broken;
- parallel processing by key within a partition (for example, the Confluent parallel consumer library);
- if a poison message has stopped the whole partition — send it to the DLT.
Watch lag in time too, not only in message count: whether "5,000 messages behind" is a lot or a little depends on the stream rate.
Sign up to mark studied questions and save your progress.