In this lesson · topic 2 of 7
Topic 2
Four queues and four signals
6 min read
From a growing queue to a refusal
Step through what a queue does to a request as the load rises, and what a server can say back.
What a queue does to a request
Step 1 / 5- queue
- 2
- waiting too long
- 0
Light load
Requests arrive a little slower than the server finishes them, so the queue is short and every client gets an answer well inside its timeout.
More work than capacity
Now requests arrive faster than the server can finish them. The queue only grows, and each new request waits behind all the ones before it.
Waiting past the timeout
The oldest requests have waited longer than their clients will. Those clients have given up, but the requests are still in the queue, and a worker will spend a full service time on each one. That work helps nobody.
Refuse at the door
A bounded queue stops this. When it is full the server answers at once with 429, and Retry-After says when to try again. The client learns about the overload immediately, and the requests already queued still have time left.
Let the consumer set the pace
With credits the server states in advance how much it can take. The client sends no more than its credit, so the queue cannot overfill. This is backpressure that travels up the pipeline before anything is refused.
- Light load. Requests arrive a little slower than the server finishes them, so the queue is short and every client gets an answer well inside its timeout. queue: 2; waiting too long: 0.
- More work than capacity. Now requests arrive faster than the server can finish them. The queue only grows, and each new request waits behind all the ones before it. queue: 8; waiting too long: 0.
- Waiting past the timeout. The oldest requests have waited longer than their clients will. Those clients have given up, but the requests are still in the queue, and a worker will spend a full service time on each one. That work helps nobody. queue: 10; wasted at the front: 4.
- Refuse at the door. A bounded queue stops this. When it is full the server answers at once with 429, and Retry-After says when to try again. The client learns about the overload immediately, and the requests already queued still have time left. response: 429, Retry-After 2.
- Let the consumer set the pace. With credits the server states in advance how much it can take. The client sends no more than its credit, so the queue cannot overfill. This is backpressure that travels up the pipeline before anything is refused. credits: 3; queue: 3.
The four policies
Unbounded FIFO. Accept everything, serve the oldest first. It is fair and it needs no tuning. Under overload it is the one that collapses, as the last topic showed.
Bounded queue, reject. Allow only so many requests to wait. When the queue is full, answer the next one at once with an error. The size is the lever. A queue of N on a server that finishes C per unit makes a wait of about N divided by C. Keep that comfortably under the client timeout and everything admitted has a chance to finish. The cost is that some callers are refused immediately, who would have been refused anyway once the queue grew.
Deadline-aware drop. Keep the queue, but when a worker takes the next request, check whether it can still finish before its caller gives up. If not, drop it without doing the work. Dropping is nearly free, so the worker moves on to a request that can still succeed. This policy needs to know each request’s deadline, which means the client has to send it along with the request. In the lab a request is skipped when its start time plus a service allowance would pass its deadline.
Adaptive LIFO. While the queue is short, serve oldest first. When it grows long, serve the newest first. Under overload, the newest request is the one whose caller has waited the least and is most likely still there. The oldest requests are the likeliest to have been abandoned already. Serving newest first means the work that gets done is work someone wants. The cost is fairness: the requests at the bottom of the pile wait the longest and may never be served.
How a server says no
Refusing is only useful if the caller understands it and acts on it. These are the usual signals.
An error status. HTTP defines 429, Too Many Requests, for a caller that is sending too much, and 503, Service Unavailable, for a server that cannot take the request right now. Use the one that is true. A 429 says “you are the problem”, and a 503 says “I am the problem”.
Retry-After. A response header that tells the client when to try again. On a 429 or 503 it turns a refusal into a schedule. Without it every client picks its own delay, and they tend to pick the same one.
Fail fast. A refusal should be quicker than serving the request. An error that takes as long as the work gives the server nothing back.
Credits and pull. In a pipeline you can stop the problem further up. With credit-based flow control the consumer grants the producer a number of credits, and the producer may send only that many. With pull-based designs the consumer asks for the next items when it is ready, and the producer waits. In both, the producer cannot overrun the consumer, because it has no permission to. The Reactive Streams specification builds this in, as a request for a number of items from the downstream side.