Video in progress
Consumer lag keeps growing. How do you investigate, and what do you do?
First I look at which partitions the lag is growing on: all of them or just one. If it's all of them, the consumer is slow or there aren't enough of them; if it's one, it's a hot key or a stuck consumer.
📖 Answer
The first step is the numbers:
kafka-consumer-groups.sh --bootstrap-server kafka:9092 \
--describe --group payment-service
For each partition it prints CURRENT-OFFSET, LOG-END-OFFSET, LAG and which consumer it's assigned to.
Then it depends on which picture you get:
Lag growing evenly across all partitions. The incoming flow increased or processing slowed down. Look at the consumer metric records-consumed-rate and the per-record processing time. The cause is often an external call: DB, HTTP. Adding more consumers only helps if there are enough partitions: put 10 consumers on 6 partitions and 4 of them sit idle.
Large lag on one partition. Either many keys land on that partition (skew), or the consumer holding it is stuck: take a thread dump. Or a single "poison" message is going around in retries.
Lag jumping up like a staircase. That's a sign of rebalances: look for Revoking partitions in the log.
Quick measures: tune max.poll.records and the batch, batch the DB writes, reduce synchronous calls. It's also useful to track lag in time, not only in message count (through Burrow or an exporter): "we're 5 minutes behind" makes more sense to the business.
Sign up to mark studied questions and save your progress.