← All posts

Measuring Kafka end-to-end latency without trusting two clocks

The hardest question in adding Kafka to Spitfire: how do you time a record from produce to consume without measuring the clock difference between two machines instead? The decisions we made, and why.

Spitfire is the distributed load testing platform we build. You put together tests for HTTP, gRPC, databases and queues in the browser, without writing code. When we added Kafka, most of the design came easily; one metric did not: end-to-end latency, the time between a record being produced and being consumed.

This post covers how we measure it, why we chose to measure the right thing rather than everything, and how we watch consumer lag without touching the real service.

The easy way, and why it is wrong

The obvious method is simple: the producer writes the current time into the message, and the consumer subtracts it from its own clock when the message arrives.

The trouble is that when the producer and the consumer run on different machines, the two times come from two different clocks. A few milliseconds of difference between two NTP-synced servers is normal, and the latency you are trying to measure is often exactly that size. The number you report as "p95 end-to-end latency" ends up showing the gap between two clocks, not your system. If the gap is negative, latency can even drop below zero.

One machine is not safe either: the wall clock can jump forward or back when NTP corrects it. Pull the clock back 30 ms in the middle of a test and every record measured at that moment looks 30 ms faster.

The worst thing a load testing tool can do is produce a number that looks trustworthy and is wrong. So we set the rule up front: only compare two times read from the same clock.

Decision 1: stamp every record with who stamped it

Spitfire adds a header to every record it produces:

spitfire-ts: <unix nanoseconds>;<source>

The source is a random ID for one run on one runner process (6 bytes, hex). It is created once per runner and shared by all its virtual users (VUs).

When a consume step reads a record, it checks the header first. If the record was stamped by its own source, produced in the same run by the same process, it records the latency as kafka_e2e_latency. Records produced on another runner are not measured; that runner's clock may differ.

Decision 2: stamp from the monotonic clock

Even within one process we did not want to rely on the wall clock. In Go, time.Now() carries a monotonic reading too, but it is dropped as soon as you turn the time into a number with UnixNano(). So we produce the stamp like this:

// Once, when the process starts: the wall clock plus the monotonic time since.
var clockStart = time.Now()

func stampNow() int64 {
	return clockStart.UnixNano() + int64(time.Since(clockStart))
}

time.Since uses the monotonic clock, so the difference between two stamps taken in the same process stays equal to the real elapsed time, even if NTP moves the clock in the middle of the test. The stamp's absolute value is roughly wall time; since we only ever compare stamps from the same process, that does not matter.

The price: sampling

This approach has an obvious cost. Run a test on N runners and only about 1/N of the records a consumer reads were produced on its own runner; the rest are not measured.

We accepted that on purpose:

  • A correct, sampled distribution is worth far more than a complete, wrong one.
  • In a test running thousands of records a second, even a 1/N sample is plenty for p95 and p99.
  • We do not hide it: the run page states plainly that the measurement is a sample.

Decision 3: set up clients the way a real service does

A measurement only means something if the load is realistic. We built the Kafka driver on franz-go and modelled the two sides differently:

  • Producers: all VUs on a runner share one producer client per connection. That is how a real service works: concurrent requests collect in one client and go out in batches. A client per VU would put a connection load on the brokers they would never see in production.
  • Consumers: every VU has its own consumer client. With a groupId they all join the same group and share the partitions like real service instances. One consequence follows naturally: a group cannot have more active consumers than partitions. Put 20 consumers on a topic with 12 partitions and 8 of them sit idle, so match the consumer count to your production instances.

Decision 4: watch the real service's lag, read-only

In a load test the most important question is often not whether the test's own consumer keeps up, but whether your real service does. A Spitfire step can watch other consumer groups with lagGroups. To make that safe we set a few rules:

  1. Read only. Through Kafka's admin API (franz-go's kadm package), Spitfire lists the partitions' end offsets and the group's committed offsets; the difference is the lag. Spitfire never joins the group, commits or resets offsets. Watching does not change how the real service works.
  2. One runner per run watches. Consumer lag is a property of the cluster, not of a runner's share of the load. Five runners querying the same group separately would mean idle load on the brokers and duplicates in the report. If two steps want to watch the same topic and group, the group is still reported once.
  3. Every two seconds, with a timeout. Each poll may take at most two intervals (4 seconds), so a slow broker cannot lock up the watch loop.
  4. No permission? Say so, and keep the test running. On an authorization error the watch stops and writes a single warning: which topic is not watched and which permission it needs (Describe on the topic and on the group). The load test carries on.
  5. Bound the series. Per-partition series are kept for at most 64 partitions; the group's total still counts every partition.

What we learned

A few principles this work left us with for anyone building a measurement tool:

  • First ask which clock a time comes from. Any metric that subtracts two times from different machines is suspect.
  • Partial but right beats complete but wrong. As long as you tell the user it is a sample.
  • The tool's load should look like real traffic. With the wrong client model, a correctly measured number measures the wrong thing.
  • Stay read-only when you watch a real system. A test tool accidentally disturbing a production consumer group costs more than any problem the test could find.

The user guide on Spitfire's site walks through setting up a Kafka test, writing thresholds and a complete example scenario step by step.