Learning path: Serve a Model in Production: Docker, Latency and Scaling · Step 6 of 9
Latency Math: p50, p95, p99 and Why the Tail Reaches Your Users
What p50, p95 and p99 really mean, why an average of 109 ms can describe nobody, why a p95 needs its load beside it, and why a page that makes ten calls hits a slow one about 40% of the time.
What you will learn
- Read p50, p95 and p99 as positions in a sorted list of request times
- Explain why an average latency can describe no real request
- Always report a percentile together with the load it was measured at
- Work out tail amplification with 1 - 0.95^n
Before you start
- A Production FastAPI for an ML Model
- Basic NumPy
Our rain API is up and it answers correctly. Now we want to say how fast it is. That sounds easy, until you try to put it in one number.
Same requests, two answers
Here's one load test on the rain API, with one caller sending 640 requests, every one of them successful. Ask for a typical request and it's about 17.6 ms. That's p50. Ask for a slow one and it's about 28.1 ms. That's p95. And 32 of these requests took even longer than that.
So which number is the speed of our API? Both are true. They answer different questions.
A hundred people at a bakery
Think of a bakery on a busy morning, with a hundred customers. Each waits a different time. Line them all up, shortest wait at the front, longest at the back.
- Walk to customer 50 and ask how long they waited. That's p50. Half waited less, half waited more.
- Customer 95 is p95. Only five people had it worse.
- Customer 99 is p99. Just one person had it worse.
A percentile is only a position in a sorted line, nothing more.
The average describes nobody
Why not just take the average? Here's a small example, with numbers for illustration only. Ten requests: nine take 10 ms, and one gets stuck and takes 1,000 ms.
The average is (9 × 10 + 1,000) / 10 = 109 ms. Now look at the list. Not one request took 109 ms. The nine quick ones were far quicker, and the slow one far slower. Sorted, p50 is 10 ms, and the slow one sits at the end where you can see it.
That's why we quote percentiles. Our load test doesn't even report an average.
Percentiles in two lines
In loadtest.py, every request's time goes into one array, ms: 640 values at one caller. Then:
"p50": float(np.percentile(ms, 50)),
"p95": float(np.percentile(ms, 95)),
np.percentile works on the sorted values. With 50 it walks to the halfway point: 17.6. With 95 it walks 95% of the way: 28.1. (The full load test, with its workers, is in Measuring Latency with OpenTelemetry and Locust.)
This script stops at p95. Change the 95 to 99 and you'd get p99, but 1% of 640 is about six requests, so that number would rest on just six values. The further out you go, the more requests you need before you can trust the number.
A percentile needs its load
The row the load test returns keeps the concurrency, how many callers were sending at once, right beside p50 and p95. Here's the same API on the same machine (local CPU, one uvicorn worker, load test on the same machine):
| Callers at once | p50 (ms) | p95 (ms) | Errors |
|---|---|---|---|
| 1 | 17.6 | 28.1 | 0 |
| 2 | 32.2 | 49.3 | 0 |
| 4 | 60.2 | 86.1 | 0 |
| 8 | 123.7 | 171.6 | 0 |
| 16 | 270.8 | 356.2 | 0 |
| 32 | 526.7 | 717.2 | 0 |
| 64 | 1,017.3 | 1,243.6 | 0 |
So "p95 is 28 ms" is only half a sentence. p95 at how many callers? Always say the load along with the number. (What this table means for how many copies of the API you need is the subject of Sizing and Autoscaling an Inference Service.)
One page, ten calls
Now the part that surprises people. Say one page on a weather site shows ten towns, and it calls our API ten times at once (an illustration). Nine answers come back quickly. The tenth is one of the slow ones, and the page is still waiting on it. The page is as slow as its slowest call.
That's the tail, and that's what reaches the user. Five percent sounds rare for one call, but a user isn't making just one call.
Tail amplification
A call is slow 5% of the time, so it's fast 95% of the time. For two calls to both be fast, that's 0.95 × 0.95. So the chance that a page with n calls hits at least one slow call is:
1 - 0.95^n
| Calls per page | Slow beyond p95 | Slow beyond p99 (1 - 0.99^n) |
|---|---|---|
| 2 | about 10% | |
| 5 | about 23% | |
| 10 | about 40% | about 10% |
| 20 | about 64% | |
| 100 | about 63% |
This is arithmetic, not a measurement. It assumes each call is slow independently, and in real systems slow moments often come together. Treat it as a way of thinking. The idea holds: the more calls behind one page, the more the tail becomes what people see.
Common mistakes
- Quoting an average. One stuck request drags it to a value no request actually had.
- Quoting a percentile with no load. The same API went from 28.1 ms to 1,243.6 ms at p95 as callers grew from 1 to 64.
- Trusting p99 from a few hundred requests. At 640 requests it rests on about six values.
- Judging a page by one call's p95. A page with many calls feels the tail much more often.
Quick recap
| Idea | Real number |
|---|---|
| A percentile is a position in a sorted list | one caller: p50 17.6 ms, p95 28.1 ms |
| Averages mislead | nine at 10 ms and one at 1,000 ms average 109 ms (illustration) |
| Say the load | 64 callers: p95 1,243.6 ms, same API |
| The tail adds up | 10 calls per page: about 40% hit a slow one (calculated) |
Try it yourself
Add a p99 line to loadtest.py next to the p95 line, and see how far it sits from p95. Then run more requests and see whether it settles.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →Containerising a Model API: A Dockerfile for FastAPI and scikit-learn, Line by Line
Next →Measuring Latency of an ML API: Timing Inside with OpenTelemetry, Outside with a Load Test and Locust
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)Related articles
Python Virtual Environments: One Project, One Toolbox
Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.
Git and GitHub Fundamentals: Commits, Branches, Push and Pull
The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.
Testing a FastAPI App with pytest: Three Small Tests
A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.
Config and Logging Instead of Hard-Coded Values in ML Training
Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.