Apache Kafka Streaming (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Handling Consumer Lag: Detection, Diagnosing Root Causes, and Autoscaling Consumer Pods.
senior LevelMost AskedKubernetesSystem Design
In Simple Words:
Consumer Lag = Log End Offset (LEO) - Current Consumer Committed Offset.
It measures how many records are sitting in the broker unread.
Diagnostic Checklist:
- Uneven Lag across Partitions: Caused by a hot partition key (e.g. 80% of traffic using the same customer ID).
- Lag growing across all partitions: Consumer processing logic is too slow (slow database queries, external REST calls, CPU starvation).
- Autoscaling with KEDA (Kubernetes Event-driven Autoscaling):
- Deploy KEDA in Kubernetes with a
ScaledObjectmonitoring consumer group lag. - When total lag exceeds 5,000 records, KEDA automatically scales consumer pods from 2 to the maximum partition count (e.g. 10 pods for 10 partitions).
- Note: You can never scale consumer pods beyond the total number of partitions in the topic!
- Deploy KEDA in Kubernetes with a