Course contents

15. Performance Engineering Lesson 4 of 8

Queueing Fundamentals

See arrival rate, service rate and utilisation in action, why latency forms a hockey stick near 100%, how variability creates queues, and Little’s law.

18 minAdvanced9 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Utilisation ρ = arrival rate ÷ service rate; it must stay below 1, and well below it at peak.
  • Queueing delay grows like ρ ÷ (1 − ρ): the last 10–20% of utilisation costs most of the latency.
  • Above ρ = 1 there is no steady state: the backlog grows until requests are rejected or time out.
  • Queues come from variability; with perfectly regular arrivals and work, even 90% busy means no waiting.
  • Little’s law L = λ × W converts latency and rate into the concurrency you must provision.
  • A shared queue (least outstanding requests, a work queue) beats independent per-server queues.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

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.
The lesson as textEvery step of the simulation, written out.
  1. 1. Arrival rate, service rate, utilisation

    Every queue is described by two rates. Requests arrive at λ = 50 per second, at random moments. The server has one worker and needs 10 ms per request on average, so it can serve μ = 100 per second.

    Utilisation ρ = λ ÷ μ = 0.5: the worker is busy half the time. Even so, some requests arrive while it is busy and wait. Theory predicts an average wait of about 10 ms here, as long as the work itself; the median request barely waits at all.

    Scene: Users send about 50 requests per second at random moments to an API server with one worker that needs about 10 ms per request. The server is about half busy and latency is low.

    Key idea: Utilisation ρ = arrival rate ÷ service rate. Below 1 the server keeps up on average, but requests can still wait.

  2. 2. Busier: the wait grows faster than the load

    Traffic rises to 80 per second, so ρ = 0.8. The load went up by 60%, but the queueing delay roughly quadruples: the theoretical average wait goes from 10 ms to 40 ms.

    The reason is the factor ρ ÷ (1 − ρ) in the waiting time: 1 at ρ 0.5, 4 at ρ 0.8. The less idle time is left, the longer a burst of arrivals takes to drain, and every request behind it waits.

    Scene: At 80 requests per second the server is about 80% busy. A queue appears more often and P95 latency is several times higher than at 50 requests per second.

    Key idea: Average wait ∝ ρ ÷ (1 − ρ): it is not proportional to load, it explodes as idle time disappears.

  3. 3. Near 100%, latency shoots up

    Now 95 per second, ρ = 0.95. That is only 15 points more than before, yet ρ ÷ (1 − ρ) jumps from 4 to 19. The theoretical average wait is about 190 ms, and the queue counter under the API swings between empty and dozens.

    Plot latency against utilisation and you get a hockey stick: flat for a long time, then nearly vertical. The last few percent of utilisation cost more latency than everything before them, which is why services are run at 60–80% at peak, not 95%.

    Scene: At 95 requests per second the server is almost always busy, its queue swings between empty and dozens of requests, and average latency rises more between 80 and 95 requests per second than it did between 50 and 80.

    Key idea: The latency curve is a hockey stick: the last 10–20% of utilisation costs more latency than the first 80%.

  4. 4. Above 1, the queue only grows

    At 150 per second, ρ = 1.5. There is no steady state any more: 50 more requests arrive per second than leave, so the queue grows by about 50 per second until it hits its limit of 150. From then on each new request is rejected (503) or, with an unbounded queue, would wait longer and longer.

    Throughput is stuck at the service rate, about 100 per second, and the requests that do get in wait roughly 150 × 10 ms = 1.5 s. Any queue with λ > μ for long enough ends like this.

    Scene: At 150 requests per second the server is overloaded: its queue is full at 150 waiting requests, throughput stays at about 100 per second, the rest is rejected, and accepted requests wait over a second.

    Key idea: If λ > μ, no queue size is enough: the backlog grows until requests are rejected or time out.

  5. 5. Queues come from variability, not from load alone

    Back to 90 per second (ρ = 0.9), but now requests arrive evenly spaced (one every 11 ms) and every request takes almost exactly 10 ms. Each one finds the worker free: P95 is barely above the 20 ms of network plus work.

    With random arrivals and variable work, the same ρ = 0.9 would mean an average wait of about 90 ms. Kingman’s approximation says the wait scales with (cv²ₐ + cv²ₛ) ÷ 2: the variability of arrivals and of service. Real traffic is bursty, so you can rarely remove it; but smoothing work (splitting slow endpoints off, batching, rate limits) does shorten queues.

    Scene: Requests now arrive evenly spaced at 90 per second and each takes almost exactly 10 ms. The server is 90% busy, yet requests almost never wait and latency stays near 20 ms.

    Key idea: Wait ≈ variability × ρ ÷ (1 − ρ) × service time. With no variability, a server can run at 90% without queueing.

  6. 6. Little's law: L = λ × W

    Random arrivals and variable work are back, still at 90 per second. Watch In flight: the number of requests somewhere in the system right now. Little’s law says that on average L = λ × W: requests in the system = arrival rate × time each spends in it.

    Theory puts W near 0.11 s at this load (20 ms of network and work plus about 90 ms of waiting), but a queue this busy swings a lot from one 10-second window to the next; this run sits nearer 0.15 s. Whatever W turns out to be, In flight follows λ × W: here about 10 to 15 requests. The law holds for any stable system, whatever the arrival pattern or scheduling, which is why it turns a latency and a rate into the number of threads, connections or queue slots you need.

    Scene: Random arrivals at 90 requests per second. On average about 10 to 15 requests are in flight, which equals the arrival rate times the average time in the system.

    Key idea: L = λ × W holds for any stable system: in-flight work = throughput × time in system.

  7. 7. Four servers, four separate queues

    Traffic grows to 340 per second, so we run four identical servers (400 per second in total, ρ = 0.85) behind a balancer that picks a server at random.

    Each server is now its own little queue at ρ 0.85. A request can wait behind a slow one on API 2 while API 3 sits idle: random choice does not look. P95 is far above the 20 ms a request needs.

    Scene: Four API servers with one worker each sit behind a balancer that picks one at random. At 340 requests per second each is about 85% busy; queues form on some servers while others are idle, and P95 is high.

    Key idea: Separate queues waste capacity: one can be idle while another has a backlog.

  8. 8. Join the shortest queue

    Same servers, same 340 per second, but the balancer now uses least connections: each request goes to the server with the least work in progress. That behaves almost like one shared queue feeding four workers, the way a single line at a bank feeds every teller.

    No request waits while a server is idle, so P95 falls sharply even though utilisation is unchanged. Pooling servers behind one queue is one of the cheapest latency wins there is.

    Scene: The balancer sends each request to the server with the least work in progress. All four stay about 85% busy, but requests rarely wait while another server is idle and P95 drops sharply.

    Key idea: One queue in front of c servers beats c separate queues at the same utilisation.

  9. 9. What the queueing model tells you

    Four rules carry most of capacity work. ρ = λ ÷ μ must stay below 1, and well below it at peak. Wait grows like ρ ÷ (1 − ρ), so leave headroom. Variability (bursts, slow requests) is what creates queues; reduce it where you can. L = λ × W converts latency and rate into the concurrency you must provision.

    And when you add servers, give them a shared queue (least connections, a work queue, a pull model) rather than independent ones.

    Scene: Four API servers behind a least-connections balancer handle 340 requests per second at about 85% utilisation with low latency.

    Key idea: Keep ρ below about 0.8 at peak, reduce variability, size with L = λ × W, and share queues.