TExcelTAD
Home › Site Reliability Engineering: SLOs, Incidents & Chaos › Latency SLOs from Histograms

Latency SLOs from Histograms

A latency SLO says "99% of valid requests finish within 800 ms". In Prometheus the data for it comes from a histogram: a set of counters …_bucket{le="X"} that count requests that took at most X seconds, plus …_count for all requests. Because buckets are cumulative, the good-event count for the SLO is simply the bucket at the threshold:

latency SLI = rate(duration_bucket{le="0.8"}) / rate(duration_count)
bad ratio   = 1 − latency SLI                      (feeds the same burn-rate machinery as errors)

No percentile is computed, nothing is estimated: as long as the threshold is a bucket boundary, the SLI is exact. That's the main reason SRE teams state latency SLOs as threshold ratios rather than percentiles.

Rules and a test

The recording rule produces the bad ratio (slow requests per request), so burn rate is again ratio / (1 − SLO). The test simulates 1,000 requests a minute where normally 99.5% finish within 0.8 s; at minute 30 the payment provider slows down and only 90% do. It also asks for histogram_quantile(0.99, …), the p99 estimate a dashboard would show:

bash · try it yourself
cat > latency-rules.yml <<'YML'
groups:
  - name: slo-checkout-latency
    rules:
      - record: job:slo_slow_requests_per_request:ratio_rate5m
        expr: |
          1 - (
            sum by (job) (rate(http_request_duration_seconds_bucket{job="checkout",le="0.8"}[5m]))
            /
            sum by (job) (rate(http_request_duration_seconds_count{job="checkout"}[5m]))
          )
YML
promtool check rules latency-rules.yml
cat > latency-test.yml <<'YML'
rule_files: [latency-rules.yml]
evaluation_interval: 1m
tests:
  - name: payment provider slows down at 30m
    interval: 1m
    input_series:
      # cumulative buckets: each minute 1000 requests; normally 995 finish within 0.8 s
      - series: 'http_request_duration_seconds_bucket{job="checkout",le="0.25"}'
        values: '0+850x30 25800+300x29'
      - series: 'http_request_duration_seconds_bucket{job="checkout",le="0.5"}'
        values: '0+960x30 29400+600x29'
      - series: 'http_request_duration_seconds_bucket{job="checkout",le="0.8"}'
        values: '0+995x30 30750+900x29'
      - series: 'http_request_duration_seconds_bucket{job="checkout",le="1.5"}'
        values: '0+999x30 30960+990x29'
      - series: 'http_request_duration_seconds_bucket{job="checkout",le="+Inf"}'
        values: '0+1000x60'
      - series: 'http_request_duration_seconds_count{job="checkout"}'
        values: '0+1000x60'
    promql_expr_test:
      # burn rate of a "99% under 0.8 s" SLO: 0.5 before, 10 during the slowdown
      - expr: round(job:slo_slow_requests_per_request:ratio_rate5m / 0.01, 0.1)
        eval_time: 20m
        exp_samples: [{labels: '{job="checkout"}', value: 0.5}]
      - expr: round(job:slo_slow_requests_per_request:ratio_rate5m / 0.01, 0.1)
        eval_time: 50m
        exp_samples: [{labels: '{job="checkout"}', value: 10}]
      # the p99 estimate, for comparison
      - expr: round(histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket[5m]))), 0.001)
        eval_time: 20m
        exp_samples: [{labels: '{}', value: 0.757}]
      - expr: round(histogram_quantile(0.99, sum by (le) (rate(http_request_duration_seconds_bucket[5m]))), 0.001)
        eval_time: 50m
        exp_samples: [{labels: '{}', value: 1.5}]
YML
promtool test rules latency-test.yml
Checking latency-rules.yml
  SUCCESS: 1 rules found

  SUCCESS

The burn rate of the latency SLO goes from 0.5 to 10, which would trip the 6-hour/30-minute page after a while and the 1-hour/5-minute page not at all (10 < 14.4): exactly the slow-but-serious burn those rules exist for.

The p99 numbers deserve a second look. Before the slowdown the estimate is 0.757 s, but no request was measured at 0.757 s: histogram_quantile knows only that the 99th percentile lies in the 0.5–0.8 s bucket and interpolates linearly inside it. During the slowdown it says exactly 1.5, the upper edge of a bucket, because that's where the interpolation lands. Percentiles from classic histograms are estimates whose precision depends on the bucket layout.

Thresholds between buckets

If the SLO threshold is 0.75 s but the buckets are 0.5 and 1, there is no counter for "≤ 0.75 s", and the SLI must be estimated by interpolation too. The model draws 100,000 seeded latencies, computes the true share under 0.75 s, and compares it with what a histogram with typical buckets would estimate:

python · try it yourself
import random

rng = random.Random(42)
latencies = [rng.lognormvariate(-1.6, 0.9) for _ in range(100_000)]   # seconds, long right tail
BUCKETS = [0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]

def cumulative(bounds):
    return [sum(1 for x in latencies if x <= b) for b in bounds]

def estimate_share(threshold, bounds):
    counts = cumulative(bounds)
    lower, lower_count = 0.0, 0
    for b, c in zip(bounds, counts):
        if threshold <= b:                          # interpolate inside this bucket
            frac = (threshold - lower) / (b - lower)
            return (lower_count + frac * (c - lower_count)) / len(latencies)
        lower, lower_count = b, c
    return 1.0

for threshold in (0.5, 0.75):
    true = sum(1 for x in latencies if x <= threshold) / len(latencies)
    est = estimate_share(threshold, BUCKETS)
    print(f"threshold {threshold} s: true share {true:.3%}  histogram estimate {est:.3%}  "
          f"bad-ratio error {((1 - est) - (1 - true)) / (1 - true):+.0%}")
est = estimate_share(0.75, BUCKETS[:4] + [0.75] + BUCKETS[4:])
print(f"with a 0.75 bucket added: estimate {est:.3%} (exact)")
threshold 0.5 s: true share 84.302%  histogram estimate 84.302%  bad-ratio error +0%
threshold 0.75 s: true share 92.696%  histogram estimate 90.285%  bad-ratio error +33%
with a 0.75 bucket added: estimate 92.696% (exact)

At a bucket boundary the estimate is exact. Between buckets, linear interpolation assumes requests are spread evenly inside the bucket, which a long-tailed latency distribution is not: here the bad ratio comes out noticeably wrong, and burn rates computed from it are off by the same factor. The fix is cheap: add a bucket at every SLO threshold in the instrumentation, and keep thresholds on those buckets when SLOs change.

Native histograms

Prometheus 3 also supports native histograms, which use many exponential buckets chosen automatically and store them in one series. They make percentiles much more precise, and histogram_fraction(0, 0.8, …) returns the share of observations up to 0.8 s directly. They need instrumentation and scrape settings that support them; see Observability and the Prometheus documentation before switching an SLO to them.

Latency SLIs: what to measure

  • Measure where users wait: at the load balancer or gateway, not inside one handler.
  • Count only successful requests as good (Computing SLIs from Request Logs): either join the status into the histogram's labels, or make the error SLO and the latency SLO separate budgets.
  • One threshold per journey is usually enough; a second, looser one (99.9% under 3 s) catches the extreme tail.

Test yourself

Why is a latency SLI exact when the threshold is a bucket boundary?

  1. Because Prometheus stores every request
  2. The bucket counter at that boundary is exactly the number of requests at or below the threshold
  3. Because histogram_quantile is exact
  4. It isn't exact

The session's p99 estimate before the slowdown was 0.757 s. What does that number mean?

  1. A request took exactly 0.757 s
  2. The 99th percentile lies in the 0.5-0.8 s bucket; 0.757 is a linear interpolation inside it
  3. The SLO threshold
  4. The average latency

Your SLO threshold is 300 ms, but the histogram buckets are 0.25 and 0.5. What should you do?

  1. Use histogram_quantile
  2. Add a 0.3 bucket to the instrumentation
  3. Use the 0.5 bucket and call it 300 ms
  4. Average the two buckets