Histograms & Percentiles
histogram_quantile, aggregating buckets, and what averages hide
Latency is the signal users feel most directly, and it is almost never normally distributed. Most requests are fast; a few are very slow. The average hides those slow ones. Percentiles show them.
Why averages lie
Suppose 99 requests take 10 ms and 1 takes 5 seconds. The average is about 60 ms, which looks fine. But one user in a hundred waited 5 seconds, and a page that makes 50 backend calls hits that slow path on most loads.
# mean latency per route
sum by (route) (rate(http_request_duration_seconds_sum[5m]))
/ sum by (route) (rate(http_request_duration_seconds_count[5m]))
# p99 latency per route
histogram_quantile(0.99,
sum by (route, le) (rate(http_request_duration_seconds_bucket[5m])))Graph both for /api/search, which the demo app gives a long-tailed
latency distribution. The mean sits low and steady; p99 is several times
higher and much noisier. The Demo App · RED dashboard puts these side
by side.
How histogram_quantile works
histogram_quantile(φ, buckets) needs, for each group, the set of
cumulative bucket counts with their le labels. It then:
- Finds the total count (the
+Infbucket). - Computes the target rank: φ × total. For p99 of 1000 requests, rank 990.
- Walks the buckets to find the first one whose cumulative count reaches that rank.
- Linearly interpolates within that bucket, assuming observations are spread evenly between its lower and upper bound.
le="0.05" 950 # 950 requests took <= 50ms
le="0.1" 985
le="0.25" 998 # rank 990 falls in (0.1, 0.25]
le="+Inf" 1000
# 990 is 5/13 of the way from 985 to 998, so
# p99 ≈ 0.1 + (5/13) * (0.25 - 0.1) ≈ 0.158sThe real p99 could be anywhere between 100 and 250 ms. The answer is only as precise as your buckets, which is why bucket boundaries should sit around the latencies you care about.
If the quantile falls in the +Inf bucket, meaning more than 1% of
requests are slower than your largest bucket, histogram_quantile
returns the largest finite boundary. With a top bucket of 5s, a p99
reported as exactly 5 means "5 seconds or worse, could be 60." Make
sure your largest bucket is above any timeout.
The rule: aggregate buckets, keep le
The pattern is always the same: rate the buckets, sum them by
le plus whatever you want to group by, then take the quantile.
# p99 across all instances and routes
histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket[5m])))
# p99 per route
histogram_quantile(0.99, sum by (route, le) (rate(http_request_duration_seconds_bucket[5m])))
# WRONG: dropped le, so there are no buckets left to compute from
histogram_quantile(0.99, sum by (route) (rate(http_request_duration_seconds_bucket[5m])))
# WRONG: averaging per-instance p99s is not the fleet p99
avg(histogram_quantile(0.99, rate(http_request_duration_seconds_bucket[5m])))This is the real power of histograms: because buckets are counters, you can add them across any number of instances and get a correct fleet-wide percentile.
Exact SLO measurements
Percentiles are estimates. But if your threshold is a bucket boundary, "what fraction of requests were fast enough" is exact:
# share of /api/orders requests that finished within 100ms
sum(rate(http_request_duration_seconds_bucket{route="/api/orders", le="0.1"}[5m]))
/
sum(rate(http_request_duration_seconds_count{route="/api/orders"}[5m]))This is how latency SLOs should be measured: "99% of requests under 100 ms" is a ratio of two counters, not a percentile. It also aggregates correctly over any time window, which percentiles don't.
Prefer "fraction under threshold" for alerts and SLOs, and percentiles for dashboards and investigation. A p99 alert flaps when traffic is low (with 20 requests, p99 is basically your single slowest request). A ratio of good to total events is stable.
Heatmaps
A percentile line throws away the shape of the distribution. A heatmap shows all of it: time on the x-axis, buckets on the y-axis, and colour for how many requests landed in each bucket.
# in Grafana, Format = Heatmap, panel type = Heatmap
sum by (le) (increase(http_request_duration_seconds_bucket{job="demo-app"}[$__rate_interval]))Heatmaps make bimodal latency obvious, such as a cache-hit band at 5 ms and a cache-miss band at 200 ms. A p50 line would sit between the two bands, at a latency no request actually had.
Apdex-style scores
A single "user happiness" number from two thresholds: requests under T are satisfied, under 4T tolerated, the rest frustrated:
(
sum(rate(http_request_duration_seconds_bucket{le="0.1"}[5m]))
+ sum(rate(http_request_duration_seconds_bucket{le="0.5"}[5m])) # 0.5 ≈ 4T bucket
) / 2 / sum(rate(http_request_duration_seconds_count[5m]))The satisfied requests are counted in both buckets, so they count as 1 after dividing by 2; tolerated ones count once, so they're worth 0.5.
Lab
- Graph p50, p90, and p99 for
/api/searchon one panel. - Inject latency:
curl -X POST 'localhost:8080/admin/chaos?extra_latency_ms=300'. - Watch p50 and p99 move. Then graph the mean. Which reacted most?
- Graph the fraction of
/api/ordersrequests under 100 ms. It should collapse to near 0 with 300 ms of extra latency. - Open the Latency distribution heatmap in Grafana and watch the band shift up.
- Reset:
curl -X POST 'localhost:8080/admin/chaos?extra_latency_ms=0'.