Backpressure – When Systems Learn to Say No

A digression accompanying the Scalability series.

A red traffic light glowing at night, close-up Photo: Erwan Hesry @ Unsplash - Unsplash licence

There have been traffic lights on German motorway on-ramps for some years now, regulating the flow of traffic onto the motorway. When the motorway is full, the lights turn red and only allow one car through every few seconds. Cars on the slip road wait. Meanwhile, the cars on the motorway keep moving.

Nobody enjoys waiting at that light, and hardly anyone believes it does any good – yet it measurably helps. It is exactly this counterintuitive logic that underpins one of the most effective patterns against overload.

The principle is called backpressure. A system that cannot take any more passes this on upstream so that the sender slows down. The alternative would be to accept everything and collapse under the load.

Make them wait, meter, turn away

In software, there are essentially three ways of passing the pressure back.

The simplest is to make the sender wait. Requests end up in a queue with limited capacity, and anyone trying to write to a full queue blocks until there is room again. This smooths out short load peaks, and the sender slows down of its own accord. The price is waiting time, which adds to the response time. An unbounded queue does none of this: the problem piles up in it unseen until memory runs out.

Sender, a full queue with six occupied slots, and service. The sender's arrow ends at a red barrier in front of the queue; the sender is blocked. Make them wait: the queue is full, and anyone trying to write to it blocks until there is room again.

It is more elegant if the receiver announces how much it can handle. TCP has been doing this for decades (flow control, RFC 793, since 1981): the receiver tells the sender how much room is left in its buffer, and the sender sends no more than that. Reactive Streams applies the principle to data streams within an application – the consumer explicitly requests how many elements it wants next. That is the traffic light on the on-ramp: the motorway sets the pace.

Sender and service. A dashed arrow from the service to the sender reports “room for 3”; an arrow from the sender to the service carries exactly three requests. Meter the flow: the receiver announces how much it can take, and the sender sends no more than that.

The third way is the crude one: turning requests away. The service responds with HTTP 503 (“Service Unavailable”) or 429 (“Too Many Requests”) and, at best, uses Retry-After to say when another attempt is worthwhile. Brutal, but honest. The client knows immediately where it stands. That is better than waiting 30 seconds for a timeout and then getting an error message anyway.

Sender and service. The sender's request bounces off the service; a red arrow comes back with 503 or 429 and Retry-After: 30. Turn away: the service refuses right away and says when a retry is worthwhile.

Rate limiting is related, but not the same thing. It restricts the number of requests per unit of time, typically per client or API key, regardless of how the service is doing at that moment. A token bucket algorithm allows short bursts but enforces a fixed limit on average. This protects the service from individual clients that generate a disproportionate amount of load – whether through misconfiguration, aggressive crawlers or a bug in some retry logic. Backpressure responds to the state of the system, rate limiting to the behavior of a client.

When do you need this?

At the saturation point. Kingman’s formula shows that waiting times grow disproportionately the closer a system gets to its capacity limit. If more load arrives permanently than it can work off, the queue grows without end. Without backpressure, the service keeps accepting requests regardless. Threads block on database connections, and response times rise for all requests – including those the service could actually still have served.

On top of that, much of the work is then wasted. If the client gave up after ten seconds, the service may well still be computing its response. It is fully utilized and yet delivers hardly anything that anybody still wants (Amazon Builders’ Library).

With backpressure, some requests are turned away or slowed down, and the remaining ones receive their response in a normal amount of time. In an emergency department, this is called triage.

Connection pools: backpressure as a side effect

The connection pool is a textbook example of backpressure that nobody planned as such. When all connections to the database are in use, the next thread either waits for a free connection – or receives an exception after a timeout. The pool limits the amount of load that is passed on to the database.

This is backpressure. It is just not explicitly designed, but arises as a side effect of resource limitation. This is exactly why it often performs poorly: the timeout is set too high (30-second default in some frameworks), there is no useful error message for the client, and the blocked threads occupy their place in the thread pool until it is also exhausted.

Anyone who configures connection pool timeouts deliberately – short enough not to block the thread pool, long enough to buffer short peaks – is essentially doing backpressure design. Most teams do it without being aware of it.

The uncomfortable questions

Clean as the principle is, in practice it raises uncomfortable questions.

Who gets turned away? A blanket HTTP 503 hits all requests equally – the user who is just about to pay as much as the bot crawling the sitemap for the third time. The answer to this is load shedding: the service deliberately discards the unimportant requests to keep capacity free for the important ones. That is considerably more complex. Somebody has to define what an “important” request is, and the decision has to be made faster than processing the request would take.

What happens afterward? The rejected request does not disappear. Either the user gives up, or the client tries again and generates additional traffic – at the very moment the system is overloaded. Retries without backoff and jitter are a reliable way of turning a brief overload into a long one.

Can the request be repeated? Retries only work for idempotent operations. A search query can be repeated safely. With an order, it depends on whether the system detects duplicates. Without idempotency, every retry is a risk.

What all three questions have in common is that backpressure shifts complexity from the server to the client. The service becomes simpler – it says no and is done. The client now has to decide how long to wait, how often to retry and what to show the user. Retry logic, idempotency, timeout handling and fallbacks all end up with the caller.

In a microservice architecture, where every service is a client of another, this complexity multiplies along the entire call chain. Anyone who introduces backpressure without thinking about the client side is moving the problem rather than solving it.


Sources

Comments