Skip to content

Your Kafka consumer lag is an estimate. Ours says so. ​

Every Kafka console shows consumer lag as a plain integer. Rahdar, an open-source operations console for Apache Kafka, RabbitMQ and Apache ActiveMQ Artemis, shows it as ≈ 13,200 with a reason attached. This post explains why, down to the type that enforces it.

Lag is a subtraction, and neither operand holds still ​

For one partition, lag is the end offset minus the group's committed offset. For a group on a topic, it is that difference summed over the partitions. Three things keep the result from being a record count.

The end offset moves while you read it. The committed offsets and the end offsets come from separate requests. On a busy topic the two readings describe different moments.

Not every offset holds a record. A transactional producer writes a commit or abort marker into the log, and the marker takes an offset. Compaction removes records and leaves their offsets behind. The distance between two offsets therefore counts positions, and it can overcount records.

The start of the log moves too. If retention has deleted records past the group's committed offset, the group cannot be behind by records that no longer exist.

Here is the whole per-partition computation in the Kafka driver of OpenBroker, the open SDK Rahdar reads every broker through:

java
long lag = at.lag().orElse(Math.max(0, last - Math.max(at.offset(), first)));

at.lag() is the lag the cluster itself reports for the partition, when it does. Otherwise it is the end offset last minus the group's position, with the position clamped to the log start first, and the result clamped at zero.

The driver knows what that number is worth. It attaches this sentence to every group it returns:

java
/** Why a lag is only an estimate. */
static final String ESTIMATE_REASON =
        "The end offset moves while it is read, and transaction markers and compaction leave offsets that"
                + " hold no record, so the lag can overcount.";

A partition with no answer is not a partition with zero lag ​

The second problem is worse, because it produces a number that is wrong in the comforting direction.

Summing over partitions assumes every partition has a lag. Some do not. A group may have no committed offset on a partition. An end offset may fail to come back. The easy code path is getOrDefault(tp, 0L), which reports those partitions as fully caught up.

The driver refuses to do that. A partition without an answer goes into a separate map with its reason, and stays out of the sum:

java
if (at == null) {
    unavailable.put(tp.partition(), "The group has no " + offsetsName() + " offset on this partition.");
} else if (last == null || first == null) {
    unavailable.put(tp.partition(), "The partition's end offset could not be read.");
} else {
    // ... lag computed, added to the breakdown and the total
}

The total then says what it left out. The reason on the group gains a second sentence: "N partitions are left out of the total because their lag is unknown."

A total of 12,400 over five of six partitions is a different fact from 12,400 over six of six. The person on call needs to know which one they are reading.

The type: exact, estimate, or unavailable with a reason ​

OpenBroker carries this distinction in its API, so a consumer cannot lose it by accident. Counts that a caller acts on, such as the affected count of a preview or the total of a browse, use this type. It is quoted here in full, minus the three static factories:

java
/** A number that says how much it can be trusted. */
public sealed interface Count {

    record Exact(long value) implements Count {}

    /** Derived, such as a backlog computed from offset distances. Never shown as exact. */
    record Estimate(long value) implements Count {}

    /** The system cannot say, and why. An unavailable count is never zero. */
    record Unavailable(String reason) implements Count {
        public Unavailable {
            Objects.requireNonNull(reason, "reason");
        }
    }
}

Two details do the work:

  • Unavailable has no value field. There is nothing to read as zero. The reason is required, so "unknown" always arrives with an explanation.
  • The interface is sealed. A switch over a Count has to handle all three cases, or the compiler complains. Rahdar's own code is full of case Count.Unavailable(var reason) -> branches for this reason.

The backlog metric follows the same rule with a different shape. It is a MetricValue with a Precision of EXACT or ESTIMATE, and a per-partition breakdown. The Kafka driver always builds a group's backlog with Precision.ESTIMATE. There is no code path that reports Kafka lag as exact.

The trade-off is real. Every consumer of the SDK pays for a three-way branch where a long would have been one line. We think that is the correct price.

What the operator sees ​

The rule on the screen is one line of TypeScript:

ts
return backlog.accuracy === 'ESTIMATE' ? `≈ ${figure}` : figure;

An exact figure is plain. An estimate carries the approximately-equal mark and has "estimate" in its accessible name, so a screen reader hears it too. An unavailable figure reads Unavailable, never 0.

The figure is a button. It opens the reason the driver gave, and Show partitions where the system has a breakdown.

The backlog of one consumer group on one topic, by partition: lag, committed offset and end offset, slowest partition first, with the age of the sample

The drawer lists partitions slowest first, and its first line states the age of the sample: "Sampled 0s ago. The slowest partitions come first." In this capture the group analytics on topic orders has six partitions. Four of them have a committed offset of 0, and the six lags sum to 13,200.

A partition whose lag could not be read shows Unavailable in the Lag column with its reason underneath, and None or Unknown for a missing committed or end offset.

One candid note: partitions with an unknown lag currently sort to the bottom of that list, below the caught-up ones. There is a fair argument they belong at the top.

The group's own page shows the same table per topic:

A Kafka consumer group's page: type, state, members, and every partition with its lag, committed offset and end offset

The estate Dashboard applies the rule one level up. Its Backlog tile sums every cluster the user may read, across all three systems. In the current capture it reads ≈ 297.5K, and under it:

Estimate: events: 6 subscriptions report an estimated backlog; rabbit: 1 subscription reports no backlog, 5 subscriptions report an estimated backlog.

A sum is only as exact as its least exact term, and the tile names which terms those are, per cluster.

The same rule on previews ​

Every destructive operation in Rahdar has a dry run that returns an affected count without acting. That count has the same problem as lag: the queue or log keeps moving between the preview and the act.

ADR-0022 settled this early, when Rahdar only spoke to Artemis. The dry-run count is a broker-side estimate and is labelled a point-in-time estimate in the API and the UI. The audit row of the executed operation records the count the broker actually affected. In the ADR's words, that second number "is the number of record". When the two disagree, the audit trail holds both.

Kafka added a twist, recorded in ADR-0221. Purging a group's backlog means moving the group to the last offset it can read. Rahdar's bulk preview used to estimate from the scraped lag, which also counts records an open transaction still holds. The confirmation therefore named a move the broker would not make.

The preview now asks the driver once per selected group, and the number on the confirmation is that answer. The ADR's decision reads:

A count the driver does not report is unavailable, never zero, and the purge is not offered.

The cost is one driver call per group at preview time, bounded by the existing cap on bulk selections. The ADR lists a batched preview as the alternative, and rejects it for now because it would need a new SDK shape.

An absent number reads as zero ​

Rahdar's frontend rules contain one sentence that all of the above follows from: an absent number reads as zero, which is the most dangerous possible misreading.

Someone paged at 3 a.m. glances at a lag column. A blank cell, a dash and a 0 all tell them the same thing: this consumer is fine, look elsewhere. If the truth is "we could not read this partition", the console has just sent them away from the problem.

So the number says what it is. Exact figures are plain, estimates are marked and explained, and unknowns say Unavailable and why.

To see it on your own cluster:

Rahdar and OpenBroker are both Apache-2.0. The lag code quoted above is in Subscriptions.java of the Kafka driver.

Apache Kafka, Apache ActiveMQ Artemis and RabbitMQ are trademarks of their respective owners. Rahdar is not affiliated with or endorsed by them.

Apache-2.0. Apache Kafka, Apache ActiveMQ and Apache ActiveMQ Artemis are trademarks of the Apache Software Foundation. RabbitMQ is a trademark of its owner. Rahdar is an independent project, not produced by, endorsed by, or affiliated with the ASF or the owner of RabbitMQ.