Queues, Workers and Little’s Law: Sizing Capacity Before the Queue Explodes
Use queues to decouple a web site from slow work, and learn why waiting time explodes as utilisation approaches 100 percent. Simulate a single-server queue, check Little's law L = λW on the data, and compare with the exact formula in a second implementation.
In this lesson you will
- Explain why queues decouple systems
- State Little’s law
- See why waiting explodes near full utilisation
- Size workers using the formula
About 28 minutes
The email that took eight minutes
When a customer buys a ticket on Wren’s site, the server must create a PDF, send an email and update a report. Doing it all inside the web request makes checkout slow, and when the email service stalls, checkouts fail. The cloud answer is a queue: the web server drops a small message, “send ticket 4711”, into a managed queue and replies at once, and separate workers take messages from the queue and do the slow work at their own pace. The queue decouples the two sides, so a burst of orders only makes the queue longer for a while and does not break checkout, and you can add workers without touching the web server. Messages are usually delivered at least once, so a worker must be able to handle the same message twice without sending two emails, a property called idempotence.
How many workers are needed? Queueing theory gives surprisingly simple rules. Let λ be the arrival rate of jobs, μ the rate at which one worker finishes them, and ρ = λ / μ the utilisation. Little’s law, true for any stable system, says that the average number of jobs in the system, L, equals the arrival rate times the average time a job spends in it: L = λ W. If 8 jobs arrive each second and each spends half a second in the system, there are on average 4 jobs in it. For the simplest random model, a single worker with random arrivals and random job lengths, the average time is W = 1 / (μ – λ). The consequence is the central warning of capacity planning: as the utilisation ρ approaches 1, the denominator approaches zero and the waiting time grows without limit. At 50 percent utilisation a job spends twice its service time in the system, at 90 percent ten times and at 95 percent twenty times.
| Symbol | Meaning | Unit |
|---|---|---|
| λ | Arrival rate | jobs per second |
| μ | Service rate of one worker | jobs per second |
| ρ = λ / μ | Utilisation | between 0 and 1 |
| W | Average time in system | seconds |
| L = λ W | Average jobs in system | jobs |
function mulberry32(a) { return function () { a |= 0; a = a + 0x6D2B79F5 | 0; let t = Math.imul(a ^ a >>> 15, 1 | a); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; }
const mu = 10, N = 400000; // one worker handles 10 jobs per second on average
function run(rho, seed) {
const rnd = mulberry32(seed), exp = rate => -Math.log(1 - rnd()) / rate, lam = rho * mu;
const arr = new Float64Array(N), dep = new Float64Array(N); let t = 0, free = 0, sum = 0;
for (let i = 0; i < N; i++) { t += exp(lam); arr[i] = t; const start = Math.max(t, free); free = start + exp(mu); dep[i] = free; sum += free - t; }
// average number in system by sweeping arrival and departure events (not from the sojourn times)
let i = 0, j = 0, n = 0, last = 0, area = 0; const T = dep[N - 1];
while (j < N) { const ta = i < N ? arr[i] : Infinity, td = dep[j]; if (ta <= td) { area += n * (ta - last); last = ta; n++; i++; } else { area += n * (td - last); last = td; n--; j++; } }
return { W: sum / N, L: area / T, lamObs: N / T };
}
const out = [];
for (const rho of [0.5, 0.8, 0.9, 0.95]) { const r = run(rho, 1000 + Math.round(rho * 100)); out.push({ rho, W: r.W, L: r.L, lamObs: r.lamObs, theory: 1 / (mu - rho * mu) }); console.log('rho', rho.toFixed(2), '| W simulated', r.W.toFixed(3), 's, theory', (1 / (mu - rho * mu)).toFixed(3), 's | L simulated', r.L.toFixed(3), ' lambda x W', (r.lamObs * r.W).toFixed(3)); }
console.log('DATA ' + JSON.stringify(out));
rho 0.50 | W simulated 0.200 s, theory 0.200 s | L simulated 1.001 lambda x W 1.001
rho 0.80 | W simulated 0.493 s, theory 0.500 s | L simulated 3.944 lambda x W 3.944
rho 0.90 | W simulated 0.962 s, theory 1.000 s | L simulated 8.662 lambda x W 8.662
rho 0.95 | W simulated 2.059 s, theory 2.000 s | L simulated 19.544 lambda x W 19.544
The simulation runs 400,000 jobs through one worker with a service rate of 10 jobs per second, at four utilisations. Arrivals and service times are random, drawn from exponential distributions with a seeded generator, so the run is repeatable. At ρ = 0.5 the average time in the system is 0.200 seconds, against the theoretical 0.200, which is 0.1 seconds of service stretched to 0.2 by waiting. At 0.8 it is 0.493 against 0.500, at 0.9 it is 0.962 against 1.000 and at 0.95 it is 2.06 against 2.00. The waiting time is not proportional to the load: going from 50 to 95 percent utilisation multiplies it by about ten.
The program also tests Little’s law without using the waiting times. It sweeps through the arrival and departure events in time order, keeps the number of jobs present, and integrates that number over time to get the average number of jobs, L. The last two numbers of each line are this L and the product of the observed arrival rate and the average time, and they agree: at ρ = 0.8 they are 3.944 and 3.944, as the law demands, and the theoretical value for this system is ρ / (1 – ρ) = 4 jobs, in line with 8 x 0.5. The practical rule is to keep utilisation of a queue-fed pool below roughly 70 to 80 percent, and to add workers automatically when the length of the queue or the age of its oldest message grows, which is the scaling rule of lesson 7.
Independent check: the average times were recomputed in Python with a different method, the Lindley recursion, which computes the waiting time of each job from the previous one by W(n+1) = max(0, W(n) + S(n) – A(n+1)) over 600,000 random jobs with a separate random generator. The results, 0.200, 0.500, 0.969 and 1.967 seconds, agree with the theory and with the JavaScript simulation within sampling noise, which grows with utilisation because the queue is slow to settle near the limit. Both are consistent with the exact formula. The model assumes random arrivals and one worker; with several workers the waiting is much shorter at the same utilisation, which is one reason to prefer a pool of small workers to one big one.
Check by hand: with λ = 8 and μ = 10, ρ = 0.8, W = 1 / (10 – 8) = 0.5 seconds, and by Little’s law L = 8 x 0.5 = 4 jobs. If the load rises to λ = 9.5, W = 1 / 0.5 = 2 seconds and L = 19 jobs, so a 19 percent increase in load made the time four times longer.
A queue absorbs a burst and also hides a system that cannot keep up. Watch the age of the oldest message, and alert when it grows for several minutes.
Queues deliver a message at least once, so processing it twice must be harmless. Use a job id, and record that it was done before sending the email.
No cloud account is needed for this course. The cloud services in these lessons are small simulations that run on your own computer, in Python 3.13 or Node.js 22, because the ideas, such as access policies, queues, autoscaling and templates, are the same at every provider. Prices and names are invented or generic, and each provider’s services and prices change often, so check the current documentation of the provider you use. Every result was checked a second way. The formula W = 1 / (μ – λ) applies to the single-worker model with random arrivals and service. Real systems differ, so measure.
Practice
Find the largest arrival rate for which the average time stays below 0.25 seconds with μ = 10. Simulate two workers sharing a queue at the same total utilisation and compare. Add a limit of 5 jobs waiting and count the rejected ones.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Why use a queue between a web server and slow work?
- It makes the work free
- It removes the need for workers
- It decouples them so bursts do not break checkout
- It encrypts messages
Respond fast, work later.
What does Little’s law state?
- W = λ L
- L = λ W
- L = W / λ
- L = λ + W
Jobs in system = arrival rate x time in system.
What happens to waiting time as utilisation approaches 100 percent?
- It stays the same
- It grows without limit
- It falls to zero
- It halves
W = 1 / (μ – λ).
Why must workers be idempotent?
- Messages may be delivered more than once
- To save memory
- Queues forbid state
- To speed up the queue
At-least-once delivery.
Why compare the simulation with a recursion and a formula?
- Independent confirmation of the result
- To lengthen the code
- The queue requires it
- To raise utilisation
Three methods agree.
Summary
- Queues decouple fast requests from slow work.
- Little’s law: L = λ W.
- Waiting time explodes as utilisation nears 100 percent.
- Verify simulations with a formula and a second method.
Questions
What is a dead-letter queue?
A second queue where messages go after failing several times, so a bad message does not block the others and can be inspected later.
How do I choose the number of workers?
Divide the arrival rate by the service rate of one worker and add margin so utilisation stays near 70 percent, then scale on the queue length.
Is a queue always better than calling the service directly?
No. It adds delay and complexity. Use it when the work is slow, bursty or can fail and be retried.
