Skip to content

Lesson 7 of 9 · 27m

Operating Kafka: Lag, Poison Messages, and Recovery Drills

Build an operational playbook for lag spikes, stuck consumers, dead-letter handling, broker degradation, and rehearsed recovery rather than dashboard theater.

Kafka rarely fails all at once. It degrades in ways that look like application slowness: one hot partition, one follower out of ISR, one consumer poisoned by a malformed record, one disk nearing full. The teams that recover quickly are not the teams with the most dashboards. They are the teams that rehearsed boring runbooks before the incident and wrote down which knob is safe to turn.

Start with the narrowest failing boundary

When someone says "Kafka is down," establish which layer is actually unhealthy:

kafka-topics.sh --bootstrap-server kafka-1:9092 --describe --under-replicated-partitions

kafka-consumer-groups.sh --bootstrap-server kafka-1:9092 --group invoice-projector --describe

Possible answers differ radically: broker unavailable, partition leader missing, consumer group empty, lag growing, processing stalled despite low lag, or downstream database saturated. Restarting everything is not diagnosis; it is roulette with extra downtime.

Lag triage is arithmetic plus context

Compare produce rate and consume rate per partition. A consumer group processing 5,000 records per second cannot drain a topic receiving 8,000 records per second. But lag that jumps vertically after a deploy usually signals rebalance, deserialization failure, or downstream timeout rather than organic capacity loss.

Useful application metrics include:

records/sec by partition; processing time p50/p95/p99; commit latency/failures
deserialization exceptions; downstream latency; retry and dead-letter counts
rebalance count and time since last rebalance

Offset lag alone is not enough. A million tiny telemetry records are not equivalent to a million large enrichment jobs.

Poison messages need an explicit exit path

A typical failure loop looks like this:

poll 88120 -> deserialize throws -> no commit -> partition stalls -> lag grows

The fix is not "catch and continue" unless you have defined where bad records go. Write them to a dead-letter topic with metadata:

{"source_topic":"payments.authorized","source_partition":7,"source_offset":88120,"error_class":"MismatchedInputException","payload_base64":"eyJhbW91bnQiOi..."}

Preserve raw bytes when legal and practical. A transformed or truncated copy makes postmortem repair much harder. Alert on DLT growth; the dead-letter topic is a control, not a landfill.

Recovery drills reveal hidden coupling

Practice these before production needs them:

1. Reset a test group and rebuild a projection. 2. Stop one broker and watch ISR.
3. Repair and replay dead letters. 4. Roll consumers while watching duplicates.
5. Restore a downstream snapshot and reconcile with retained events.

You are testing application behavior as much as broker behavior. The first real incident is a terrible time to learn that a projector cannot tolerate replay.

Capacity failures often show up as correctness failures

Slow consumers create larger batches, more memory pressure, more downstream concurrency, longer processing time, and eventually rebalances. That feedback loop looks like duplication and flapping. Backpressure is better than blind parallelism: bound in-flight work and fail visibly instead of unbounded queue growth inside the consumer.

Field notes

  • Never delete consumer group offsets as a first resort; you erase operational truth and may trigger duplicate processing after restart.
  • --reset-offsets --to-datetime is useful but timestamp granularity and retention boundaries matter. Verify dry-run output first.
  • Brokers under disk pressure can cause cascading leader movement and replication churn. Set disk alerts before the filesystem is full.
  • A poisoned key can affect every record behind it in the same partition even when other partitions remain healthy.

Was this lesson helpful?

Sign in to rate