Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Consumer Lag
HLD

Consumer Lag

The single most important queue metric — measuring backlog, reading its trends, and acting before users notice.

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.
 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

SignalAlertMeaning
lag growth ratesustained climb 15mcapacity problem forming
absolute lag> SLO-derived boundstaleness budget breached
oldest message age> minutes-class SLAuser-visible soon
lag = 0 suddenlysuspicious!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.

My Private Notes

Notes are auto-saved locally to this device.