Latency, throughput and percentiles
Overview
“Make it faster” can mean several different things. A system can answer each request quickly but handle few of them, or handle huge volumes while each request takes a while. This lesson gives you the precise vocabulary, which you’ll use in requirements (“p99 under 200 ms”), estimates and trade-off discussions.
Latency, throughput and bandwidth
- Latency is the time one request takes, from the moment it’s sent until the answer arrives. Measured in milliseconds.
- Throughput is how many requests the system completes per unit of time, usually requests per second (RPS) or queries per second (QPS).
- Bandwidth is the maximum amount of data a link can carry, such as 1 Gbit/s. Throughput is what you actually push through it.
These are related but separate. A truck full of hard drives driving across the country has terrible latency (days) and enormous throughput (petabytes). A chat message has tiny throughput needs but must have low latency.
Concurrency and parallelism
- Concurrency: many tasks are in progress at the same time. A single core can be concurrent by switching between tasks whenever one waits for I/O.
- Parallelism: tasks literally run at the same moment on different cores or machines.
Web servers rely heavily on concurrency: most requests spend their time waiting for a database or another service, so one server can keep hundreds of them in progress. CPU-heavy work, such as encoding video, needs parallelism: more cores or more machines.
Little’s Law
A simple rule connects the three ideas:
requests in progress = throughput × latencyIf a service handles 1,000 requests per second and each takes 0.2 seconds, about 200 requests are in progress at any moment. That number tells you how many threads, connections or memory buffers you need.
It also explains a common failure. If a database slows from 20 ms to 200 ms per query while traffic stays the same, the number of in-flight requests rises ten times. Connection pools fill up, threads run out, and the slowdown spreads to every service that calls this one. You’ll learn how to contain that in week 6.
Why averages lie: percentiles
Suppose 99 requests take 20 ms and one takes 3 seconds. The average is about 50 ms, which describes none of the requests. Instead, measure percentiles:
- p50 (median): half the requests are faster than this. The typical experience.
- p95: 95% are faster; 1 in 20 is slower.
- p99: 99% are faster; 1 in 100 is slower.
- p99.9: the extreme tail.
Requirements are written with percentiles: “p99 latency under 300 ms for the feed API”. In interviews, saying “p99” instead of “average” is a small signal that you’ve run real systems.
The tail matters more than you think
The slowest requests are not rare for your users:
- Heavy users hit the tail most. Someone loading 100 pages a day will meet the p99 about once a day.
- Fan-out multiplies it. If loading a page calls 10 services in parallel and waits for all of them, the page is as slow as the slowest call. With each call having a 1% chance of being slow, about 10% of pages are slow. With 100 calls, it’s 63%.
Common ways to cut tail latency include timeouts with retries, sending a backup request if the first is slow (hedged requests), avoiding long garbage-collection pauses and keeping hot data in memory.
Latency budgets
When a request passes through several components, give each a share of the target. For a 200 ms p99 target for a search page:
| Step | Budget |
|---|---|
| Network to the region and back | 60 ms |
| Load balancer and gateway | 5 ms |
| App server logic | 20 ms |
| Search index query | 80 ms |
| Rendering and slack | 35 ms |
Budgets make it obvious where to optimise and when a design can’t meet the target: if the search index alone takes 150 ms at p99, no amount of tuning elsewhere will help.
Throughput and saturation
As load on a component approaches its capacity, latency doesn’t grow evenly; it shoots up. Requests start queuing behind each other, and at about 70–80% utilisation queues grow quickly. That is why production systems keep headroom: you plan for peak traffic plus a margin, not for the average.
Recap
Latency is how long one request takes; throughput is how many finish per second; bandwidth is the size of the pipe. Concurrency keeps many requests in progress, parallelism runs them at the same time, and Little’s Law ties throughput and latency to the number in flight. Always talk about latency as percentiles, watch the tail, and remember that fan-out makes the tail everyone’s problem.
Read the numbers
An API reports these numbers for the last hour: average latency 45 ms, p50 30 ms, p99 1,200 ms, throughput 2,000 requests per second. (a) What does the p99 tell you that the average hides? (b) Roughly how many requests per hour take longer than 1.2 seconds? (c) Using Little's Law with the average latency, about how many requests are in progress at any moment? (d) A page makes 10 of these calls in parallel and waits for all of them. Why will it often be slow?
(a)
(b)
(c)
(d)(a) 1% of requests take over 1.2 s: a long tail the 45 ms average completely hides.
Something occasionally goes badly wrong (a slow query, garbage collection, a retry).
(b) 2,000 req/s x 3,600 s = 7.2 million requests per hour; 1% of that is
about 72,000 slow requests per hour.
(c) Little's Law: in-flight = throughput x latency = 2,000 x 0.045 s = 90 requests.
(d) The page is as slow as its slowest call. The chance that at least one of 10
calls hits the slowest 1% is 1 - 0.99^10, about 9.6%. So roughly 1 page in 10
takes over 1.2 s, even though each call is "usually fast".Summary
- Latency is the time for one request; throughput is how many requests finish per second; bandwidth is the maximum data rate of a link.
- Concurrency means many tasks in progress at once; parallelism means tasks literally running at the same moment on different cores.
- Report latency as percentiles (p50, p95, p99); averages hide the slow tail that users notice.
- When one request fans out to many calls, the slowest call sets the overall latency, so tail latency matters even more.
- Little's Law: requests in progress = throughput × latency.