What it is
Queueing theory describes what happens when work arrives at something that can only do a limited amount at once: a CPU, a thread pool, a database, a message consumer, a checkout counter. It gives a small set of quantities and laws that explain why systems slow down long before they are "full", and how much concurrency and headroom you need.
The basic vocabulary:
- Arrival rate λ: requests arriving per second.
- Service time S and service rate μ: how long one request occupies a server, and how many one server can finish per second (μ = 1 ÷ S; with c parallel servers the total is c ÷ S).
- Utilisation ρ = λ ÷ (c × μ): the fraction of time the servers are busy.
- W: time a request spends in the system (waiting plus service); Wq: the waiting part.
- L: number of requests in the system (waiting plus in service).
Why it exists
Engineers intuitively expect latency to grow in proportion to load: twice the traffic, twice the delay, and trouble only at 100%. Real systems do not behave like that. A service at 50% utilisation is fast, the same service at 90% is several times slower, and at 98% it is unusable, even though nothing is "out of capacity" yet. Queueing theory explains that curve, and turns it into rules for capacity planning, pool sizing and timeouts.
How it works
Why requests wait below 100%. Arrivals are not evenly spaced, and requests are not all the same size. Sometimes several arrive close together, or one takes much longer than average. Requests that arrive while every server is busy wait. Idle time cannot be saved up for later, so a burst that arrives after a quiet period is not compensated by it.
The ρ ÷ (1 − ρ) factor. For a single server with random (Poisson) arrivals, the Pollaczek–Khinchine formula gives the average wait exactly:
Wq = ρ ÷ (1 − ρ) × S × (1 + cv²ₛ) ÷ 2
where cv_s is the coefficient of variation of the service time. With exponential-like service (cv = 1) that is ρ ÷ (1 − ρ) × S: 1 × S at ρ 0.5, 4 × S at 0.8, 9 × S at 0.9, 19 × S at 0.95, 99 × S at 0.99. That is the hockey stick: flat, then nearly vertical.
Variability. Kingman's approximation generalises this to any arrival pattern: Wq ≈ (cv²ₐ + cv²ₛ) ÷ 2 × ρ ÷ (1 − ρ) × S. Halve the variability and you roughly halve the wait at the same utilisation. With perfectly regular arrivals and identical requests (both cv = 0), a server can run at 90% with no waiting at all; the lesson shows this. Real traffic is bursty (cvₐ often above 1), so this is a lever to pull, not a state you can reach.
Above ρ = 1. If λ exceeds the service rate, there is no steady state. The backlog grows by λ − μ per second for as long as the overload lasts. A bounded queue then rejects; an unbounded one makes every request wait longer until clients time out (and often retry, adding load). Queues absorb bursts; they cannot absorb sustained overload.
Little's law. For any stable system, averaged over a long enough period: L = λ × W. It needs no assumption about arrival patterns, service distributions or scheduling order. An API doing 400 req/s with an average of 250 ms in the system has 100 requests in progress on average, so a thread-per-request server needs at least 100 threads, and a database serving those requests sees up to that many concurrent queries. It also works in reverse: if a pool of 20 connections is fully used and each query holds a connection for 50 ms, the pool cannot deliver more than 20 ÷ 0.05 = 400 queries per second.
Shared versus separate queues. c servers fed from one queue (M/M/c) wait far less than c servers each with their own queue at the same utilisation, because with separate queues a request can be stuck behind a slow one while another server is idle. In a balancer, least outstanding requests (or "power of two choices") approximates a shared queue; random or round-robin routing does not look at queue state. The lesson shows P95 more than halving from random to least connections at ρ 0.85 with four servers.
When to use it
- Sizing thread pools, connection pools, worker counts and queue lengths (Little's law).
- Setting a target utilisation for capacity planning and autoscaling (typically 60–80% at peak for latency-sensitive services, depending on variability and SLO).
- Explaining latency regressions that appear "out of nowhere" as traffic grows a little.
- Deciding between independent queues and a shared queue (work queues, pull-based consumers, least-request balancing).
When not to
- As a precise predictor of P99 for a real system: real service times have heavy tails, arrivals are correlated (retries, cron jobs, viral spikes), and multi-stage systems interact. Use the formulas for direction and order of magnitude, then measure with load tests.
- For systems that are mostly idle: queueing effects are negligible below ~30–40% utilisation.
- When the bottleneck is not a queue at all (a lock held across a slow call, a coordination protocol, GC pauses): model that resource explicitly first.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Run at high utilisation (90%+) | Fewer machines | Long, volatile queueing delays; no burst headroom |
| Run at moderate utilisation (60–75%) | Stable latency, burst headroom, room for a failure | More capacity per unit of traffic |
| Long queues | Fewer rejections during short bursts | Higher latency; work done for clients that already gave up |
| Short queues and fast rejection | Bounded latency, fast feedback | More errors during bursts; callers must retry or degrade |
| Shared queue / least-request routing | Lower waiting at the same utilisation | Needs load information or a central queue |
Alternatives
- Load testing: measure the real latency-throughput curve instead of deriving it. Use both: theory tells you where to look, load tests tell you the actual knee.
- Discrete-event simulation (as in this lab): for multi-stage systems where closed-form formulas break down.
- Operational laws (utilisation law U = X × S, forced-flow law): simpler relationships that need no distribution assumptions and work well on production metrics.
Failure modes
- Running hot: a service tuned to 90% utilisation that falls over during an ordinary burst or a single instance failure.
- Unbounded queues: memory grows, latency grows, and the system ends up processing requests whose callers timed out long ago.
- Retry amplification: as latency rises, clients time out and retry, raising λ exactly when ρ is already near 1.
- Hidden variability: a rare slow endpoint (export, report) sharing a pool with fast requests drives up everyone's wait.
- Per-instance queues with blind routing: one instance with a backlog while others are idle.
In production
Watch utilisation and queue depth per resource, not just averages over a fleet. Track queueing delay separately from service time where you can (time waiting for a thread or a connection). Set timeouts and queue limits from Little's law and the latency you can tolerate. Separate slow and fast workloads into different pools (bulkheads). Prefer least-outstanding-requests balancing for request/response services, and pull-based consumers for work queues. Plan capacity at a target utilisation that leaves room for bursts and for losing an instance.
Common misconceptions
- "Latency stays flat until 100%." It rises steadily and then sharply, because waiting grows like ρ ÷ (1 − ρ).
- "A bigger queue fixes overload." It postpones rejections and raises latency; if λ > μ the queue fills regardless of its size.
- "Little's law only applies to simple queues." It applies to any stable system: a whole service, a database, a thread pool, a team's ticket backlog.
- "Average utilisation of 50% means no queueing." Averages hide bursts; one-minute averages can be 50% while one-second peaks hit 100%.
Interview angle
Expect to be asked why you would not run servers at 95% CPU, how to size a thread or connection pool, or why latency spiked when traffic grew only 10%. Strong answers name ρ = λ ÷ μ, state that waiting grows like ρ ÷ (1 − ρ), use Little's law with numbers ("2,000 req/s × 50 ms = 100 concurrent requests, so a pool of 100 plus headroom"), and mention variability and shared queues as levers besides adding capacity.
Model assumptions
The simulation is a simplified model, not a benchmark. The API server has one worker with a 10 ms mean service time (lognormal, cv 1, so μ = 100 req/s) and room for 150 waiting requests; in the variability step service time has cv 0.1 and arrivals are evenly spaced. Otherwise arrivals are Poisson. Network time is 5 ms each way to the single server, about 6 ms via the balancer (4 ms + 1 ms hop + 1 ms balancer processing). Theoretical waits quoted in the steps come from the M/G/1 formula; a 10-second simulated window of a busy queue fluctuates around them considerably, which is itself a property of queues near saturation. "Least connections" approximates, but is not identical to, a single shared queue. Utilisation is the busy-worker fraction, not CPU. Numbers are illustrative. Latency percentiles (P50, P95, P99) are computed over the requests that succeeded in the last 5 simulated seconds: failed, rejected and timed-out requests are not in them and are counted in the Errors tile, which is always shown next to them. Rejecting or timing out the slowest requests therefore improves the percentiles while making the system less reliable, so read the two together.
How to read this demonstration.
- Actual engine simulation: every step and the sandbox: one server with one worker, Poisson or evenly spaced arrivals, lognormal service times, the bounded queue, and four servers behind a balancer with random or least-connections routing.
- Scripted animation: none.
- Hybrid demonstration: none.
- Simplified model: the formulas quoted in the steps (Pollaczek-Khinchine waits, Kingman) are theory for the long-run average; the simulation measures a short window (a few seconds) whose average and P95 fluctuate considerably around them near saturation, so one take can disagree with the formula by tens of percent. Record several takes or quote ranges.
- Time compression: slow motion (about 0.06 times real time) so requests can be followed; the tiles use simulated seconds.
- Not supported by the simulation: client timeouts and retries, request priorities, non-FIFO disciplines, multi-stage pipelines. Latency percentiles count successful requests only.