Sub topic 3
Why unbounded queues make things worse
This chapter assumes you know Little's law from the previous chapter (items in system = arrival rate × time in system). It also assumes you can read short Python examples.
The tempting fix
A service gets a burst of work and can't keep up. The easy answer is to put the extra work in a queue with no size limit, so that nothing is rejected. For a short burst this works. If the arrival rate stays above the service rate, it fails.
Suppose requests arrive at 100 per second and the service finishes 80 per second. The queue grows by 20 items every second. Nothing in the system pushes back.
arrival_rate = 100 # requests per second
service_rate = 80 # requests per second
for second in (1, 10, 60, 600):
backlog = (arrival_rate - service_rate) * second
wait = backlog / service_rate
print(f"t={second:>3}s backlog={backlog:>6} wait={wait:6.1f}s")The output is:
t= 1s backlog= 20 wait= 0.2s
t= 10s backlog= 200 wait= 2.5s
t= 60s backlog= 1200 wait= 15.0s
t=600s backlog= 12000 wait= 150.0sThe service never stops working, yet the wait keeps growing. By Little's law, a longer queue means a longer time in the system.
Three ways it fails
- Latency grows without limit. A new request waits behind everything already queued. The queue is first-in, first-out, so every request pays for the backlog, including the ones that arrive late.
- Memory grows without limit. Each queued item holds a request object, a buffer, or a connection. When memory runs out, the operating system kills the process. The whole queue is lost, and the restart starts cold.
- Work is wasted. Clients have timeouts. Once the wait exceeds the client's timeout, the client gives up. The server still processes the request, and nobody reads the result. The server is busy, but useful work (goodput) drops toward zero.
The feedback loop
Failure often accelerates, because clients react to slowness by retrying.
Each retry adds a request while the abandoned original is still queued, so the load is higher than before. Later chapters cover retries in detail.
Why you don't see it coming
An unbounded queue hides overload. CPU may look normal, and no errors are returned. The first visible symptom is high latency, and by then the backlog is large.
Recovery is also slow. The queue drains only at the difference between service and arrival rates. If arrivals fall to 60 per second, a backlog of 12,000 drains at 20 per second. That takes 600 seconds, and clients see slow responses the whole time.
What to do instead
The remedy is to limit how much work waits. The next chapters cover bounding the queue, which gives the system a place to push back. They also cover deadlines, so the server skips work that nobody is waiting for.
Exercise
Requests arrive at 100 per second and the service completes 80 per second. Clients time out after 5 seconds. Using the queue growth in this chapter, after how many seconds does the wait for a newly arriving request exceed the client timeout? What happens to requests after that point?
Show answer
The backlog is 20 × t and the wait is (20 × t) / 80 = t / 4 seconds. The wait exceeds 5 seconds when t > 20. After that, each request waits longer than its client will, so the client has already given up when the server starts it. The server spends its capacity on responses nobody receives.