Lesson 05 / 10 · Performance

Lag Management and Performance Monitoring

Kafka consumer lag: what it measures, how to monitor it with Prometheus and Grafana, alert thresholds, and the fixes that bring lag back down.

2-3 hours advanced Not completed

On this page · 12 sections
  1. What is Consumer Lag?
  2. The 7 Common Lag Patterns
  3. Measuring Lag: The Right Way
  4. Setting Up Prometheus + Grafana
  5. Alerting Strategies
  6. Lag-Based Autoscaling with KEDA
  7. End-to-End Latency Measurement
  8. Debugging Slow Consumers
  9. Production Monitoring Checklist
  10. Hands-on exercise: create lag, read it, fix it
  11. Key Takeaways
  12. Quiz

What is Consumer Lag?

Consumer lag = How far behind your consumer is from the latest message in a partition.

Partition 0: [msg-0] [msg-1] [msg-2] [msg-3] [msg-4] [msg-5] [msg-6]
                            ↑                    ↑
                      consumer offset        latest offset
                            (3)                  (6)
                           
                    Lag = 6 - 3 = 3 messages

Why lag matters:

  • High lag = Slow processing = Data staleness
  • Zero lag = Real-time processing = Good
  • Negative lag = Consumer ahead of producer = Impossible (indicates bug)

The 7 Common Lag Patterns

Here are the patterns I see in production and how to fix them:

1. The Steady Climb

Lag: 0 → 100 → 500 → 1000 → 5000 → 10000...
Cause: Consumer can't keep up with producer rate
Fix: Scale consumers or optimize processing

2. The Spiky Pattern

Lag: 0 → 0 → 0 → 5000 → 0 → 0 → 0...
Cause: Bursty traffic or slow processing spikes
Fix: Increase batch size, add buffering

3. The Staircase

Lag: 0 → 1000 → 1000 → 1000 → 2000 → 2000...
Cause: Consumer stuck on specific messages
Fix: Dead letter queue, skip problematic messages

4. The Roller Coaster

Lag: 0 → 5000 → 0 → 8000 → 0 → 12000...
Cause: Rebalancing storms
Fix: Static membership, cooperative rebalancing

5. The Plateau

Lag: 0 → 10000 → 10000 → 10000 → 10000...
Cause: Consumer crashed, not processing
Fix: Health checks, auto-restart

6. The Sawtooth

Lag: 0 → 1000 → 0 → 1000 → 0 → 1000...
Cause: Normal batch processing
Fix: Nothing - this is healthy

7. The Explosion

Lag: 0 → 0 → 0 → 1000000 → 2000000...
Cause: Consumer completely stopped
Fix: Emergency scaling, circuit breakers

Measuring Lag: The Right Way

There are multiple ways to measure lag, each with trade-offs:

1. JMX Metrics (Most Common)

# Per partition
kafka.consumer:type=consumer-fetch-manager-metrics,client-id=*,topic=*,partition=*
records-lag-max
records-lag-avg

# Per consumer group
kafka.consumer:type=consumer-coordinator-metrics,client-id=*
rebalance-rate-per-hour

2. Kafka Admin API

from kafka.admin import KafkaAdminClient
from kafka.structs import OffsetSpec

admin = KafkaAdminClient(bootstrap_servers='localhost:9092')

# Get latest offsets
latest_offsets = admin.list_consumer_group_offsets('my-group')

# Get consumer offsets  
consumer_offsets = admin.list_consumer_group_offsets('my-group')

# Calculate lag
for partition, offset in consumer_offsets.items():
lag = latest_offsets[partition] - offset
print(f"Partition {partition}: {lag} messages behind")

3. Burrow (LinkedIn’s Tool)

# Install Burrow
go get github.com/linkedin/Burrow

# Start Burrow
./burrow --config-dir=./config

# Check lag via HTTP API
curl http://localhost:8000/v3/kafka/local/consumer/my-group/lag

Setting Up Prometheus + Grafana

The gold standard for Kafka monitoring:

1. JMX Exporter

# jmx_prometheus_javaagent.jar
java -javaagent:jmx_prometheus_javaagent.jar=8080:config.yaml \
-jar kafka-server-start.sh config/server.properties

2. Prometheus Config

# prometheus.yml
scrape_configs:
- job_name: 'kafka-brokers'
static_configs:
- targets: ['broker1:8080', 'broker2:8080', 'broker3:8080']

- job_name: 'kafka-consumers'
static_configs:
- targets: ['consumer1:8080', 'consumer2:8080']

3. Grafana Dashboard

# Key panels to include:
- Consumer lag by group and partition
- Consumer throughput (messages/sec)
- Broker metrics (CPU, memory, disk)
- Rebalance frequency
- Error rates

Alerting Strategies

Don’t just monitor - alert on the right things:

Critical Alerts

  • Lag > 10,000 messages - Consumer falling behind

  • Zero consumer throughput - Consumer stopped

  • Rebalance frequency > 5/hour - Instability

  • Consumer error rate > 1% - Processing issues

Warning Alerts

  • Lag > 1,000 messages - Getting behind

  • Consumer throughput 80% - Resource pressure

Lag-Based Autoscaling with KEDA

Automatically scale consumers based on lag:

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: kafka-consumer-scaler
spec:
scaleTargetRef:
name: kafka-consumer
minReplicaCount: 1
maxReplicaCount: 10
triggers:
- type: kafka
metadata:
bootstrapServers: kafka:9092
consumerGroup: my-group
topic: my-topic
lagThreshold: '1000'

End-to-End Latency Measurement

Lag tells you how behind you are, but not how long messages take to process:

# Add timestamps to your messages
message = {
'data': payload,
'produced_at': time.time(),
'message_id': uuid.uuid4()
}

# In consumer
def process_message(message):
start_time = time.time()
produced_at = message['produced_at']

# Process message
result = do_work(message['data'])

# Calculate latencies
end_to_end_latency = start_time - produced_at
processing_latency = time.time() - start_time

# Log metrics
logger.info(f"E2E: {end_to_end_latency:.2f}s, Processing: {processing_latency:.2f}s")

Debugging Slow Consumers

When lag is high, here’s your debugging checklist:

1. Check Consumer Metrics

  • Are consumers actually running?

  • Are they polling frequently enough?

  • Are they committing offsets?

2. Check Processing Logic

  • Is processing taking too long?

  • Are there database bottlenecks?

  • Are there external API calls?

3. Check Resource Usage

  • CPU usage high?

  • Memory usage high?

  • Network I/O saturated?

4. Check Partition Distribution

  • Are all partitions being consumed?

  • Is one partition hot?

  • Are consumers evenly distributed?

Production Monitoring Checklist

Must Have

  • Consumer lag monitoring (per partition)

  • Consumer throughput tracking

  • Error rate monitoring

  • Rebalance frequency alerts

  • End-to-end latency tracking

Nice to Have

  • Consumer group health dashboard

  • Partition distribution visualization

  • Historical lag trends

  • Automated scaling based on lag

  • Dead letter queue monitoring

Hands-on exercise: create lag, read it, fix it

Goal: produce faster than a group consumes, read the lag per partition and decide what to do about it.

# 100,000 messages into the 3-partition "events" topic
bin/kafka-producer-perf-test.sh --topic events --num-records 100000 --record-size 100 --throughput -1 \
  --producer-props bootstrap.servers=localhost:9092

# A "slow" consumer that stops after 30,000 messages
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic events --group slow \
  --from-beginning --max-messages 30000 > /dev/null

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group slow
bin/kafka-get-offsets.sh --bootstrap-server localhost:9092 --topic events

Questions:

  1. What is the total lag, and is it spread evenly across partitions?
  2. If this group processes 500 messages/sec and producers write 400/sec, how long until it catches up? What if producers write 600/sec?
  3. Write an alert rule (in words or PromQL) that fires on a real problem but not on a short spike.
Answers
  1. You will see something like this (exact numbers vary):
GROUP  TOPIC   PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG
slow   events  0          11018           29462           18444
slow   events  1          9475            35740           26265
slow   events  2          9507            34798           25291

Total lag is the sum, about 70,000. It is uneven because the perf-test producer spreads batches across partitions unevenly. A partition with much more lag than the rest usually means a hot key or a slow consumer instance. 2. The group gains 500 − 400 = 100 messages/sec, so 70,000 / 100 ≈ 700 s, about 12 minutes. At 600/sec the lag grows by 100/sec forever: add consumers (up to the partition count), make processing faster, or add partitions. 3. Alert on lag that is high and not shrinking, for example: lag above 10,000 for 10 minutes and the lag’s rate of change is ≥ 0. With the kafka-exporter metric: sum by (consumergroup) (kafka_consumergroup_lag) > 10000 and deriv(sum by (consumergroup) (kafka_consumergroup_lag)[10m:]) >= 0, with for: 10m. Better still, alert on time lag (how old the oldest unprocessed message is) when you have it, because 10,000 messages means very different things on different topics.

Key Takeaways

  1. Monitor lag patterns, not just numbers - patterns tell the real story
  2. Set up proper alerting - don’t just monitor, alert on what matters
  3. Use Prometheus + Grafana: most Kafka exporters and dashboards assume them
  4. Implement autoscaling - let the system scale itself
  5. Measure end-to-end latency - lag is just one part of the picture

Quiz

  1. What is consumer lag for a partition?
  2. Lag goes 0 → 1000 → 0 → 1000 in a regular rhythm. Is that a problem?
  3. Lag is flat at 10,000 and not moving. What’s the likely cause?
Show answers
  1. The difference between the latest offset in the partition (log end offset) and the consumer group’s committed offset.
  2. Usually not. That sawtooth pattern is normal batch processing.
  3. The consumer has stopped processing (crashed or stuck), the “plateau” pattern; add health checks and auto-restart.

Done with this lesson?

Marking it complete updates the sidebar, the course page and the Continue button.