Video in progress

Why should you watch latency with p95 or p99 rather than the average?

The average "melts" slow requests into the fast ones. p99 says "1 percent of requests are slower than this" and points at exactly the users who complain.

📖 Answer

An example: out of 100 requests, 98 take 20 ms and 2 take 5 seconds.

  • the average: (98·20 + 2·5000) / 100 ≈ 120 ms, "good";
  • p99: 5 seconds, and somebody closed the page and left.

Now, those 2 percent mean thousands of people every day. On top of that, opening one page sends 10–20 requests to the backend, so the chance of a single user hitting at least one slow request is much higher than 1%.

How to read percentiles:

  • p50 (the median): the typical experience;
  • p95/p99: the "tail" (tail latency), where GC pauses, lock waits and slow SQL show up;
  • max: a single worst case, very noisy.

Turning on the histogram in Micrometer:

management:
  metrics:
    distribution:
      percentiles-histogram:
        http.server.requests: true

One caveat: you can't average the p99 values of separate instances, it's mathematically wrong. So you have to aggregate the histogram buckets and compute the quantile on the Prometheus side.