Percentiles: why the average hides the problem
You will see p50, p95 and p99 in almost every exercise on this site. They are a way of describing how slow the slow requests are. This lesson works through one small example by hand so the idea sticks.
Latency
Latency is how long a request takes: the time from asking for something to getting the answer. It is usually measured in milliseconds, written ms. A thousand milliseconds make one second. A page that answers in 100 ms feels instant. One that takes 3,000 ms feels broken.
Why not the average?
Suppose ten people load a page. Nine wait 100 ms. One waits 5,000 ms. The average is 590 ms. Nobody waited 590 ms. The number describes none of them: it is too slow for the nine and far too kind to the one. An average hides exactly the experience you most need to know about.
What a percentile is
Line all the requests up from fastest to slowest. A percentile is the time of the request at a chosen point along that line.
- p50: the request in the middle. Half were faster, half slower. Also called the median. It tells you what a typical visit feels like.
- p95: 95 of every 100 requests were at least this fast. The slowest 5 took longer.
- p99: 99 of every 100 were at least this fast. The slowest 1 in 100 took longer.
So "p99 is 2 seconds" means: out of every hundred requests, ninety-nine finished within two seconds, and one took longer.
Working one out by hand
Here are ten request times in milliseconds, already sorted from fastest to slowest:
position: 1 2 3 4 5 6 7 8 9 10 time (ms): 80 90 95 100 100 110 120 150 400 2000
A simple rule that works everywhere: multiply the percentile by the number of requests, divide by 100, and round up. That gives the position to read.
- p50: 50 × 10 ÷ 100 = 5. The 5th value is 100 ms.
- p90: 90 × 10 ÷ 100 = 9. The 9th value is 400 ms.
- p99: 99 × 10 ÷ 100 = 9.9, rounded up to 10. The 10th value is 2,000 ms.
The average of these ten is 324.5 ms. Compare that with what the percentiles say: a typical visit takes 100 ms, and the worst one in ten takes at least four times as long. That is a much more useful picture.
Why the slow few matter
One in a hundred sounds rare. But a busy service handles thousands of requests every second, so one in a hundred is a great many people. And loading a single page often needs twenty or more requests behind the scenes. If each has a one-in-a-hundred chance of being slow, roughly one page in five hits at least one slow request. The slow tail is what many users actually feel.
Two mistakes to avoid
- Averaging percentiles. If server A has a p99 of 1 second and server B has a p99 of 3 seconds, the p99 of both together is not 2 seconds. To get it you must go back to all the individual request times, combine them and sort again.
- Trusting a percentile from very few requests. With ten requests, p99 is simply the slowest one. Say how many requests a figure is based on.