ealeng

Episode 03 of 55 · Estimates and load testing

Tail latency traps: right numbers, wrong answers

A load test can measure correctly and still mislead you afterwards. Averaging the per-second p99s of one test gave 160 ms; the p99 of the same requests was 925 ms. A page waiting on 20 parallel lookups hit a slow one 63% of the time, not the 18% the textbook formula predicts. And eight runs of the same code gave p99s from 1.58 to 3.81 ms. Three checks to make before you trust a number.

Lab 02 · Load Testing and Tail Latency14 min read
Video transcript5:43

0:00Cold open

0:01

Say my dashboard shows the slowest requests of every second. Averaged over a minute: about a hundred and sixty milliseconds.

0:10

Then I checked every single request. The slowest one in a hundred took about nine hundred and twenty-five.

0:18

About six times worse. Same data.

0:22

The test was right. The mistake came after it.

0:27Intro

0:28

Hands-on system design, part three.

0:31

Today: three ways that correct numbers still fool you.

0:35Meet Lucía

0:37

I’m Lucía, a platform engineer. Last time, I built a load test I could trust.

0:43

My test service freezes for one second in every ten, on purpose. So the right answer is known.

0:50

The answer is the p99: the slowest one request in a hundred. Here, about nine hundred milliseconds. My test found it.

1:00

But a right number can still give you a wrong answer. Here are three ways.

1:0601 · Averaging percentiles

1:07

First: averaging. Here’s a made-up example. Ten seconds, a hundred requests in each second. One second freezes.

1:16

Each second gets its own p99. The nine quiet seconds: one millisecond. The frozen second: a thousand.

1:26

Average those ten numbers, and you get about a hundred milliseconds.

1:31

Now put all thousand requests together. A hundred of them were frozen.

1:37

That’s ten in a hundred, far more than one in a hundred. So the p99 of all of them is a full second.

1:45

The average gave a quiet second the same weight as the second when everyone waited.

1:5202 · The real test

1:54

My real test looks just like that. Fifty-eight seconds. The red bars are the seconds with a freeze.

2:02

Average the fifty-eight bars: about a hundred and sixty milliseconds.

2:07

The p99 of all the requests: about nine hundred and twenty-five.

2:14

So how do you combine seconds, or servers? Keep tallies, not percentiles.

2:20

A histogram is a tally: how many requests landed in each speed range.

2:25

Tallies can be added. Take the quiet second and the frozen second, and add them, range by range.

2:33

Then read the p99 once, from the total: one second. Right.

2:39

Many monitoring tools keep one small tally per second, or per server, and add them up the same way.

2:4703 · One page, twenty calls

2:49

Second: a page that asks twenty backends at once, and waits for all twenty. In my test, they’re twenty database lookups, made at the same time.

2:59

It’s like a table of twenty at a restaurant. Dinner starts when the last plate arrives. One slow plate, and the whole table waits.

3:08

Call a lookup slow if ninety-nine lone lookups in a hundred are faster. So one time in a hundred.

3:16

If the twenty were separate coin flips, all twenty would be fast on about eighty-two pages in a hundred.

3:23

So about eighteen pages in a hundred would hit a slow one.

3:28

I measured it. What do you think it was?

3:33

Sixty-three pages in a hundred. More than three times as many.

3:39

Why? They weren’t coin flips. All twenty share one app, and its database connections. Like one kitchen cooking every plate.

3:48

A typical lookup took about a millisecond inside a page. That’s already past the slow line.

3:5604 · Nothing changed

3:57

Third: noise. Say I speed up my code, and the p99 drops from one point eight to one point six milliseconds. Did I win?

4:08

First, I ran the old code eight times, and changed nothing. It’s called an A-A test: the same version against itself.

4:18

The typical request, the median, barely moved. About zero point four milliseconds, every time.

4:25

The p99 is a different story. From one point five eight to three point eight one. More than double.

4:34

And each run is sure of itself. Run one’s error bar, for example: plus or minus seven hundredths of a millisecond.

4:42

Each run is far too sure. Between runs, the laptop itself changes. Other programs were running too.

4:50

So my one point eight to one point six? It’s inside the wobble. It proves nothing.

4:57

To believe a change, run each version several times, taking turns, and compare the spreads.

5:05Back to Lucía

5:06

So, the numbers were right, and they still fooled me. Three checks.

5:12

Add up tallies. Never average percentiles.

5:16

Measure the whole page, not one call.

5:19

Measure the noise before you compare.

5:23

Next time: how hard can you push one server before it falls over? And can you predict it?

5:29

For today: a correct number can still be the wrong number to look at.Lucía: Add the tallies. Then read the p99.

5:35Summary

5:36

Tallies added up, the whole page measured, and the noise measured first.

Key takeaways

  • ✓Never average percentiles. The mean of 58 per-second p99s was 159.5 ms; the p99 of all 29,148 requests was 925.5 ms, about 6× higher. The average of p99s isn't a percentile of anything, and it can be too high as well as too low.
  • ✓Keep tallies, not percentiles. A histogram counts requests per latency range, and histograms with the same ranges add up. The 58 per-second histograms added together gave a p99 of 925.7 ms, within 0.02% of the exact value.
  • ✓A page that waits for n parallel calls is as slow as the slowest. If calls were independent, 1 − 0.99ⁿ of pages would include a call slower than its p99: 18.2% at n = 20. Measured: 62.8% of 8,532 pages.
  • ✓The formula is a floor, not an estimate. The 20 lookups shared one app process and its database connections, so each was slower inside a page (median 1.14 ms) than alone (0.37 ms), already past the 0.93 ms slow line. Measure the whole page, not one call.
  • ✓Measure the noise before you compare. Eight runs with nothing changed (an A/A test) gave a p99 from 1.58 to 3.81 ms, a range 16× wider than one run's own 95% interval. The median barely moved (0.390–0.405 ms).
  • ✓One run's interval only covers sampling noise inside that run, not the machine changing between runs. To believe a change, run each version several times, taking turns (A, B, A, B…), and compare the spreads. 'p99 went from 1.8 to 1.6 ms' is noise on this laptop.

A load test can be right and still mislead you. This chapter is about what happens after the measurement: three ways that correct numbers give wrong answers. Averaging the per-second p99s of one test gave 160 ms, while the p99 of the same requests was 925 ms. A page that made 20 lookups at once hit a slow one 63% of the time, where the textbook formula says 18%. And the same code, run eight times, gave p99s from 1.58 to 3.81 ms. Each trap comes with the check that catches it.

The examples come from the second half of Lab 02 (Load Testing and Tail Latency) of the system design labs: Exercise 2 of 02_hdr_histogram.py (averaging percentiles), 05_fanout.py (one page, many calls) and 06_aa_noise.py (the noise when nothing changed). The first half of the lab, scripts 01 to 04, is Chapter 2: closed- and open-loop load tests, the freeze the lab plants on purpose, and checking the load generator. The setup is the same as in Chapter 1: one laptop (an Apple M4 Max), a Python web app (FastAPI, one worker) and Postgres 18 in Docker, a million rows, and a load generator on the same machine. The numbers are this laptop's; the traps are everywhere.

Where we left off

In Chapter 2, the lab added an endpoint, GET /stall/k/{id}, that does the usual lookup but freezes for the first second of every ten seconds. Because the freeze is planted, the right answer is known in advance: about 10% of requests land in a freeze and wait for the rest of it, so the p99 (the latency that 99 in 100 requests beat, so it describes the slowest 1 in 100) should be about 900 ms. An open-loop load test, one that sends requests on a fixed schedule like independent users instead of waiting for each reply, measured 925.5 ms. The test was right. Everything below starts from numbers that are right.

Trap 1: averaging percentiles

Many dashboards show a p99 per second, or per server, and then combine them. The easy way to combine numbers is to average them. Here's why that fails, first on a made-up example.

A made-up example. A test runs 10 seconds, with 100 requests in each. A normal request takes 1 ms. In one second, the service freezes and all 100 requests take 1,000 ms.

  • Each second gets its own p99. The nine quiet seconds: 1 ms. The frozen second: 1,000 ms.
  • Average those ten numbers: (9 × 1 + 1,000) ÷ 10 = 100.9 ms.
  • Now put all 1,000 requests together. 100 of them froze: 10 in 100, far more than 1 in 100. So the p99 of all of them is 1,000 ms.

The average gave a quiet second the same weight as the second in which everyone waited. And it isn't only too low. Change the example so that only 5 requests freeze in that one second: that second's own p99 is still 1,000 ms (5 slow requests in 100 reach the slowest 1%), so the average is still 100.9 ms. But 5 in 1,000 is half of 1%, so the p99 of all the requests is 1 ms, and the average is now 100× too high. The average of p99s isn't the p99 of anything.

The real test. The lab split the open-loop run of the freezing endpoint into 58 one-second windows (about 500 requests each, after dropping the first 2 seconds as warm-up), computed each window's p99, and combined them several ways:

p99
mean of the 58 per-second p99s 159.5 ms
median of the per-second p99s 3.1 ms
max of the per-second p99s 1,031.2 ms
p99 of the 58 histograms added together 925.7 ms
exact p99 of all 29,148 requests 925.5 ms

The average is off by about 6×, and none of the other per-second summaries is right either. The median says almost nothing went wrong; the max is just the worst second. Each 1-second freeze falls across two of the windows (they're cut by when each request was meant to be sent, not aligned with the freezes), so 12 windows are elevated and 46 are quiet, 44 of those under 10 ms.

Try it · Average the p99s, or add the talliesLab 02 · 02_hdr_histogram.py, Exercise 2

A made-up test: 10 seconds, 100 requests in each. A quiet request takes 1 ms, a frozen one 1,000 ms.

Seconds with a freeze

Frozen requests in each of those seconds

12345678910
each second's own p99 average of the 10 p99s p99 of all 1,000 requests
Average of 10 p99s
100.9 ms
p99 of all 1,000
1,000 ms
Frozen requests
100/1,000

The average is too low: 100 slow requests are 10.0% of all 1,000, more than 1 in 100, so the p99 of everything is a full second. The quiet seconds pulled the average down.

Percentiles here are nearest-rank: sort, then take the value at position ⌈0.99 × n⌉. With 100 requests a second, one frozen request isn't enough to move a second's p99; with 1,000 together, it takes 11. Real windows are cut by when each request was meant to be sent; each 1 s freeze falls across two of them.

Keep tallies, not percentiles. A histogram is a tally: how many requests landed in each latency range. Tallies with the same ranges can be added, range by range, and the sum is exactly the tally you'd have got by recording every request in one place. So the fix is to keep one histogram per second (or per server), add them, and read the p99 once from the total.

The lab's histogram is an HDR histogram (high dynamic range): its ranges get wider as latency grows, so every value is kept within 0.1% of its true size, and its memory is fixed (184 KB covers 1 µs to an hour, however many requests you record). Added together, the 58 per-second histograms gave 925.7 ms against the exact 925.5 ms, a 0.02% difference that comes from the range width, not from adding.

The same rule covers anything you combine: seconds into minutes, servers into a fleet, regions into a service. Counts, sums (and so means) and maxima combine; medians and percentiles don't. Monitoring tools that report percentiles across servers keep mergeable tallies for this reason: Prometheus histograms, OpenTelemetry's exponential histograms, t-digests.

Trap 2: one page, twenty calls

A web page often asks many backends at once and can't render until every one has answered. That's fan-out: the page is as slow as its slowest call. Think of a table of 20 at a restaurant. Dinner starts when the last plate arrives, and one slow plate keeps the whole table waiting.

The formula. Call a backend call slow if it's slower than that backend's own p99, which happens one time in 100. If the 20 calls were independent coin flips, all 20 would be fast with probability 0.99²⁰ ≈ 0.82. So about 18% of pages would wait on at least one slow call. In general, the share is 1 − 0.99ⁿ: 4.9% at 5 calls, 18.2% at 20. One backend's p99 becomes the page's p82. This effect is called tail amplification.

The measurement. The lab's stand-in for 20 backends is an endpoint, GET /fanout/{n}, that runs n primary-key lookups at once inside one request and reports each lookup's own time. So the page's latency and every call inside it come from the same request. A load generator sent 150 pages a second, open loop, for 60 seconds per value of n. A lookup on its own took a median of 0.37 ms and a p99 of 0.93 ms (8,632 lookups), so "slow" means slower than 0.93 ms.

Lookups per page Pages 1 − 0.99ⁿ Pages with a lookup > 0.93 ms Median lookup inside the page Slowest lookup's p99, if independent / measured Page p99
1 8,632 1.0% 1.0% 0.37 ms 0.93 / 0.93 ms 3.2 ms
5 8,556 4.9% 11.4% 0.40 ms 1.58 / 1.99 ms 4.5 ms
20 8,532 18.2% 62.8% 1.14 ms 3.05 / 5.94 ms 9.1 ms

The direction is right: more calls per page, more pages hit a slow one. The size is not: at 20 lookups, 63% of pages, more than three times the formula.

Why. The formula assumes each call is an independent draw from the single-call distribution. These 20 weren't. They all ran in one app process, on one event loop (the single thread that runs the app's code, one piece at a time), and through one connection pool (the fixed set of open database connections the app shares between requests). So they queued behind each other, like one kitchen cooking every plate for the table, and each one got slower: the median lookup inside a 20-lookup page took 1.14 ms against 0.37 ms alone. The typical lookup was already past the 0.93 ms slow line. The lab didn't isolate the cause further, so take it as the most likely explanation, not a proven one. Like one kitchen cooking every plate for the table.

Try it · One page, n lookups at onceLab 02 · 05_fanout.py · 150 pages a second, open loop

Lookups per page (n)

A page is red if at least one of its lookups was slower than a lone lookup's p99 (0.93 ms). The page waits for all n.

If the lookups were coin flips: 1 − 0.9920

18.2% of pages

Measured, 8,532 pages

62.8% of pages

Median lookup, in page
1.14 ms
Median lookup, alone
0.37 ms
Slowest lookup p99
5.94 ms
Page p99
9.11 ms

Twenty at once: 18.2% predicted, 62.8% measured. The typical lookup inside a page took 1.14 ms, already past the 0.93 ms slow line, because all 20 share one app process and its database connections. Independence would put the slowest lookup's p99 at 3.05 ms; it was 5.94 ms.

The “backends” are a stand-in: GET /fanout/n runs n primary-key lookups at once inside one request and reports each lookup's own time. Separate services on separate machines would be closer to independent; anything they share (a pool, a thread, a database) pushes them toward the right-hand grid.

Separate services on separate machines are closer to independent, so they sit nearer the formula. But anything they share, a database, a cache, a network link, a thread pool, pushes them toward the measurement. Treat 1 − 0.99ⁿ as a floor. And independence also tells you how bad the page gets: the slowest of n independent calls is under a time t only if all n are, so its p99 is one call's 0.99^(1/n) quantile, about the p99.95 of a single call at n = 20. Even with perfect independence, a page with 20 calls lives in each backend's far tail.

What to do. Measure the whole page, not one call, and report the page's latency. To fight tail amplification: fewer calls per page, caching, a shorter tail on each backend, and hedged requests (sending a second copy of a call that hasn't answered after a short delay and taking whichever returns first), described in Dean and Barroso's "The Tail at Scale". The lab lists hedging as an exercise; it didn't measure it.

Trap 3: nothing changed

Say you speed up your code and the p99 drops from 1.8 ms to 1.6 ms. Did you win?

Before comparing two versions, the lab ran the same version against itself: an A/A test, the same load test repeated with nothing changed. Any difference between the runs is noise: the scheduler, other programs, the VM, the laptop itself. Eight runs of the lookup at 3,000 requests a second, open loop, 25 seconds each, about 69,000 requests per run:

Run Requests p50 p99 (95% interval) p99.9
1 69,190 0.397 ms 1.77 (1.71–1.85) ms 4.6 ms
2 69,120 0.403 ms 2.39 (2.29–2.58) ms 74.1 ms
3 69,110 0.390 ms 1.58 (1.52–1.62) ms 5.4 ms
4 68,788 0.402 ms 1.66 (1.60–1.75) ms 4.5 ms
5 69,289 0.404 ms 1.79 (1.74–1.85) ms 7.4 ms
6 68,841 0.405 ms 2.24 (2.15–2.34) ms 46.2 ms
7 68,765 0.397 ms 1.77 (1.71–1.84) ms 5.5 ms
8 68,966 0.396 ms 3.81 (3.32–4.51) ms 98.3 ms

And the summary across runs. The spread is the range divided by the median; the last column compares the range with the width of one run's own 95% interval (the median width over the 8 runs):

Median Range over 8 runs Spread One run's interval Range ÷ interval
p50 0.400 ms 0.390–0.405 ms 4% 0.002 ms 7.7×
p99 1.78 ms 1.58–3.81 ms 126% 0.14 ms 15.8×
p99.9 6.5 ms 4.5–98.3 ms 1,450% 2.3 ms 41×

The median barely moved. The p99 more than doubled between the best and the worst run, and the range was 16× wider than any one run's interval. Each run was sure of itself: run 1's interval is about ±0.07 ms. And each was far too sure.

Why one run's interval is too narrow. The interval (an order-statistic interval, the kind Chapter 1 checked) covers sampling noise: which requests happened to land in this run. It knows nothing about the machine changing between runs. On this laptop, another long-running job was busy throughout, and the load average (roughly, how many programs were running or waiting for a CPU core) was 12.5 when the A/A runs ended. The tail feels that first. The p99.9 swings hardest of all because each run has only about 69 requests above it, so one 100 ms hiccup decides it; the median rests on about 35,000 requests on each side.

Try it · The same test, 8 timesLab 02 · 06_aa_noise.py · GET /k/{id}, 3,000 req/s open loop, 25 s
Run 11.77 ms
Run 22.39 ms
Run 31.58 ms
Run 41.66 ms
Run 51.79 ms
Run 62.24 ms
Run 71.77 ms
Run 83.81 ms
the run's p99 its own 95% interval range over the 8 runs
Range over 8 runs
1.58–3.81
Range ÷ median
126%
One run's interval
0.140 ms
Range ÷ interval
15.8×

Nothing changed, yet p99 ranged from 1.58 to 3.81 ms: more than double, and 16× wider than one run's own interval.

A change claims p99 went from 1.8 ms (dashed) to…

1.6 ms is inside the range the unchanged code produced on its own (1.58–3.81 ms). One run each proves nothing.

About 69,000 requests a run. The interval is an order-statistic 95% interval: it covers sampling noise inside one run, not the machine changing between runs. Another long-running job was busy throughout, and the load average was 12.5 when the runs ended.

So "1.8 to 1.6 ms" is inside the wobble. It proves nothing. The spread between identical runs is your noise floor: no improvement smaller than it means anything. The lab's rule for every later lab: a change must beat the A/A spread, measured the same way. Run each version several times, interleaved (A, B, A, B…, so slow patches of the machine hit both), and compare the two sets of runs as distributions, not one number against another. Your laptop's noise floor will differ from this one: measure it, don't borrow it.

Caveats

  • One laptop, shared. The load generator, the app and Postgres share one machine's cores through Docker Desktop's VM, and another long-running job was busy throughout. A quiet machine should show a narrower A/A spread.
  • The fan-out backends are a stand-in. /fanout/{n} is n lookups inside one app process, so they share far more than separate services would. The 63% shows what sharing does; it isn't a number for your services. The cause (lookups queueing behind each other) is the most likely explanation, not one the lab isolated.
  • The averaging example is made up; the 159.5 ms against 925.5 ms is the real run. The per-second view is the lab's own computation, not a real dashboard's. Real dashboards differ in window length and in how they combine; check yours.
  • Histogram percentiles are approximate by design: the HDR histogram here keeps every value within 0.1%. Other mergeable sketches trade accuracy for memory differently.
  • Eight runs is a small sample of the noise. It's enough to show that one run's interval is too narrow, not to pin the noise floor down precisely.

The checklist

  1. Never average percentiles across seconds, servers or regions. Keep a histogram per window, add them, and read the percentile once.
  2. Check what your dashboard does when it combines p99s across servers or time. If it averages, it's showing a number no user experienced.
  3. Measure the whole page, not one call. Use 1 − 0.99ⁿ as a floor, and expect worse when calls share anything.
  4. Run an A/A test first: the same code several times, nothing changed. Its spread is your noise floor.
  5. Don't trust one run's interval for a comparison. It covers sampling noise inside the run, not the machine between runs.
  6. To believe a change, run each version several times, interleaved, and compare the spreads. A difference inside the A/A range is noise.

Practice: tallies, fan-out and noise

Write the lab's arithmetic: a nearest-rank percentile, the average of per-window percentiles (what not to do), a tally with fixed ranges, adding two tallies and reading a percentile from the total, the 1 − 0.99ⁿ formula, the share of pages with a slow call, the A/A spread, and a check of a claimed change against it. The tests use the recorded numbers: the made-up example's 100.9 ms against 1,000 ms, the real test's 58 per-second p99s averaging 159.5 ms, 18.2% predicted for 20 calls against 62.8% measured, and the eight A/A p99s with their 126% spread.

Try it · Practice in PythonRuns in your browser · ⌘/Ctrl + Enter to run
tail_practice.py84 lines

Quiz

Try it · Check your understanding0 of 6 answered
  1. 1.A dashboard shows each second's p99, and you average 58 of them. On the lab's test that gave 159.5 ms. The p99 of all the requests was 925.5 ms. Why so far apart?

  2. 2.Your 40 servers each report a p99 every minute. How do you get the fleet's p99?

  3. 3.A page calls 20 backends at once and waits for all of them. Each backend is slower than its own p99 one time in 100. If the calls are independent, what share of pages includes a slow call?

  4. 4.The lab measured 62.8% of pages with a slow lookup, against 18.2% predicted. What's the most likely reason?

  5. 5.After a change, one run's p99 went from 1.8 ms to 1.6 ms. Each run's 95% interval is about ±0.07 ms. Is it an improvement?

  6. 6.In the A/A test, why did p99.9 range from 4.5 to 98 ms while p50 stayed near 0.4 ms?

Check your understanding

Try each question before opening its answer.

Ten seconds, 100 requests each; in one second, all 100 take 1,000 ms and every other request takes 1 ms. What's the average of the per-second p99s, and what's the p99 of all the requests?

The per-second p99s are nine 1 ms values and one 1,000 ms value, so their average is about 100.9 ms. All together, 100 of 1,000 requests froze, 10 in 100, well past 1 in 100, so the p99 is 1,000 ms. The average gave a quiet second the same weight as the second when everyone waited.

Can the average of per-second p99s ever be too high instead of too low?

Yes. If only 5 requests freeze in one second, that second's own p99 is 1,000 ms, so the average is still about 100.9 ms. But 5 in 1,000 is under 1%, so the p99 of all the requests is 1 ms. The average of percentiles isn't a percentile of anything, so it can miss in either direction.

Why can histograms be combined when percentiles can't?

A histogram is a count per latency range, and counts add. Adding the histograms of 58 seconds, range by range, gives exactly the histogram of all their requests, and the p99 read from it (925.7 ms) matched the exact p99 (925.5 ms) within the range width. A percentile is a position in a sorted list; two positions from two lists don't tell you the position in the merged list.

A page calls 5 backends at once. What does 1 − 0.99⁵ predict, what did the lab measure, and why the gap?

The formula predicts 4.9% of pages with a call slower than a lone call's p99. The lab measured 11.4% (and 62.8% against 18.2% at 20 calls). The lookups shared one app process, its event loop and its database connections, so they queued behind each other and each was slower inside a page than alone. Shared resources make fan-out worse than the formula.

With 20 truly independent calls, which percentile of a single call sets the page's p99?

The slowest of 20 is under t only if all 20 are, so P(slowest ≤ t) = F(t)²⁰. For that to be 0.99, each call needs F(t) = 0.99^(1/20) ≈ 0.9995: the single call's p99.95. A page with many calls lives in each backend's far tail, even before sharing makes it worse.

What's an A/A test, and what did it show about one run's confidence interval?

The same test run several times with nothing changed; the differences between runs are noise. Eight runs gave p99s from 1.58 to 3.81 ms, a range 16× wider than one run's 0.14 ms interval. One run's interval covers which requests happened to land in that run, not the machine changing between runs.

Your change moved p99 from 1.8 to 1.6 ms. What do you do before claiming it?

Measure the noise floor with an A/A test. Here, the unchanged code alone ranged from 1.58 to 3.81 ms, so 1.6 ms is inside the noise. Then run each version several times, taking turns (A, B, A, B…), and compare the two sets of runs. Claim the change only if the difference is clearly larger than the A/A spread.

Next up: Queues, utilization and capacity: how hard can you push one server before it falls over, and can you predict where latency turns up before you measure it?

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.