5. Latency vs Throughput
5.4 Why do averages lie, and what are p50, p95 and p99?
Measuring honestly
On the highway: Most days your commute takes 30 minutes. Once a week a truck breaks down and it takes 2 hours. The "average" says 43 minutes, but you never actually had a 43-minute trip. And the 2-hour day is the one you complain about.
In the system: Request latencies aren't spread evenly. Most requests are fast and a few are very slow, so the mean gets pulled up by the slow ones and describes nobody.
Figure: the mean sits where almost no request actually landed. Percentiles tell you what real users experienced.
Percentiles fix this. Sort all the latencies, then:
- p50 (the median): half of requests were faster than this. The "typical" request.
- p95: 95% were faster; 1 in 20 was slower.
- p99: 99% were faster; 1 in 100 was slower.
- p99.9: 1 in 1,000 was slower.
Why p99 matters more than it sounds: a user loading one page may make dozens of requests, and a busy site serves millions of requests a day. If p99 is 2 seconds, then 1% of requests, possibly tens of thousands a day, take 2 seconds or more. Often the slowest requests belong to your most valuable users: the ones with the biggest carts and the longest order history.
Two rules when you use percentiles:
- Always say the window: "p99 over the last 5 minutes".
- Never average percentiles. The p99 of the whole fleet is not the average of each server's p99 (the Level 4 bug in this chapter shows why). Combine the raw measurements, or histograms, then take the percentile.
Memory code: "The average describes nobody. Ask p50 how it usually goes, and p99 who's suffering."