Monitoring & Consumer Lag
The idea in one sentence
Section titled “The idea in one sentence”Kafka exposes hundreds of metrics through JMX, but the single most important application-level signal is consumer lag — the gap between a partition’s log-end-offset and the consumer group’s committed offset, which tells you whether your consumers are keeping up.
Consumer lag: the one number to watch
Section titled “Consumer lag: the one number to watch”A producer keeps appending records, advancing each partition’s log-end-offset (the position of the newest record). A consumer group commits how far it has processed. Lag is the difference:
lag = log-end-offset − committed offset
Lag near zero means consumers are keeping pace. Lag that grows steadily means consumers are falling behind — records are arriving faster than they are processed, and end-to-end latency is climbing. You read it directly with the consumer-groups tool:
# Describe a consumer group: shows CURRENT-OFFSET, LOG-END-OFFSET, and LAG per partitionkafka-consumer-groups.sh --bootstrap-server localhost:9092 \ --describe --group order-processorThe output has a LAG column per partition. Watch its trend, not one reading: flat or falling is healthy; steadily rising is the earliest warning that a consumer is overloaded, stuck, or crashed.
flowchart LR prod["Producer appends records"] --> leo["Log-end-offset (newest)"] cons["Consumer group commits"] --> cur["Committed offset (processed)"] leo -->|"difference"| lag["LAG = log-end-offset - committed offset"] cur --> lag
Broker-side metrics via JMX
Section titled “Broker-side metrics via JMX”Kafka brokers publish metrics over JMX, which tools like Prometheus (via the JMX exporter), Grafana, or your APM scrape. A few are non-negotiable:
- Under-replicated partitions (
UnderReplicatedPartitions) — partitions whose ISR is smaller than the replication factor. Should be zero; anything above means a replica is lagging or a broker is down and durability is at risk. - Offline partitions (
OfflinePartitionsCount) — partitions with no leader, so they are unavailable. Should always be zero. - Active controller (
ActiveControllerCount) — exactly one across the cluster. Zero or more than one signals a controller problem. - Request latency — produce and fetch request times (
TotalTimeMsbroken into queue, local, and remote phases). Rising latency points at disk, network, or replication pressure before clients feel it.
# Enable JMX on a broker by exporting the port before start (example)# then a JMX exporter or your APM scrapes these MBeansJMX_PORT=9999What to alert on
Section titled “What to alert on”Turn the key metrics into alerts, not just dashboards:
- Consumer lag rising past a threshold (or beyond an SLA in seconds) for any critical group.
- Under-replicated partitions greater than zero for more than a brief moment.
- Offline partitions greater than zero — page immediately.
- Request latency p99 crossing your budget for produce or fetch.