Course contents · Lesson 8 of 22

Latency and Percentiles: Why Averages Lie

Metrics · Lesson 8 of 22 · 9 min Engineer

Your dashboard says the average response time is 131 ms, comfortably fast. At the same time, support tickets say the site is "sometimes painfully slow". Both are true. The average blends a thousand quick requests with a few very slow ones, and the slow ones vanish in the blend. This lesson explains latency and percentiles: why averages hide the requests users complain about, and how p50, p95 and p99 bring them back into view.

Mean, median and percentiles

Latency is the time between a request arriving and its response going back: how long a user waits. You measure it for every request, so you quickly have thousands of numbers and need a way to summarise them.

The mean, or average, adds all the times and divides by how many there are. It is easy to compute, but a few very slow requests drag it around. A percentile is the value that a given share of requests stay at or below. The median, written p50, is the middle value: half the requests were faster. p95 is the value that 95 of every 100 requests stayed at or below. So "p99 = 800 ms" means 99 of every 100 requests took 800 ms or less, and 1 in 100 took longer.

To find a percentile by hand, sort the times and count along the list. With 20 requests, p50 is the 10th value and p95 is the 19th, because 95% of 20 is 19. This is the nearest-rank method; tools differ slightly in how they interpolate between values, but with many requests the difference is small.

Flowchart: twenty response times in arrival order are sorted from 42 to 1,250 ms; arrows from the 10th, 19th and 20th sorted values lead to cards reading p50 68 ms, p95 380 ms and max 1,250 ms, next to a card for the mean, 153 ms
The mean, 153 ms, is slower than 17 of the 20 requests yet tells you nothing about the 1,250 ms one; percentiles name both.

Why the slow tail matters

Real latency data rarely looks like a neat bell curve. It is usually a tall heap of fast requests with a thin tail that stretches far to the right. The tail is the slow end of the distribution, and it is where the complaints come from.

Histogram of 1,000 response times, tall at the left and fading into a thin tail past a break in the axis out to 3 seconds, with dashed lines at p50 63 ms, mean 131 ms, p95 400 ms and p99 2.4 s
The mean lands at twice the median because a thin tail of multi-second requests drags it to the right.

In that example of 1,000 requests, the median is 63 ms but the mean is 131 ms, because a few multi-second requests pull it up. p95 is about 400 ms and p99 is 2.4 s. A dashboard showing only the mean would report a healthy service while about 10 of those 1,000 requests took 2.4 seconds or more.

Two panels on the same scale: the mean, 131 ms, a short green bar that looks healthy; the p99, 2.4 s, a bar about 18 times longer, with a 10 by 10 dot grid where one dot is highlighted
Same data, two summaries: the mean says all is well, the p99 says one request in a hundred is painfully slow.

The tail matters even more because one user action rarely makes one request. A single page load can call backend services dozens of times, and the page is only as fast as its slowest call. If each call has a 1% chance of landing in the slow tail, a page with 10 calls hits it about 9.6% of the time (1 − 0.9910), and a page with 100 calls about 63% of the time. Real calls are not fully independent, but the lesson holds: at scale, your p99 is what many users actually feel.

Combining servers: add buckets, not percentiles

You cannot average percentiles. Suppose server A handled 995 requests at about 100 ms and server B handled 5 requests at about 3 s. A's p99 is 100 ms and B's is 3 s, so their average is 1.55 s. Sort all 1,000 requests together, though, and the 990th is still a 100 ms request: the real p99 is 100 ms. The averaged number is not a percentile of anything.

Histograms, covered in the metric types lesson, solve this. Each server exports bucket counts, and counts can be added. In PromQL you first sum the bucket rates from every server, keeping only the le label, then estimate the percentile once from the combined buckets:

histogram_quantile(0.95,
  sum by (le) (rate(http_request_duration_seconds_bucket[5m])))

histogram_quantile() finds the bucket where the 95% mark falls and assumes values are spread evenly inside it, so the result is an estimate whose precision depends on your bucket bounds. Put a bucket boundary exactly at every target you care about, such as 0.3 seconds if you promise 300 ms.

Setting targets and measuring by outcome

A useful latency target names a percentile, a threshold and a time window, for example "p95 under 300 ms over 5 minutes". The percentile says how many users you are protecting, and the threshold says what "fast enough" means for them. Many teams watch p50 for the typical experience and p95 or p99 for the unlucky ones.

With a bucket boundary at 0.3 seconds, you can also measure the exact share of requests that met the threshold, with no estimation at all:

sum(rate(http_request_duration_seconds_bucket{le="0.3"}[5m]))
  /
sum(rate(http_request_duration_seconds_count[5m]))

A result of 0.97 means 97% of requests finished within 300 ms over the last five minutes. That ratio of good requests to all requests is the raw material for the service level objectives covered later in this course.

Finally, measure failed requests separately. Errors are often very fast: a service that rejects requests in 2 ms because its database is down makes the latency graph look better just as users start seeing errors. Add a small, bounded label such as outcome="success" or outcome="error" to the histogram, keep it in the query with sum by (le, outcome), and read each line on its own. This is the latency signal from the golden signals lesson, measured honestly.

Check yourself

A service has p50 = 40 ms and p99 = 1.2 s. What does the p99 tell you that the p50 does not?

That 1 in every 100 requests takes 1.2 s or longer; the p50 only describes the typical request.

Two servers report a p95 of 200 ms and 600 ms. Is the fleet's p95 400 ms?

Not necessarily, because percentiles cannot be averaged; you add up both servers' bucket counts and compute the p95 from the total.

The error rate jumps and, at the same moment, the latency graph improves. What is a likely cause?

Fast failures, such as instant rejections, are mixed into the latency numbers; splitting latency by outcome would show it.