Try
↑ ↓ move↵ openesc close

Interview prep / Percentiles

Junior · Percentiles · System design interview question

How does a monitoring system store a request's latency in a histogram, and how does it later answer "what is the p95"?

Sketch your own answer first, then compare it with the one below.

A strong answer

The histogram is a fixed list of buckets, each covering a range such as 0 to 100 ms or 100 to 250 ms. Recording a request means finding its bucket and adding one to that counter. Nothing about the individual request is kept, so memory stays the same whether the service handles ten requests or ten million.

Prometheus-style histograms store the counts cumulatively: the bucket labelled le="0.25" counts every request at or below 250 ms. To find the p95 of 10,000 requests, look for the first bucket whose cumulative count reaches 9,500. If the 250 ms bucket holds 9,000 and the 500 ms bucket holds 9,900, the p95 lies between 250 and 500 ms. The system then assumes requests are spread evenly inside that bucket and interpolates.

The trade-off follows directly. The answer is an estimate, and its error is bounded by the width of the bucket that holds the percentile. A coarse bucket layout makes the dashboard cheap and the p95 blurry. A finer one costs one extra time series per bucket, per label combination.

Sources

Learn it by running it

The percentiles and why averages lie lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms