Glossary · C

Consumer Lag

Distance between the newest message written to a topic or queue and the last message a consumer has processed, measured in messages or in time.

By Obseria TeamUpdated

Definition

Consumer lag is the distance between the newest message written to a partition, topic or queue and the position up to which a consumer or consumer group has processed it. In Apache Kafka it is calculated per partition as the log end offset minus the committed offset of the consumer group. Lag can also be expressed as time, meaning how old the oldest unprocessed message is, which shows directly how delayed downstream data is.

What is consumer lag?

Consumer lag describes how far a consumer trails behind the producers of a message stream. Producers append messages to a topic or queue, consumers read and process them. As long as consumers keep up, the gap stays small and roughly constant. If producers write faster than consumers process, the gap grows, and every message waits longer before it is handled.

In Apache Kafka each partition is an ordered log with numbered offsets. The broker knows the log end offset, the position of the next message to be written, and each consumer group commits the offset up to which it has processed records. Lag per partition is the difference between the two, and the lag of a consumer group is usually reported as the sum or the maximum across its partitions. Other systems use different names for the same idea: Pulsar speaks of the backlog of a subscription, while RabbitMQ and SQS expose the number of waiting messages, which is closer to queue depth.

Offset lag vs. time lag

Lag can be measured in two ways, and both are useful:

  • Offset lag counts the messages that have not been processed yet. It is cheap to compute and available everywhere, but hard to interpret on its own: 50,000 messages of lag can mean a few seconds on a busy topic and several hours on a quiet one.
  • Time lag expresses how old the oldest unprocessed message is, or how long the consumer would need to catch up at its current rate. It answers the question users actually care about: how stale is the data downstream? Time lag can be derived from message timestamps or estimated from offset lag divided by the consumption rate.

Managed services often provide a time based metric directly, for example ApproximateAgeOfOldestMessage for Amazon SQS or IteratorAgeMilliseconds for Kinesis Data Streams.

Common causes of growing lag

  • Traffic spikes: producers temporarily write more than the consumer group can process. Lag rises and falls again afterwards, which is normal as long as it recovers.
  • Slow processing: a slow database, an external API with high latency or backpressure from a downstream system reduces the throughput of each consumer.
  • Too few consumers or partitions: in Kafka a partition is read by at most one consumer per group, so additional consumers beyond the partition count stay idle.
  • Rebalancing: when consumers join, leave or miss their heartbeat, the group redistributes partitions. During a rebalance affected partitions are not consumed, and frequent rebalances caused by crashes or long processing times exceeding max.poll.interval.ms lead to recurring lag.
  • Hot partitions: an uneven key distribution sends most messages to a few partitions, so lag grows on those while the group total looks harmless.
  • Poison messages: a record that fails again and again blocks its partition. A dead letter queue lets the consumer move on.

How to measure consumer lag

For a quick look, Kafka ships the command line tool kafka-consumer-groups.sh:

kafka-consumer-groups.sh --bootstrap-server broker:9092 \
  --describe --group order-service

It lists each partition with CURRENT-OFFSET, LOG-END-OFFSET and LAG. For continuous monitoring there are two sources. The Java consumer client exposes records-lag-max and per partition records-lag in its fetch manager metrics, seen from the consumer's own perspective. An external view based on committed offsets comes from exporters or from the Kafka metrics receiver of the OpenTelemetry Collector, which reports metrics such as kafka.consumer_group.lag. The external view also works when a consumer has crashed and stopped reporting anything, which is exactly when lag matters most.

Alerting on consumer lag

A fixed threshold on offset lag rarely works well, because normal lag differs between topics and over the day. More robust approaches are:

  • Alert on time lag against a freshness target, for example (as an illustration) "orders must be processed within five minutes".
  • Alert on the trend: lag that keeps rising for a longer period means consumers are permanently slower than producers, while short peaks that recover are expected.
  • Look at the maximum per partition, not only the group sum, to catch hot or stuck partitions.
  • Combine lag with consumer health: committed offsets that no longer move point to a stalled consumer rather than a load problem.

Lag and autoscaling

Because lag reflects unprocessed work, it is a better scaling signal for consumers than CPU usage. Autoscaling tools such as KEDA offer a Kafka scaler that adds consumer pods when lag per partition exceeds a configured value and removes them when the backlog is gone. Two limits apply: scaling beyond the number of partitions brings no extra parallelism, and every scaling step triggers a rebalance that briefly pauses consumption. A sensible cool down period avoids scaling back and forth.

More background with concrete metrics is in our article on Kafka monitoring and consumer lag metrics.

How Obseria helps

Obseria collects consumer lag, broker and client metrics through OpenTelemetry and shows them per topic, partition and consumer group in queue monitoring. The Kafka integration connects lag with the traces of producing and consuming services, so you can see whether a slow database call or a rebalance caused the backlog. Alerts can be defined on time lag, trends or per partition maxima, and Obseria Intelligence helps correlate rising lag with deployments and infrastructure changes.

Frequently asked questions

There is no universal value. A small, stable lag that recovers quickly after peaks is normal. What matters is whether the lag stays within your freshness requirements and whether it keeps growing over time.

Queue depth counts messages waiting in a queue. Consumer lag measures the distance between the newest message and the position of a specific consumer group. In Kafka several groups read the same topic, each with its own lag.

records-lag-max is a metric of the Kafka Java consumer client. It reports the maximum lag in number of records across the partitions assigned to that consumer, measured from the consumer's own perspective.

Only up to the number of partitions, because each partition is read by one consumer per group. If processing itself is slow, for example because of a downstream bottleneck, more consumers can even increase the pressure on that system.

Put these concepts into practice

Try Obseria free for 14 days, with OpenTelemetry and no proprietary agents.