Defining Lag
LAG = messages produced but not yet processed.
log model (kafka) makes it precise:
[log] e1 e2 e3 e4 e5 e6 e7 e8
▲ ▲
consumer offset latest produced
└──── lag = 4 ───┘
per consumer-group-per-partition:
total_lag = Σ(latest_offset − committed_offset)
queue model equivalent: QUEUE DEPTH (messages waiting).
Why Lag Is the Metric That Matters
it translates directly to USER-VISIBLE staleness:
lag 5000, rate 100/s → newest event processed in ~50s
→ "why is my dashboard 1 minute old?"
lag ∞-ish and growing → pipeline effectively DOWN even though
every component shows green ✓✓✓
CPU/memory can lie; lag is the TRUTH of throughput matching.
Reading Lag Trends
the SHAPE tells the story:
steady low: ▁▁▁▁▁▁▁ healthy ✓
spike + recovery: ▂▆▂▁▁▁ burst absorbed by buffer —
messaging doing its JOB ✓
climbing forever: ▁▃▅▇█▇█▇↗ capacity deficit ✗ ACT NOW
sawtooth rising: ▃▁▄▂▅▃▆▄↗ scaling can't keep pace ✗
also watch: TIME-LAG (age of oldest unprocessed message)
sometimes more honest than counts during rate swings:
100k lag at 50k/s = seconds stale. fine!
100k lag at 10/s = hours stale. fire!
The Lag Equation and Its Levers
d(lag)/dt = production_rate − consumption_rate
raise consumption_rate via:
□ MORE CONSUMERS: up to partition count ceiling
(20 partitions max out at 20 parallel readers!)
□ FASTER CONSUMERS: batch reads, parallelize WITHIN processing,
async IO to downstream, cut per-message overhead
□ REDUCE WORK: slim payloads, skip no-op messages
if production_rate is genuinely > max consumption_rate:
no tuning saves you → scale partitions/repartition,
or shed/backpressure upstream (own lesson).
Alerting Discipline
| Signal | Alert | Meaning |
|---|---|---|
| lag growth rate | sustained climb 15m | capacity problem forming |
| absolute lag | > SLO-derived bound | staleness budget breached |
| oldest message age | > minutes-class SLA | user-visible soon |
| lag = 0 suddenly | suspicious! | consumers dead? monitoring broken? |
lag=0 alerting sounds paranoid until the day consumers
silently die and dashboards show "perfect" empty queues.
Interview Framing
“Your pipeline falls behind during traffic spikes” scored answer: define lag precisely with offset diagram, read-the-trend triage (burst-recovery vs deficit), concrete levers in order (consumers→efficiency→partitions), time-lag as the staleness-honest metric, and lag-based autoscaling as the standard control loop. Knowing the partitions-cap-consumers ceiling is a frequent interviewer checkpoint.
Premium Content
Unlock Consumer Lag and all premium lessons with a subscription.
All premium lessons
Ad-free experience
Priority support
From ₹199.99/year — See plans