Tail latency and queueing
The average hides your slowest users. Learn why the slow tail grows with fan-out and with busy queues, and what seniors do about it.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
This story is made up, but it is common. A shop's search page sends each query to 100 index servers at once. It merges their answers and shows the results. Each server answers in about 10 ms.
The team is proud of the dashboard. Every server shows an average latency near 20 ms. But users say search "often hangs for a second". The team cannot find a slow server, because no single server looks slow.
The cause is small. Every few seconds, each server pauses for garbage collection. About 1 call in 100 to any server takes a full second. That looks rare on one server.
But every search waits for all 100 servers. So most searches hit at least one slow server. The rare problem on each server became the normal case for users.
The idea in simple words
Latency is how long a request takes. It is never one number. Some requests are fast and a few are very slow. The slow end of this spread is called the tail.
A percentile describes the spread. The p50 (median) is the typical request. The p99 is the time that 99% of requests are faster than. The p999 (p99.9) is the time that 999 in 1,000 requests are faster than.
Google's SRE book says latency is better seen as a distribution than as an average. A simple average "can obscure these tail latencies". In one of its examples, a typical request takes about 50 ms, but 5% of requests are 20 times slower. 2
Two forces make the tail worse as you grow:
- Fan-out. One user request calls many servers and waits for all of them.
- Queueing. When servers get busy, requests wait in line, and waiting time grows very fast near full load.
The key idea. Users feel your tail, not your average. Fan-out makes rare slowness common, and a busy queue makes waits explode. Measure p99, keep spare capacity, and refuse work early.
How it works
Fan-out makes the tail common
Dean and Barroso give the classic example. Each server usually answers in 10 ms, but its p99 is one second. If a user request uses one server, 1 request in 100 is slow. If it must collect answers from 100 servers, 63% of user requests take more than one second. 1
The maths: one server is fast 99% of the time. All 100 are fast with chance 0.99^100 ≈ 0.366. So the chance that at least one is slow is 1 − 0.366 ≈ 0.63.
Rarer slowness does not save you. If only 1 call in 10,000 is slow, a service with 2,000 servers still sees almost 1 user request in 5 take over a second. 1 Check it: 1 − 0.9999^2000 ≈ 0.18.
Step through it. Use Next and Back, or the ← and → keys.
- One search request is sent to 100 servers at once. The page can show results only when all 100 have answered. Each server usually answers in 10 ms.
- Each server has rare slow moments, such as a garbage collection pause. 1 call in 100 takes a full second. If a request used only one server, just 1% of requests would be slow.
- The request is fast only if all 100 servers are fast. Each is fast with chance 0.99. All 100 together: 0.99 multiplied by itself 100 times, which is about 0.366.
- So about 1 − 0.366 ≈ 0.63 of requests wait for at least one slow server. A 1% problem on each server has become a 63% problem for users.
- One fix is a hedged request. If a server has not answered after a short delay, send the same request to another replica and use whichever answers first. In a Google test over 100 BigTable servers, this cut the p99.9 from 1,800 ms to 74 ms, with just 2% more requests.
Google measured this on a real service. The p99 for one random leaf server to answer was 10 ms. The p99 for all leaf servers to answer was 140 ms. The p99 for 95% of them was 70 ms. So waiting for the slowest 5% caused half of the total p99. 1
Where does the slowness come from? The paper lists shared machines, background daemons, garbage collection, log compaction, and queueing at many layers. Most of these are not bugs, and removing them all is not practical. 1
Little's law
Little's law links three long-run averages for any stable queue or system: L = λW. L is the number of items inside, λ (lambda) is the arrival rate, and W is the time each item spends inside. 3 It does not depend on the order of service, such as first in, first out (FIFO) or last in, first out (LIFO). 3
For a server, it means: requests in progress = requests per second × seconds per request. At 2,000 requests a second and 50 ms each, about 100 requests are in progress at any moment.
Use it both ways. If latency doubles at the same traffic, the number of requests in progress doubles too. That means twice the threads, connections and memory. This is how a slow database can use up an app server's connection pool.
Busy servers make long queues
Utilization is how busy a server is: arrival rate divided by service rate. In the simplest queue model (M/M/1), the average time in the system is 1 / (service rate − arrival rate). 4 Put another way, it is service time / (1 − utilization).
Take a service time of 10 ms:
| Utilization | Average time in system |
|---|---|
| 50% | 20 ms |
| 80% | 50 ms |
| 90% | 100 ms |
| 95% | 200 ms |
| 99% | 1,000 ms |
Going from 50% to 95% busy makes each request about 10 times slower. Real systems are not this simple model, but the shape holds. Little writes that as arrivals approach the service rate, the queue and the wait "increase, slowly at first and then rapidly". 3 In one load test he reports, the queue grew about linearly with load and then climbed steeply. Past about 18 requests a second, further requests were mostly dropped. 3
Our advice: do not plan to run latency-sensitive services near 100% busy. In the last 10% of capacity, the slowest requests get much slower.
Backpressure and load shedding
When work arrives faster than you can finish it, the queue grows without end. You have two honest choices:
- Backpressure. Tell the sender to slow down. A bounded queue does this: when it is full, the sender must wait or get an error.
- Load shedding. Reject the extra work quickly, so the work you accept still finishes on time.
Google's SRE book says queued requests use memory and add latency. It suggests small queues, such as 50% or less of the thread pool size, so the server rejects early. 5 It also suggests per-task throttling on CPU, memory or queue length. 5
Amazon explains why a long queue is worse than a rejection. When latency climbs past the client's timeout, requests fail even though the server did the work. If median latency equals the client timeout, half the requests time out. 7 Amazon calls the useful part goodput: throughput handled without errors and fast enough for the client to use. 7 It suggests a limit on how long a request may sit in a queue. Under overload, it suggests a LIFO queue where the protocol allows it. 7
Not all requests are equal. Google tags each request with a criticality, from CRITICAL_PLUS down to SHEDDABLE, and rejects the least critical requests first. 6
Hedged requests
You cannot remove every slow moment, so hide them. A hedged request sends a request to one replica. If no answer comes after a short delay, it sends a backup copy to another replica and uses whichever answers first. 1
The trick is the delay. Wait until the request is older than the p95 for its kind, and the extra load is only about 5%. 1 In a Google test, reading 1,000 keys spread over 100 BigTable servers, a hedge after 10 ms cut the p99.9 from 1,800 ms to 74 ms. It sent just 2% more requests. 1
Tied requests go further: both servers get the request, and the first one to start it tells the other to cancel. 1 Our advice: hedge only safe, repeatable reads, or writes that are idempotent.
Timeouts and deadlines
Every call needs a timeout. Google's SRE book says it is usually wise to set a deadline. A missing or very high deadline can keep resources busy long after the problem has passed. 5
Pass the remaining time down the call chain. Each server should check it before doing more work, and drop requests that have already run out of time. 5 Limit retries too. The book advises randomized exponential backoff and a retry budget per process, such as 60 retries a minute. 5
Trade-offs
| Tool | Cuts the tail? | Cost | Use it when |
|---|---|---|---|
| More spare capacity | Yes Lower utilization means shorter queues. | Money for idle servers. | Latency-sensitive paths. |
| Hedged requests | Yes , often by a lot. | A few % extra load. Needs safe repeats. | Reads with many replicas, large fan-out. |
| Smaller fan-out | Yes Fewer servers to wait for. | Bigger shards, or less parallel work. | You can group data so one request hits fewer servers. |
| Load shedding | Yes , for accepted work. | Some users get a fast error. | Traffic can exceed capacity. |
| Backpressure | Yes , by keeping queues short. | Senders must slow down or buffer. | Pipelines and queues between your own services. |
| Bigger queues | No It hides overload and adds wait. | Memory, and stale work. | Short, rare bursts only. |
Our simple rule: measure p99 per call, keep queues short, set deadlines, and hedge only safe reads.
At work: how a senior uses this
- Ask for p50, p99 and p99.9, never only the average. Ask: "p99 of what, over what time window?"
- Draw the fan-out of a request. Count how many calls it waits for, and work out how often at least one of them is slow.
- Use Little's law to size pools: requests per second × latency = requests in progress. Size threads and connections from it.
- Set alerts on queue length and the age of the oldest item, not only on CPU.
- Set a timeout on every outbound call, below the caller's own deadline.
- In load tests, push on purpose past the point where latency starts to rise steeply. Write down the load where latency jumps.
- Read latency numbers and estimation to get the base numbers right first.
With AI
What to ask the agent
"Here are latency histograms for our 12 downstream calls. Which call drives our p99? Show the maths."
"Our service gets 3,000 requests a second at 40 ms p50 and 400 ms p99. Use Little's law to size the thread pool and the database connection pool. Explain each number."
"Add a hedged read to this client: send a backup to a second replica after the p95 latency, cancel the loser, and add a metric for how often the hedge fires."
"Find every outbound HTTP or gRPC call in this repo that has no timeout or no deadline propagation."
What to check yourself
- The agent did not average percentiles. A p99 across servers cannot be built by averaging each server's p99.
- Hedging is used only for reads or idempotent writes, and the backup really cancels the first call.
- The hedge delay comes from measured latency, not a guess.
- Queues have a size limit, and there is a rule for what happens when they are full.
- Timeouts shrink down the call chain, so an inner call never outlives its caller.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 7 sources and related lessons
- Jeffrey Dean and Luiz André Barroso, “The Tail at Scale”, Communications of the ACM 56(2), February 2013 (opens in a new tab)
Why rare slowness becomes common with fan-out, Google's measurements, and hedged and tied requests. - Chris Jones, John Wilkes and Niall Murphy, with Cody Smith, “Service Level Objectives”, Site Reliability Engineering (Google, O'Reilly 2016), chapter 4 (opens in a new tab)
Why to think of latency as a distribution, and why an average hides the tail. - John D. C. Little, “Little's Law as Viewed on Its 50th Anniversary”, Operations Research 59(3), 2011 (opens in a new tab)
L = λW explained by its author, with a server load test where queues explode. - MIT OpenCourseWare, “M/M/1 Queue”, course 2.854 Introduction to Manufacturing Systems, Fall 2016 (opens in a new tab)
The two-page formula sheet for the simplest queue, with utilization and time in system. - Mike Ulrich, “Addressing Cascading Failures”, Site Reliability Engineering (Google, O'Reilly 2016), chapter 22 (opens in a new tab)
Queue sizes, load shedding, deadlines and their propagation, and retry budgets. - Alejandro Forero Cuervo, “Handling Overload”, Site Reliability Engineering (Google, O'Reilly 2016), chapter 21 (opens in a new tab)
Per-customer limits, request criticality and client-side throttling. - Amazon Builders' Library, “Using load shedding to avoid overload”, 2019 (opens in a new tab)
Goodput, timeouts that meet rising latency, and dropping work that nobody waits for.
Case studies: Amazon: Why queue backlogs can turn a short outage into a long one · Discord: Why Discord rewrote a Go service in Rust · Google: The Tail at Scale: rare slowness becomes common at scale · Netflix: Adaptive concurrency limits instead of hand-tuned limits
Related: Latency numbers and back-of-envelope maths · Load shedding and graceful degradation · Backpressure and flow control · Backoff, jitter and retry budgets · Fan-out on write vs fan-out on read · Load testing (coming soon) · Observability, SLOs and error budgets · Garbage collection and runtime tuning (coming soon)
Related lessons: Latency numbers and estimation, Load shedding, Backpressure and Backoff and jitter.