Sub topic 3
Why percentiles beat averages
This chapter assumes you can read short Python programs, know the request/response model, and have seen backend parts such as load balancers, services and databases. It covers how to describe latency as a distribution, and why targets should be written as percentiles.
Latency is a distribution
Latency is the time a request takes from start to finish. Two requests to the same endpoint rarely take the same time. A cache hit, a cold cache, a garbage-collection pause and a retried network call all give different results. So a service has a distribution of latencies, not one latency.
A single average hides the shape of that distribution. A few slow requests pull the average up, while most requests stay fast. The average then describes neither group.
The mean misleads
Take ten requests measured in milliseconds: 12, 15, 14, 13, 16, 18, 20, 22, 250 and 900. Eight of them finish in 22 ms or less. Two are very slow. The mean is 128 ms, which no typical user experienced. It is also far too low for the two users who waited 250 ms and 900 ms.
Percentiles
A percentile is the value below which a given share of samples fall. The p50 (median) is the latency that half of requests beat. The p99 is the latency that 99% of requests beat, so 1 in 100 requests is slower.
The nearest-rank method sorts the samples and takes the value at position ceil(p / 100 * n).
import math
def percentile(samples, p):
ordered = sorted(samples)
rank = math.ceil(p / 100 * len(ordered))
return ordered[rank - 1]
latencies_ms = [12, 15, 14, 13, 16, 18, 20, 22, 250, 900]
print(percentile(latencies_ms, 50)) # 16
print(percentile(latencies_ms, 99)) # 900
print(sum(latencies_ms) / len(latencies_ms)) # 128.0The median of 16 ms describes the typical request. The p99 of 900 ms describes the worst experience. The mean of 128 ms describes neither.
With only ten samples, p99 is just the maximum. Real percentile estimates need many samples. For p99 you want at least a few thousand requests in the window, and for p99.9 many more.
Why the tail matters
The tail is the slowest few percent of requests. It matters for three reasons.
- Users notice it. A page that is fast nine times and slow on the tenth feels unreliable.
- Heavy users hit it more often. A user who makes 100 requests in a session will probably meet at least one p99 request. The chance is 1 - 0.99^100, about 63%.
- Slow requests are often the valuable ones. Large carts, big accounts and complex queries tend to be slower, and they often belong to your most important customers.
Tail latency also grows when a request depends on several backends. A later chapter on fan-out covers that.
Which percentiles to use
| Percentile | What it tells you | Typical use |
|---|---|---|
| p50 | The typical request | Day-to-day experience |
| p90 | Where slowness starts | Early warning |
| p99 | The tail most users meet now and then | Most latency targets |
| p99.9 | The extreme tail | Strict systems, large fan-out |
Pick the percentile from the requirement. An interactive page might use p95 or p99. A background job might care only about p50 and throughput.
Common errors
- Averaging percentiles. The mean of two hosts' p99 values is not the p99 of the combined traffic. Merge histograms or raw samples first, then compute the percentile.
- Reporting only the mean or only p50. Both can look healthy while the tail is failing.
- Using too few samples. A p99 computed from 50 requests is unstable.
- Measuring in the wrong place. Server-side timing leaves out network time and queueing before the request reaches your code. Measure at the point the user or caller sees.
- Dropping slow or failed requests. A timeout that is not recorded makes the tail look better than it is.
Using percentiles in a target
A good latency target names the percentile, the threshold and the window. For example: "99% of checkout requests complete within 300 ms, measured over 30 days." Later chapters turn this into SLIs and SLOs and split the 300 ms across components.
Exercise
A service handled these latencies in milliseconds: 10, 11, 12, 12, 13, 14, 15, 16, 18, 400. Using the nearest-rank method, what are the p50 and p90, and what is the mean? Which of the three best describes the slowest request a user is likely to see?
Show answer
p50 is the 5th of 10 sorted values, which is 13 ms. p90 is the 9th value, which is 18 ms. The mean is (10+11+12+12+13+14+15+16+18+400)/10 = 52.1 ms. None describes the 400 ms outlier. With only 10 samples, p90 does not reach it, and p99 would equal the maximum of 400 ms. The mean is distorted by the outlier and matches no real request. To see the tail you need a higher percentile and more samples.