Prometheus Histograms and Quantile Estimation
A Prometheus-style histogram records how many observations fall below configured boundaries, allowing a latency distribution to be summarized as counters. Prometheus can use those counters to estimate values such as the 95th-percentile latency without storing every individual request time.
The problem histograms solve
Suppose an HTTP service logs every request duration. To answer “what was the p95 latency over the last five minutes?”, you could retain all durations and sort them. That is expensive at high request rates, and aggregating those raw samples across many processes is awkward.
A histogram trades exactness for a compact, mergeable summary. Each process increments counters as observations arrive. Prometheus periodically scrapes those counters, and its query language can combine the counters from many processes before estimating a quantile.
A quantile is a value below which a specified fraction of observations falls. The p95, or 0.95 quantile, is the estimated value below which 95% of observations fall.
You have probably encountered histogram data in metric names ending in _bucket, or seen a query containing histogram_quantile(0.95, ...). Those names and queries are the visible parts of this mechanism.
What the bucket counters mean
Imagine a request-duration histogram with boundaries at 100 ms, 300 ms, 1 second, and 3 seconds. A Prometheus client exposes a counter for each boundary:
http_request_duration_seconds_bucket{le="0.1"} 4200
http_request_duration_seconds_bucket{le="0.3"} 9100
http_request_duration_seconds_bucket{le="1"} 9870
http_request_duration_seconds_bucket{le="3"} 9995
http_request_duration_seconds_bucket{le="+Inf"} 10000
The le label means “less than or equal to.” These values are cumulative, not counts for separate intervals:
- 4,200 requests took at most 100 ms.
- 9,100 took at most 300 ms, including the 4,200 faster requests.
- 770 requests took between 300 ms and 1 second:
9870 - 9100. - Five requests took longer than 3 seconds:
10000 - 9995.
The +Inf bucket represents every observation. It supplies the total count and is required for the usual quantile calculation. Histograms also expose a _count metric with that total, and commonly a _sum metric containing the sum of observed values. The sum can produce an average, but it cannot reveal tail behavior: two datasets can have the same average and very different p95 latencies.
The bucket metrics are counters because observations only add to them. Across a time window, Prometheus normally applies rate or increase to those counters, rather than using their lifetime totals directly. Counter-aware functions also handle process restarts and counter resets.
Turning buckets into a quantile
To estimate the p95, first determine the target rank. If 10,000 requests occurred, rank 95% of the way through the observations is roughly request 9,500 when sorted by duration.
The cumulative bucket counts above show that:
- 9,500 is greater than 9,100, so the p95 is not below 300 ms.
- 9,500 is at most 9,870, so it lies in the 300 ms–1 second bucket.
Prometheus does not know the individual durations inside that bucket. It assumes they are distributed uniformly between the bucket's lower and upper boundaries. There are 770 observations in the bucket, and the target is 400 observations past the previous cumulative count of 9,100. The estimated position is therefore:
300 ms + (400 / 770) × (1,000 ms - 300 ms)
≈ 664 ms
In general, for a target quantile q:
rank = q × total_count
estimate = lower_bound
+ (rank - previous_cumulative_count) / bucket_count
× (upper_bound - lower_bound)
That formula describes the usual interior-bucket case. The implementation also handles the lowest bucket and the final +Inf bucket using boundary rules; an infinite upper boundary cannot itself be used as a useful duration estimate. The important point is that the result is an interpolation, not a measurement of an actual request.
A typical PromQL query for a service-wide p95 over five minutes is:
histogram_quantile(
0.95,
sum by (le) (
rate(http_request_duration_seconds_bucket[5m])
)
)
rate turns each cumulative bucket counter into an observation rate over the window. sum by (le) merges instances while retaining the bucket boundary label. Dropping le before calling histogram_quantile removes the information needed to reconstruct the distribution. If you need a separate quantile per route, include the route label in the grouping, for example sum by (route, le).
Why bucket boundaries change the answer
A histogram does not preserve the exact values it observed. Its boundaries determine how much information survives. If the p95 falls in a narrow bucket, interpolation has little room to be wrong. If it falls in a broad bucket such as 300 ms–1 second, the estimate could differ substantially from the real p95 while still matching every exported counter.
Boundaries should therefore reflect the decisions you want to make. For request latency, use finer buckets around objectives such as 100 ms, 300 ms, or 1 second, and wider buckets where precision matters less. Include enough high-end buckets to distinguish ordinary slow requests from severe failures; if many observations land only in +Inf, the upper tail is effectively unknown.
There is a cost: every bucket is another time series for every combination of labels such as service, route, and status code. More boundaries improve resolution but increase memory, storage, and query work. Histograms are consequently a deliberate approximation—not a promise that a displayed p95 is exact. The bucket layout is part of the measurement design, not merely a presentation choice.