Part 01 · Measuring Performance

Percentiles: p50, p95, p99

PercentilesTail latencyHistograms

What each percentile tells you, why p99 matters when a page makes many calls, and how histograms store them.

⏱ 9 min · Station 3 of 95

The problem

The product team reports "average page time: 3 ms, all good". Yet a few customers keep complaining that pages "hang". Both are true. Most requests are fast; a small share is several times slower, and some customers hit that share on almost every visit. Before the Eid sale you need a number that describes what the unlucky users see, and you need to know how far to trust the number your monitoring gives you.

The idea

A percentile answers "how fast were the fastest X% of requests?". Sort every request's latency:

  • p50 (median): half the requests were faster. The typical experience.
  • p95: 95% were faster, 1 in 20 was slower.
  • p99: 99% were faster, 1 in 100 was slower. The tail.

Why the tail matters more than it sounds:

  • Users make many requests. A session of 30 page views will very likely include at least one request slower than p95.
  • Pages fan out. If a page waits for 10 backend calls, and each call is slow 1% of the time, the page is slow whenever any call is: 1 − 0.99¹⁰ ≈ 9.6% of pages. A backend's p99 becomes the page's p90.
  • The slowest requests often belong to the best customers: the ones with the biggest carts and longest order histories.

In production, latencies are usually recorded in a histogram: counters for "how many requests took ≤ 5 ms, ≤ 10 ms, ≤ 25 ms...". That's cheap and can be summed across servers, but the exact values are gone. Prometheus estimates a percentile by finding the bucket it falls in and assuming requests are spread evenly inside it. The estimate can only be as precise as the buckets.

How it works

histogram_quantile(0.5, ...) for 18,000 requests:

  1. Sum the bucket counts across all instances and compute the per-second rate over the window.
  2. Find the bucket that contains the 9,000th request (the 50th percentile).
  3. Interpolate linearly between that bucket's lower and upper bound.

With the default buckets {5 ms, 10 ms, 25 ms, ...}, the first bucket covers 0 to 5 ms. Nearly every request of the shop falls in it, so the median is estimated by assuming requests are spread evenly between 0 and 5 ms, even though most are around 2 ms. Add boundaries where the requests actually are (1, 1.5, 2, 3, 4 ms) and the estimate snaps closer.

The same logic applies at the tail: p99 lands in the 10 to 25 ms bucket in both configurations here, and anything inside 10 to 25 ms is a guess.

Predict first

k6 measured the exact median: 2.0 ms. Prometheus estimates it from a histogram whose first bucket is 0 to 5 ms. What does Prometheus report?

Visual

≤2100902-335103-410104-56505-7.56907.5-1017010-1516015-258025-5020latency (ms), one minute at 300 requests/sp50 2 msp95 6 msp99 13-16 msmost requests are about 2 ms: that's p501 in 20 is slower than 6 ms, 1 in 100 slower than ~13 ms: the tail
Where one minute of product page requests fell, with p50, p95 and p99 marked. Most requests take about 2 ms; a long, thin tail stretches past 25 ms.
product pagewaits for all 10call 1call 2call 3call 4call 5call 6call 7call 8call 9call 10p99: 1 call in 100each call is fast 99% of the timebut the page waits for the slowest of 101 - 0.99^10 = 9.6%: about 1 page in 10 is as slow as a call's p99
A page that waits for 10 backend calls is slow whenever any one of them is: with each call slow 1% of the time, about 9.6% of pages are slow.

Common misconception

"p99 is about the 1% of unlucky requests, so it doesn't matter much." One percent of requests is not one percent of users. Every user makes many requests, and pages make many calls. At 300 requests a second, 1% is three slow requests every second, all day.

"The dashboard says p99 = 16 ms, so p99 is 16 ms." It says the 99th percentile falls somewhere in a bucket and interpolates. In our run, the dashboard's p99 was 16.1 ms with default buckets while the exact value was 13 ms. Check where your buckets are before you trust a percentile, especially near an SLO threshold.

Break it, then fix it

Measured on the lab (Intel i3-10110U; the shop and PostgreSQL each on one CPU thread; a busier laptop than in earlier lessons, so the tail is longer). The shop's histogram buckets are set by HIST_BUCKETS:

// ops.go (excerpt)
// buckets are the histogram's upper bounds in seconds. HIST_BUCKETS=fine adds boundaries below
// 10 ms, where most of the shop's requests are, so percentiles there are estimated more precisely.
func buckets() []float64 {
	if os.Getenv("HIST_BUCKETS") == "fine" {
		return []float64{.001, .0015, .002, .003, .004, .005, .0075, .01, .015, .025, .05, .1, .25, .5, 1, 2.5, 5}
	}
	return []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5}
}

buckets.sh runs one minute at 300 requests/s with each bucket set. k6 measures every request exactly; Prometheus estimates from its histogram. At the end it prints how the requests were distributed across the fine buckets:

cd ../../lab
H='http_request_duration_seconds_bucket{route="GET /products/{id}"}'
for b in default fine; do
  CACHE=off HIST_BUCKETS=$b docker compose up -d api >/dev/null 2>&1; sleep 3
  ./k6.sh product.js -e RATE=80 -e DURATION=10s >/dev/null 2>&1
  sleep 60                                  # let the warm-up leave the 1-minute window
  echo "== HIST_BUCKETS=$b: k6 measured every request exactly"
  ./k6.sh product.js -e RATE=300 -e DURATION=60s 2>/dev/null | grep latency
  sleep 6
  for q in 0.5 0.95 0.99; do
    printf "   Prometheus estimate p%-4s %s ms\n" "$(python3 -c "print(int($q*100))")" \
      "$(./prom.sh "1000*histogram_quantile($q, sum by (le) (rate($H[1m])))" | awk '{printf "%.2f", $2}')"
  done
done
echo
echo "== where the requests fell (fine buckets, last minute)"
./prom.sh "sum by (le) (increase($H[1m]))" | grep -v Inf | sed 's/le=//' | sort -g | awk '
  { le=$1; c=$2; n=c-prev; prev=c; printf "   <= %-6s ms %8.0f %s\n", le*1000, n, substr("##################################################", 1, int(n/400)) }'
== HIST_BUCKETS=default: k6 measured every request exactly
latency    avg 2.9 ms  p50 2.0 ms  p95 5.7 ms  p99 13 ms  max 185 ms
   Prometheus estimate p50   2.66 ms
   Prometheus estimate p95   6.09 ms
   Prometheus estimate p99   16.14 ms
== HIST_BUCKETS=fine: k6 measured every request exactly
latency    avg 3.1 ms  p50 2.0 ms  p95 6.1 ms  p99 16 ms  max 159 ms
   Prometheus estimate p50   1.91 ms
   Prometheus estimate p95   6.11 ms
   Prometheus estimate p99   13.07 ms

== where the requests fell (fine buckets, last minute)
   <= 1      ms        0
   <= 1.5    ms        0
   <= 2      ms    10090 #########################
   <= 3      ms     3510 ########
   <= 4      ms     1010 ##
   <= 5      ms      650 #
   <= 7.5    ms      690 #
   <= 10     ms      170
   <= 15     ms      160
   <= 25     ms       80
   <= 50     ms       20
   <= 100    ms        0
   <= 250    ms        0
   <= 500    ms        0
   <= 1000   ms        0
   <= 2500   ms        0
   <= 5000   ms        0 
Exact (k6)Estimate, default bucketsEstimate, fine buckets
p502.0 ms2.66 ms (+33%)1.91 ms (−5%)
p955.7 / 6.1 ms6.09 ms6.11 ms
p9913 / 16 ms16.14 ms13.07 ms

(Each run had its own exact values; compare each estimate with the k6 line above it.)

  • The broken part: the default buckets put the median at 2.66 ms, a third too high, because everything from 0 to 5 ms is one bucket.
  • The fix: buckets where the requests are. With boundaries at 1, 1.5, 2, 3 and 4 ms, the estimate came within 0.1 ms.
  • The tail is still coarse in both: p99 falls in the 10 to 25 ms bucket either way, and the estimates are off by about 20% in opposite directions. If your SLO were "p99 under 15 ms", you'd want a boundary at 15 ms.
  • The distribution: 10,090 requests at 2 ms or less, 3,510 between 2 and 3 ms, and a long thin tail: 250 above 10 ms, 20 above 25 ms. That tail is the "pages hang" complaint.

Engineering insight

  • Put bucket boundaries at your SLO thresholds. If the target is "99% under 300 ms", a boundary at 0.3 s makes "what share is under 300 ms" exact, which is what the SLO actually measures (counts, not interpolated percentiles).
  • Never average percentiles across servers. Sum the histogram buckets across servers, then compute the percentile (the next lesson shows why averaging lies). Histograms can be aggregated; precomputed percentiles can't.
  • More buckets cost more. Each bucket is a time series per label combination. Fine buckets on a high-cardinality metric get expensive fast. Newer Prometheus versions support native histograms with automatic, finer buckets.
  • Report p50, p95 and p99 together, plus the max. p50 says what most people feel, p99 says what the unlucky feel, and max tells you whether something pathological is happening.

Key takeaway

Percentiles describe the experience of everyone, including the unlucky: p50 the typical request, p99 the tail that fan-out and repeat visits make common. Monitoring systems estimate them from histogram buckets: here default buckets put the median a third too high, and buckets placed where the requests are fixed it.

Practice

Objective: feel how fan-out turns a rare slow call into a common slow page.

Challenge: a product page calls 5 services in parallel, and the checkout page calls 20. Each call is slower than 200 ms 1% of the time (its p99), independently. What share of product pages and checkout pages take more than 200 ms? What would each service's p99 have to be for checkout pages to be slow at most 1% of the time?

Expected result: two percentages and a per-call target.

Hint 1

A page is fast only if every call is fast.

Hint 2

P(page fast) = 0.99^N. P(page slow) = 1 − 0.99^N.

Hint 3

For the target, solve (1 − x)^20 = 0.99 for x.

Solution
  • Product page (5 calls): 1 − 0.99⁵ ≈ 4.9% of pages are slower than 200 ms.
  • Checkout (20 calls): 1 − 0.99²⁰ ≈ 18.2%: nearly one checkout in five.
  • Target: (1 − x)²⁰ = 0.99 → x ≈ 0.05%. Each service must be under 200 ms for 99.95% of calls, its p99.95, for checkout to meet a p99.

This is why large companies set backend SLOs at p99.9 or tighter, and why they work on tail latency (hedged requests, timeouts, cutting fan-out) rather than only the median.

Interview check

Q: Your service's p50 is 20 ms and p99 is 2 seconds. What does that tell you, and where would you start?

Model answer

Most requests are fast, but 1 in 100 is 100 times slower: something specific happens to a minority, not a general slowness. Likely causes: requests hitting a cold cache or a slow code path (large accounts, big queries), lock contention, garbage collection pauses, a slow dependency or retries with long timeouts, queueing behind slow requests. Start by finding what the slow requests have in common: traces or logs of requests over 1 s, broken down by endpoint, customer, instance and time.

Q: Why can't you average the p99 of ten servers to get the fleet's p99?

Model answer

Percentiles don't add up. The fleet's p99 depends on the combined distribution: one overloaded server with a 5 s p99 serving 10% of traffic can dominate the fleet's tail, while an average of ten p99s would hide it among nine healthy values. Aggregate the raw histograms (sum the buckets across servers) and compute the percentile from the sum.

Station stamp

Finish the chapter

Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.

Go Pro to earn XP →