SysDesignAtlas

Queueing Theory & Why Latency Explodes

Little's Law, the utilization curve, and the mathematical reason a system that is fine at 70% load falls apart at 85% — the most useful theory in systems engineering.

CoreMedium13 min readfundamentalslatencyqueueingcapacity
Confidence

#The observation that needs explaining

A service handles 700 requests per second with a p99 of 40 ms — meaning 99 requests in 100 finish within 40 ms, and the slowest 1% are what we're measuring. Traffic grows 20%. Now it handles 840 requests per second — well under the 1,000/s the team measured as its ceiling — and p99 is 1.2 seconds.

Nothing broke. No code changed. Load went up 20% and tail latency went up 30×.

This is not a bug, and it is not mysterious. It is the single most important curve in systems engineering, and once you have seen it you will recognise it in every incident review you ever read.

#Little's Law

The foundational result, and it is almost embarrassingly simple:

Stable means that over the long run requests finish as fast as they arrive — the queue isn't growing forever. That generality is what makes it useful. Three quantities, and knowing any two gives you the third.

Worked examples:

  • A service handles 2,000 req/s with an average latency of 50 ms. Concurrency is 2000 × 0.05 = 100 requests in flight. If your thread pool has 80 threads, the pool itself is already the bottleneck: at any moment about 20 requests have no thread to run on and must wait.
  • You want 10,000 req/s and each request takes 200 ms because it waits on a slow downstream service (one your service calls). You need 10000 × 0.2 = 2000 concurrent operations in flight. With one OS thread per request that's 2,000 threads, each mostly sitting idle while it waits. That is why this workload wants an event loop or async runtime — a design where a handful of threads juggle thousands of waiting requests — not one thread each. See concurrency and the OS.
  • A queue holds 50,000 messages and consumers drain 5,000/s. Messages wait 50000 / 5000 = 10 seconds. If you've promised customers 2 seconds (an SLA — a service level agreement, the response time you're contractually on the hook for), you need 5× the consumers, and Little's Law told you that in one line.

#The utilization curve

Little's Law tells you the relationship between the three quantities. It does not tell you why 84% utilization is dangerous. For that you need one more result.

Utilization is the fraction of the time your server is busy rather than idle: arrivals divided by how many it could handle. 84% utilization means it's working 84 minutes of every 100.

For the simplest possible case — requests turning up at random moments, one server handling them one at a time — there's a standard formula. (It's called the M/M/1 model: arrivals are random, service times are random, one server. "Random" here means Poisson arrivals, which just means each arrival is independent of the last, like raindrops hitting a window rather than a metronome.) The average time in the system is:

Put numbers in it. Take a service where one request takes 10 ms of actual work:

UtilizationMultiplier 1/(1−ρ)Response timeChange from previous row
10%1.1111 ms—
50%2.020 ms+9 ms
70%3.333 ms+13 ms
80%5.050 ms+17 ms
90%10100 ms+50 ms
95%20200 ms+100 ms
99%1001,000 ms+800 ms
99.9%100010,000 ms+9,000 ms
utilization → 0% to 100% latency → S / (1 − ρ) the knee ~70-80%
Response time versus utilization. The knee is not a metaphor.

The last 10% of capacity costs more than the first 90%. Going from 10% to 80% utilization — 70 percentage points of extra load — added 39 ms. Going from 80% to 95% — just 15 more points — added 150 ms, nearly four times as much.

#Variability makes it worse

M/M/1 assumes exponentially distributed service times — a specific, moderate amount of randomness, where most requests are quick and longer ones get steadily rarer. Real workloads are far more variable than that: a request that hits cache takes 2 ms, one that misses and does three database queries takes 200 ms.

Variability here means how much request times differ from each other, not how long they are on average. A service where every request takes exactly 50 ms has no variability; one where they range from 2 ms to 2 seconds has a lot — even if both average the same.

The general result (Kingman's formula) says queue wait scales with the square of that variability:

This is why the following all work, and why they work better than they seem they should:

  • Separating fast and slow requests into different pools. Mixing a 2 ms request behind a 2 second one inflates c²ₛ enormously. Split them and both get faster.
  • Timeouts. A timeout cuts off the very slowest requests, so no request can hold a server for, say, 30 seconds. Service times become more even, and everyone queued behind waits less.
  • Caching. Beyond making the cached request fast, it removes the slow cases from the mix.
  • Bounded batch sizes. One giant batch is a single job that takes far longer than everything else, and every request queued behind it waits for all of it. Capping batch size keeps job lengths even.

#More servers beat a faster server

Now compare one server that is twice as fast (M/M/1) against two servers of the original speed sharing one queue (M/M/2). Same total capacity.

   M/M/1, one 2× server           M/M/2, two 1× servers, shared queue

   →→→→ [ ▓▓ ]                    →→→→ ┌─ [ ▓ ]
                                       └─ [ ▓ ]

   At ρ = 0.8:                     At ρ = 0.8:
     Wq = 4.0 × S/2 = 2.0 S          Wq ≈ 1.42 × S/1 ... but each server
     (S/2 because it's 2× faster)    handles half → effective ≈ 0.71 S

   → Two servers sharing one queue beat one server of double speed,
     because a burst can be served in parallel and a single slow
     request no longer blocks everything behind it.

This is the mathematical justification for horizontal scaling beyond just cost, and — importantly — for a single shared queue rather than per-server queues. Round-robin load balancing assigns work to a server that may already be busy while another sits idle. That is why least-connections or least-outstanding-requests balancing — send each new request to whichever server has the fewest requests currently in progress — measurably beats round robin at high utilization. It gets close to the behaviour of one shared queue. See load balancing.

#Why queues do not fix overload

The intuition "add a buffer to absorb the spike" is right only if the spike is temporary and the buffer is small.

   Arrival rate 1,200/s · service rate 1,000/s · unbounded queue

     t = 1s    queue = 200     wait = 0.2 s
     t = 10s   queue = 2,000   wait = 2 s
     t = 60s   queue = 12,000  wait = 12 s
     t = 300s  queue = 60,000  wait = 60 s

   The queue never recovers while λ > μ (arrivals outpace service).
   It just converts
   "some requests fail" into "all requests are late."

If arrivals exceed service capacity, no queue size saves you. Queues absorb variance; they do not add capacity. An unbounded queue in an overloaded system produces the worst outcome available: every request is served, all of them too late to be useful, and the client has usually timed out and retried — adding more load.

The correct response to sustained overload is to shed load: bounded queues, admission control, and fast rejection. A rejected request costs almost nothing; a request served after the client gave up costs full capacity and delivers zero value. This is the core argument of backpressure.

#Applying it

Answers worth checking your reasoning against: (1) 200 / 0.1 = 2,000 req/s. (2) ρ = 0.9, multiplier 10, so ~1 s — and it's optimistic, because real service times are more variable than exponential. (3) The timeout and the pool split, because both attack variance; a bigger queue makes it strictly worse. (4) 5 seconds — and you should alarm on queue wait time, not depth, because depth means nothing without the drain rate.

#Check yourself

In the M/M/1 model, what does response time do as utilisation ρ approaches 100%, and why?Show answer
It goes to infinity: W = S / (1 − ρ), and the denominator heads to zero. With 10 ms of work it's 50 ms at 80%, 100 ms at 90% and 1 s at 99%.
A service goes from 70% to 84% utilisation. What happens to average queueing delay?Show answer
The multiplier 1/(1 − ρ) rises from about 3.3 to about 6.25, so average latency nearly doubles — and p99 moves far more — even though the ceiling was never reached.
Read Little's law backwards: if demand fixes throughput, what is the only lever on latency?Show answer
How much is in the system at once — smaller queues, admission control, less work in flight. That's the basis of backpressure, and why removing buffering can make a system faster.
Kingman's formula says queue wait grows with variability. Name three ways to cut variability without adding machines.Show answer
Split fast and slow requests into separate pools; add timeouts to cut off the slow tail; cache, which removes slow cases; keep batches bounded.
Why does a shared queue feeding many servers beat one queue per server?Show answer
With one queue, no request waits behind a slow one while another server sits idle — the airport snake versus supermarket checkouts. It's why least-outstanding-requests balancing beats round robin under load.
Arrivals are 1,200/s and capacity is 1,000/s, with an unbounded queue. What happens over five minutes?Show answer
The queue grows by 200 a second without limit — 60,000 waiting and 60 s of wait after five minutes. Queues absorb variance, not overload; under sustained overload you must shed load.
What is coordinated omission in a load test?Show answer
A generator that waits for each response before sending the next pauses when the server stalls, so it never records the requests that would have piled up. Latency numbers look fine while the system is broken.
Your queue-depth alarm fires at 10,000 messages. Why is wait time a better signal than depth?Show answer
Depth means nothing without the drain rate: 10,000 messages at 2,000/s is a 5 s wait, at 100/s it's 100 s. Alarm on how long a message waits.

#What to carry forward

  • L = λW — concurrency, throughput, latency. Know two, derive the third.
  • W = S/(1−ρ) — the knee sits around 70–80% utilization. Plan capacity to stay left of it.
  • Variance costs as much as load. Reducing variability lowers latency without adding hardware.
  • Many servers on one shared queue beat a single fast server, and beat per-server queues.
  • Queues absorb variance, not overload. Under sustained overload, shed rather than buffer.
  • Measure the utilization of the actually-constrained resource, at burst resolution, without coordinated omission.
00:00
/ 13m