Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. Why the average lies
  2. 2. Histograms, merging and honest load tests
  3. 3. Average the hosts, widen the buckets, stop waiting
  4. 4. When an averaged p99 is half the real one
  5. 5. How many requests stand behind a p99
  6. 6. Turning latency into a good-events ratio
  7. 7. Fifty hosts, one stall, one bucket choice

Topic 2

Histograms, merging and honest load tests

8 min read

From a stream to a percentile

Storing every latency forever is too expensive, so monitoring systems keep a histogram. A histogram is a list of buckets, each with a range of latencies and a count of requests that fell in it. Recording a request adds one to a counter. That is cheap and the size never grows.

To read a percentile, add up the counts from the lowest bucket until you pass the rank you want. For the p99 of a thousand requests, that is the 990th request. The bucket where the running total crosses 990 holds the p99. If the system wants a single value rather than a range, it guesses a position inside that bucket.

Two consequences follow, and the rest of this topic is about them. Histograms can be added together, bucket by bucket. And their accuracy depends on how wide the buckets are.

Percentiles cannot be averaged

Suppose many hosts each report their own p99. It is tempting to average the reports to get a fleet p99. That number is not the fleet p99. A percentile describes the shape of a whole group of requests, and the shape of a union is not the average of the shapes.

Step through a small example. Host A serves most of the traffic and is quick. Host B serves little and is often slow.

Why averaging p99s gives the wrong answer

Step 1 / 4
Host A, 90 requestsHost B, 10 requests102050100200500latency in ms, log scale. Each dot is one request.
requests on A
90
requests on B
10
  1. Two hosts report

    Host A served most of the traffic and was fast. Host B served little and had several very slow requests.

The right method is to add the buckets from every host and read the percentile once. If your tool only stores percentiles, you can still take the maximum of the per-host p99s as an upper bound, but you cannot recover the true value. That is why good metrics systems export histograms and aggregate those.

Bucket width sets the error

Fixed buckets are chosen before the data arrives. If most requests land in one wide bucket, the system can say only that the median is somewhere in that bucket, and the guess inside it can be far off. Narrower buckets are more accurate and cost more memory and more time series.

A common compromise is buckets that grow geometrically, so every bucket is a fixed percentage wider than the last. The error is then a fixed fraction of the value, whatever the value. HdrHistogram follows a close relative of this idea: it keeps a fixed number of significant digits at every magnitude. DDSketch uses geometric buckets directly, guarantees the relative error of any percentile, and merges across hosts.

The measuring tool can hide the tail

The last problem is not in the storage at all. It is in how the test sends requests.

A closed-loop load generator sends one request, waits for the reply, then sends the next. An open-loop generator sends requests on a schedule and does not care whether earlier replies have arrived. The difference matters the moment the system stalls.

One stall, two load generators

Step 1 / 4
40 requests, one every 50 ms. Request 11 stalls.
intended gap
50 ms
normal service time
20 ms
  1. A plan and a stall

    The test intends to send one request every 50 ms. Each normally takes 20 ms. Request 11 hits a 400 ms stall in the worker, such as a garbage-collection pause.

Gil Tene named this effect coordinated omission: the generator and the system coordinate, without meaning to, to leave out exactly the requests that would have looked bad. A closed-loop test of a system that sometimes stalls reports a tail that is too low, and the gap gets larger the worse the stall is.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms