In this lesson · topic 2 of 7
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- requests on A
- 90
- requests on B
- 10
Two hosts report
Host A served most of the traffic and was fast. Host B served little and had several very slow requests.
Each computes its own p99
Sort the latencies and take the value at rank 0.99 times the count. Host A is quick all the way up. Host B is dominated by its slow requests.
Averaging them invents a number
Half of A plus half of B gives a value that no request had and that treats ten requests as equal to ninety. It lands between the two clusters, in a gap where almost nothing happened.
Merge the requests, then read the percentile
Pool all one hundred latencies and take rank 0.99 again. The slow requests are five percent of the whole, so the p99 lands inside them. A metrics system gets the same answer by adding up bucket counts from every host and computing the percentile once.
- Two hosts report. Host A served most of the traffic and was fast. Host B served little and had several very slow requests. requests on A: 90; requests on B: 10.
- Each computes its own p99. Sort the latencies and take the value at rank 0.99 times the count. Host A is quick all the way up. Host B is dominated by its slow requests. p99, host A: 34 ms; p99, host B: 460 ms.
- Averaging them invents a number. Half of A plus half of B gives a value that no request had and that treats ten requests as equal to ninety. It lands between the two clusters, in a gap where almost nothing happened. average of the two p99s: 247 ms.
- Merge the requests, then read the percentile. Pool all one hundred latencies and take rank 0.99 again. The slow requests are five percent of the whole, so the p99 lands inside them. A metrics system gets the same answer by adding up bucket counts from every host and computing the percentile once. p99 of all requests: 420 ms; average of p99s: 247 ms.
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- intended gap
- 50 ms
- normal service time
- 20 ms
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.
Keeping the schedule
A generator that sends on time keeps firing during the stall. Those requests queue behind the stalled one, and each is timed from the moment it should have gone out. The stall drags a whole run of requests with it.
Waiting for each reply
A generator that waits sends nothing during the stall. It records one slow request and then carries on as if nothing had happened. The requests that would have waited are never sent, so they are never counted. This is coordinated omission.
The tail disagrees
Take the 95th percentile of each set of forty. The waiting generator reports the normal service time. The schedule-keeping one reports a latency many times higher, which is closer to what a user arriving during the stall would have seen.
- 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. intended gap: 50 ms; normal service time: 20 ms.
- Keeping the schedule. A generator that sends on time keeps firing during the stall. Those requests queue behind the stalled one, and each is timed from the moment it should have gone out. The stall drags a whole run of requests with it. requests slowed: 14; worst latency: 420 ms.
- Waiting for each reply. A generator that waits sends nothing during the stall. It records one slow request and then carries on as if nothing had happened. The requests that would have waited are never sent, so they are never counted. This is coordinated omission. requests slowed: 1; worst latency: 420 ms.
- The tail disagrees. Take the 95th percentile of each set of forty. The waiting generator reports the normal service time. The schedule-keeping one reports a latency many times higher, which is closer to what a user arriving during the stall would have seen. p95, waits: 20 ms; p95, keeps schedule: 360 ms.
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.