Consumer Lag Detection, Exporting, and Alerting
Monitor Kafka consumer lag, configure Prometheus exporters, design actionable burn-rate alerts, and automate Kubernetes autoscaling with KEDA.
In distributed event streaming, consumer lag is the single most critical health indicator of an event-driven system. It measures the delta between the latest message produced to a partition and the latest offset committed by a consumer group.
A growing lag indicates that consumers are falling behind real-time ingestion. If left unchecked, excessive lag leads to delayed business processes, stale analytical models, and potential data loss if unconsumed messages age past the topic retention horizon (log.retention.hours).
Operating production Kafka clusters requires real-time lag detection, robust Prometheus metrics exporting, actionable alerting, and automated pod autoscaling.
01The mathematics of consumer lag
For any given topic partition, Kafka tracks two offset positions:
- Log-End-Offset (LEO): The sequential offset assigned to the next incoming message written to the broker partition log.
- Current Committed Offset: The latest offset confirmed as processed by the consumer group, stored in the internal
__consumer_offsetstopic.
Consumer lag for a partition is calculated as:
Consumer Lag = LogEndOffset - CurrentCommittedOffsetFor example, if the active partition leader has written up to offset 10,000, and consumer group order-processors has committed offset 7,500, the consumer lag is 2,500 messages.
Message count lag versus time lag
Measuring lag purely by message count can be misleading:
- A lag of 10,000 messages on a high-velocity clickstream topic ingesting 50,000 messages/sec represents only 200 milliseconds of latency (healthy).
- A lag of 100 messages on a heavyweight image-processing topic ingesting 1 message/sec represents 100 seconds of delay (critical incident).
Modern monitoring architectures evaluate Time-to-Lag (lag duration in seconds). Time lag is calculated by comparing the timestamp of the unconsumed record at CurrentCommittedOffset + 1 against the current wall-clock time.
Loading the rest of the lesson…