TExcelTAD

Queueing & Latency

Why does a service that is "only" 85% busy have terrible tail latency, while at 60% it was fine? Because requests don't arrive evenly. When several arrive close together, they wait in a queue, and the closer the server runs to full utilisation, the longer those queues get, non-linearly. A little queueing theory explains most latency surprises in capacity planning and is the basis for load shedding.

Little's law

For any stable system, on average:

L = λ × W        items in the system = arrival rate × time each item spends in the system

It holds for a queue, a thread pool, a database connection pool or a whole service. A service handling 200 requests per second with an average latency of 50 ms has 200 × 0.05 = 10 requests in flight. If latency rises to 500 ms at the same rate, 100 are in flight, which may be more than its worker pool or connection pool allows.

The utilisation knee

The simplest queueing model (M/M/1: random arrivals, random service times, one server) gives the average time in the system as W = S / (1 − ρ), where S is the service time and ρ the utilisation. The model checks the formula with a seeded simulation of 200,000 requests and also reports the p99, which the formula doesn't give:

python · try it yourself
import random

def simulate(utilisation, service_ms=10.0, n=200_000, seed=1):
    rng = random.Random(seed)
    arrival_rate = utilisation / service_ms           # requests per ms
    t = free_at = 0.0
    latencies = []
    for _ in range(n):
        t += rng.expovariate(arrival_rate)            # next arrival
        start = max(t, free_at)                       # wait if the server is busy (Lindley's recursion)
        free_at = start + rng.expovariate(1 / service_ms)
        latencies.append(free_at - t)
    latencies.sort()
    return sum(latencies) / n, latencies[int(0.99 * n)]

print("util  mean (sim)  mean (S/(1-p))     p99")
for u in (0.5, 0.7, 0.8, 0.9, 0.95):
    mean, p99 = simulate(u)
    print(f"{u:>4.0%} {mean:>8.1f} ms {10 / (1 - u):>10.1f} ms {p99:>7.0f} ms")
util  mean (sim)  mean (S/(1-p))     p99
 50%     20.0 ms       20.0 ms      92 ms
 70%     33.1 ms       33.3 ms     152 ms
 80%     49.7 ms       50.0 ms     236 ms
 90%     98.6 ms      100.0 ms     486 ms
 95%    220.4 ms      200.0 ms    1194 ms

Going from 50% to 80% busy more than doubles the mean latency; from 80% to 95% multiplies it again by about four, and the p99 follows. Real services are not exactly M/M/1, but the shape, flat then a sharp knee, is universal. That is why capacity plans target 50–70% utilisation at peak for latency-sensitive services, not 90%.

Pooling: one queue, several servers

Ten servers behind one shared queue (a load balancer that sends each request to a free server) do much better at the same utilisation than ten separate queues with random assignment, because a request never waits behind a slow one while another server is idle:

python · try it yourself
import heapq
import random

def pooled(servers, utilisation, service_ms=10.0, n=100_000, seed=2):
    rng = random.Random(seed)
    rate = servers * utilisation / service_ms
    free = [0.0] * servers                         # when each server becomes free (min-heap)
    t, lat = 0.0, []
    for _ in range(n):
        t += rng.expovariate(rate)
        first_free = heapq.heappop(free)
        start = max(t, first_free)
        done = start + rng.expovariate(1 / service_ms)
        heapq.heappush(free, done)
        lat.append(done - t)
    lat.sort()
    return lat[int(0.99 * n)]

def random_assignment(servers, utilisation, service_ms=10.0, n=100_000, seed=2):
    rng = random.Random(seed)
    rate = servers * utilisation / service_ms
    free = [0.0] * servers
    t, lat = 0.0, []
    for _ in range(n):
        t += rng.expovariate(rate)
        s = rng.randrange(servers)                 # pick a server at random, wait in its queue
        start = max(t, free[s])
        free[s] = start + rng.expovariate(1 / service_ms)
        lat.append(free[s] - t)
    lat.sort()
    return lat[int(0.99 * n)]

for u in (0.7, 0.9):
    print(f"10 servers at {u:.0%}: p99 shared queue {pooled(10, u):5.0f} ms, "
          f"random assignment {random_assignment(10, u):5.0f} ms")
10 servers at 70%: p99 shared queue    47 ms, random assignment   152 ms
10 servers at 90%: p99 shared queue    57 ms, random assignment   484 ms

This is why load balancers use "least requests" or "power of two choices" instead of pure random or round robin for uneven workloads, and why one big pool of workers beats several small ones.

What it means for operations

  • Queues hide overload until it is too late. Latency rises slowly and then explodes; alert on latency SLO burn and on saturation (queue length, pool usage), not only on CPU.
  • Bound every queue. An unbounded queue turns overload into unbounded latency and memory use. A bounded one turns it into fast rejections that can be retried elsewhere (Load Shedding).
  • Little's law sizes pools. Concurrency needed = throughput × latency, plus headroom for the latency you expect during incidents.
  • Plan capacity for the knee, not the average (Capacity Planning).

Test yourself

A service handles 400 requests per second at 25 ms average latency. How many requests are in flight on average?

  1. 16
  2. 10
  3. 400
  4. 25

According to W = S / (1 - utilisation), what happens to mean latency going from 80% to 90% utilisation?

  1. It rises by 10%
  2. It doubles
  3. It stays the same
  4. It halves

Why does a shared queue in front of 10 servers give a lower p99 than random assignment?

  1. The servers are faster
  2. No request waits behind another while some server is idle
  3. Random assignment drops requests
  4. It uses fewer servers