Someone asks for the p95 latency of a service running on forty nodes. You have a p95 from each node. The tempting move is to average those forty numbers. Do not do this. The average of percentiles is not the percentile of the whole — it can be off by a wide margin, and it is wrong in the direction that hurts, understating your tail during exactly the skewed traffic that produces a bad tail.
The other tempting move is to keep every raw sample and compute the true percentile on demand. That is correct and completely impractical at any real volume. At a few million datapoints a second, storing raw samples long enough to query them is a data-warehouse bill with extra steps.
Histograms are the answer, and they are boring
The standard solution is to not keep the samples and not average the percentiles, but to keep a histogram: a set of buckets, each counting how many samples fell into a latency range. Histograms have the one property that makes distributed aggregation possible — they add. The histogram for forty nodes is the element-wise sum of forty histograms. From the merged histogram you interpolate any percentile you like, after the fact, correctly.
This is why OpenTelemetry’s explicit-bucket and exponential histograms exist, and why Prometheus histograms work the way they do. We lean on them hard.
What we do at the edge
When a datapoint arrives as a histogram, we keep it as one. When it arrives as raw observations (a StatsD timer, say), we fold it into an exponential histogram at the POP before anything leaves the edge:
le=5ms ████ 412
le=10ms ████████████ 1180
le=25ms ██████████████████ 1795
le=50ms ████████ 840
le=100ms ███ 300
le=250ms █ 95
le=+Inf ▏ 12
Exponential (base-2-ish) bucketing matters here: latency spans microseconds to seconds, and fixed-width buckets either waste resolution at the bottom or lose the tail at the top. Exponential buckets give roughly constant relative error across the whole range, which is what you want when the interesting values are the big ones.
Merging is then just addition, and a percentile is a walk through the cumulative counts:
def quantile(buckets, q):
total = sum(c for _, c in buckets)
target, seen = q * total, 0
for upper, count in buckets: # ascending bucket bounds
seen += count
if seen >= target:
return upper # interpolate within the bucket for more precision
return buckets[-1][0]
The honest caveat
A histogram percentile is an estimate, bounded by your bucket resolution. If you need the exact 95th sample, a histogram will not give it to you. But “exact” is usually a fiction anyway — you are sampling a non-stationary process and calling a snapshot the truth. What you actually want is a percentile that is correctly aggregated across your fleet and cheap enough to keep, with a known, small error. Histograms give you both.
So when our Query API returns a p95, it is reading a merged histogram, not a stored pile of samples and not an average of averages. It is the boring answer. It is also the right one.