Part 01 · Measuring Performance
Why Averages Lie
Average latency says 80 ms while one in ten customers waits 2 seconds. How averages hide pain, and why you can't average percentiles.
⏱ 9 min · Station 4 of 95
The problem
The weekly report says "average latency: 26 ms". That sounds fine for a shop. But no customer ever waited 26 ms: nine in ten waited about 2 ms, and one in ten waited about 200 ms. The average described nobody, and the customers who wait 200 ms on every tenth click are the ones writing complaints.
It gets worse at fleet scale. Three servers report their p99s; someone averages them for the dashboard; the slow server disappears into the average. These two mistakes (averaging latencies, and averaging percentiles) are the most common ways a latency dashboard lies.
The idea
Lie 1: the mean of a skewed distribution. Latency distributions are not bell curves. They have a big fast bump and a long slow tail, sometimes two separate bumps (cache hit vs miss, fast path vs slow path). The mean lands between them, where no request lives, and it moves with the tail in a way that's hard to read: a few very slow requests drag it up, a change that speeds up the fast majority barely moves it.
Lie 2: averaging percentiles. A percentile is a property of a whole distribution. The p99 of a fleet is not the average of each server's p99: one slow server serving a third of the traffic can own most of the fleet's slow requests, and averaging it with two healthy servers divides its damage by three. The fix is to combine the raw data first (sum the histogram buckets across servers), then compute the percentile.
And a third, quieter one: where you measure. A server's own histogram starts its stopwatch when its handler starts. Time spent waiting before that (in a queue, a starved CPU, the network) is invisible to it. Users see it.
How it works
The bimodal case, with 20 requests per second, 90% taking 1 ms of work and 10% taking 200 ms:
mean = 0.9 × ~2 ms + 0.1 × ~200 ms ≈ 22 ms (measured: 26 ms)
p50 ≈ 2 ms (the fast bump)
p95 ≈ 200 ms (inside the slow bump)
The fleet case, with one of three servers starved of CPU:
- each server's histogram gives its own p99: about 5 ms, 5 ms and 95 ms;
- the average of those three is 35 ms;
- summing the three servers' buckets and taking the 99th percentile of the combined counts gives 84 ms, because about a third of all requests went to the slow server;
- users, measured by the load generator from outside, saw a p99 of 3.1 seconds, because requests queued on the starved server before its handler (and its stopwatch) ever started.
Three servers behind round robin. Their p99s are 5 ms, 5 ms and 95 ms. What's the fleet's p99, computed properly from the combined histogram?
Visual
Common misconception
"Averages are fine for trends: if the average goes up, something got worse." Not reliably. If the fast majority gets faster while the slow tail gets much slower, the average can stay flat while users suffer more. If one rare request type gets slower, the average moves a lot and looks like a general problem. Percentiles separate the two.
"The server's metrics are what users experience." They're what the server experienced from the moment it started handling the request. Queueing before the handler, the network, the load balancer, and TLS all happen outside that window. Measure at the edge too (the load balancer's latency, or real-user timing from browsers and apps).
Break it, then fix it
Measured on the lab (Intel i3-10110U; each shop copy on its own CPU thread). First the bimodal service: mixed.js sends 20 requests per second, 10% of them to a 200 ms code path, slow enough that nothing queues:
cd ../../lab
echo "== 20 requests/s: 90% take 1 ms, 10% take 200 ms (no queueing at this rate)"
./k6.sh mixed.js -e TARGET=http://api:8080 -e RATE=20 -e HEAVY=0.1 -e HEAVY_MS=200 -e DURATION=60s 2>/dev/null
== 20 requests/s: 90% take 1 ms, 10% take 200 ms (no queueing at this rate)
requests 1200 (20/s)
all avg 26 ms p50 1.9 ms p95 201 ms p99 219 ms
cheap p50 1.9 ms p95 20 ms p99 34 ms
heavy p50 201 ms p95 220 ms p99 238 ms
- Mean 26 ms, p50 1.9 ms, p95 201 ms. No request took anywhere near 26 ms. The fix is to report percentiles (and, here, the two request types separately: cheap p50 1.9 ms, heavy p50 201 ms).
Then the fleet: three copies behind round-robin Nginx, api3 limited to a tenth of a CPU, 300 requests per second to a 1 ms endpoint for a minute. We ask Prometheus for each copy's p99, their average, and the p99 of the combined buckets, and compare with what k6 saw from outside:
cd ../../lab
docker compose stop api >/dev/null 2>&1
LB_CONF=round-robin docker compose --profile lb up -d --force-recreate api1 api2 api3 lb >/dev/null 2>&1
sleep 5
docker update --cpus 0.1 syslab-api3-1 >/dev/null # one copy of three is starved of CPU: slow
echo "== k6, 300 requests/s through the load balancer for 60 s"
./k6.sh product.js -e TARGET=http://lb:80 -e "PATH_TPL=/work?ms=1" -e RATE=300 -e DURATION=60s 2>/dev/null | grep latency
sleep 6
H='http_request_duration_seconds_bucket{route="GET /work"}'
echo "== p99 per copy (Prometheus)"
./prom.sh "1000*histogram_quantile(0.99, sum by (instance, le) (rate($H[1m])))"
echo "== average of the three p99s"
./prom.sh "avg(1000*histogram_quantile(0.99, sum by (instance, le) (rate($H[1m]))))"
echo "== p99 of the fleet: buckets summed first, then the percentile"
./prom.sh "1000*histogram_quantile(0.99, sum by (le) (rate($H[1m])))"
docker compose --profile lb stop api1 api2 api3 lb >/dev/null 2>&1
docker compose up -d api >/dev/null 2>&1
== k6, 300 requests/s through the load balancer for 60 s
latency avg 720 ms p50 2.0 ms p95 2975 ms p99 3089 ms max 5909 ms
== p99 per copy (Prometheus)
instance=api2:8080 4.952
instance=api1:8080 4.95
instance=api3:8080 95.11
== average of the three p99s
value 35.01
== p99 of the fleet: buckets summed first, then the percentile
value 84.19
| Number | Value | What it actually is |
|---|---|---|
| api1, api2 p99 | 4.95 ms | healthy (the top of the first 5 ms bucket) |
| api3 p99 | 95.1 ms | the starved copy, as its own stopwatch saw it |
| Average of the three p99s | 35.0 ms | a number about no one |
| Fleet p99 (buckets summed, then percentile) | 84.2 ms | correct for what the servers measured |
| p99 seen by users (k6) | 3,089 ms | including the queue in front of api3's handler |
- The fix for the dashboard is one change in the query:
sum by (le)across instances beforehistogram_quantile, neveravg()after it. - The fix for the blind spot is a second measuring point. The servers said "at worst 95 ms". Users waited seconds, because requests sat waiting for api3's starved CPU before its handler started timing them. A latency metric from the load balancer, or from the clients, would have shown it.
Engineering insight
- Store histograms, aggregate histograms, compute percentiles last. That's why Prometheus histograms (and HDR histograms, t-digests, DDSketch) exist: they can be merged across servers and time. Precomputed percentiles (Prometheus "summaries", many APM agents) cannot.
- Use the mean for what it's good at: totals. Mean latency × request count = total time spent, useful for capacity and cost. It's the wrong number for user experience.
- Measure at more than one layer. Client or edge (what users get), load balancer (includes queueing in front of servers), server (where to look inside). Big gaps between them point at the problem: here, 95 ms inside versus 3 s outside.
- Break latency down by type. A bimodal service is two services in a trench coat. Separate metrics (or labels) for cache hits and misses, small and large accounts, let you see each one clearly.
Key takeaway
The mean of a skewed latency distribution describes no request (26 ms where requests took 2 or 200 ms), and averaging percentiles across servers hides the slow one (35 ms instead of the fleet's 84 ms). Report percentiles, combine histograms before computing them, and measure from the user's side too: here users saw 3.1 seconds while every server reported 95 ms or less.
Practice
Objective: find a slowdown the servers can't see at all.
Challenge: instead of starving api3 of CPU, add 50 ms of network delay to everything it sends (./netem.sh api3 delay 50ms) and run the same measurement. Predict what the three server-side p99s and the user-side p99 will show.
Expected result: server-side numbers that look healthy, a user-side number that doesn't, and the reason.
Hint 1
When does api3's stopwatch start and stop, relative to the network delay?
Hint 2
netem delays packets as they leave the container, after the handler has finished and recorded its time.
Hint 3
One request in three goes to api3. What does that do to the users' p95 and p99?
Solution
== k6, 300 requests/s through the load balancer for 60 s
latency avg 18 ms p50 1.7 ms p95 52 ms p99 53 ms max 153 ms
== p99 per copy (Prometheus)
instance=api2:8080 4.95
instance=api1:8080 4.95
instance=api3:8080 4.95
== average of the three p99s
value 4.95
== p99 of the fleet: buckets summed first, then the percentile
value 4.95
Every server-side p99 is 4.95 ms (the top of the first bucket): api3's handler is as fast as ever, and the 50 ms is added after it stops its stopwatch. Users saw p95 52 ms and p99 53 ms, because a third of their requests went through the slow network path, and the average (18 ms) sits between the two groups again. A dashboard built only from server metrics shows a perfectly healthy fleet. This is why production setups also record latency at the load balancer and from real clients.
Interview check
Q: Your dashboard shows average latency flat at 50 ms for weeks, but support tickets about slowness are increasing. How can both be true?
Model answer
The average can stay flat while the distribution changes shape: the fast majority got a bit faster while a slow minority got much slower, or a new slow request type grew. Look at p95/p99 and at latency broken down by endpoint, customer segment and request type. Also check that the latency is measured where users experience it (edge or client), since server-side metrics miss queueing and network time.
Q: You have p99 latency per server. How do you get the p99 for the whole service?
Model answer
You can't from per-server p99s. Keep the underlying histograms (bucket counts) per server, sum them across servers for the same time window, and compute the 99th percentile from the summed histogram. In PromQL: histogram_quantile(0.99, sum by (le) (rate(..._bucket[5m]))). If only precomputed percentiles exist, the most you can say is a bound (the fleet's p99 is somewhere between the lowest and highest server's), which is rarely useful.
Station stamp
Finish the chapter
Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.
Go Pro to earn XP →