Section 9.30

Backpressure

Definition

Backpressure is feedback by which a consumer, the part of a system that processes work, makes its producer, the part that sends the work, wait, slow down or receive refusals when the consumer cannot keep up, instead of accepting work without limit. The signal travels upstream, against the flow of work: a full queue blocks or refuses the producer, or a receiver tells the sender how much more it may send. Backpressure keeps memory use and waiting time bounded when work arrives faster than it can be served.

The idea

A sensor task produces 5 readings per second, and a radio link can send only 3 per second. Between them sits a queue. If the queue may grow without limit, it gains 5−3=25 - 3 = 2 readings every second, which is 2×3600=72002 \times 3600 = 7200 readings after an hour. Memory runs out at some unpredictable moment. Long before that, each new reading waits behind thousands of older ones, so the readings that do arrive are stale. The radio cannot go faster, so the only choices left are about what the producer does.

The picture is a restaurant kitchen with a rail for order tickets. When the rail is full, the waiter stops taking orders, or tells new guests that there will be a wait, instead of pinning tickets to the wall. The kitchen’s limit is pushed back to the waiter, and from the waiter to the guests. The picture stops at one point: a guest who is turned away simply leaves, while a software producer that is made to wait may be holding something that the consumer needs, and then the wait never ends.

How it works

Backpressure has three parts.

  1. A measured capacity. The consumer’s side accounts for a limited resource at the point where work is admitted: queue slots, buffer bytes, the number of requests in progress, or credits.

  2. A signal to the producer.

    • Blocking: putting an item into a full bounded queue §9.31 makes the producer wait until space appears.

    • Refusal: the call returns “full” or “busy”. HTTP (Hypertext Transfer Protocol), the protocol of the web, has the status code 429, Too Many Requests, for this purpose.

    • Credits or windows: the receiver grants the sender permission for a number of items or bytes, and the sender may not exceed it until more is granted. TCP (Transmission Control Protocol), the Internet’s main reliable transport protocol, does this with its receive window, the number of bytes the receiver currently has room for. Bluetooth Low Energy §7.1 does it with credit-based flow control §7.16.

    • Pull: the consumer asks for more items when it is ready, as in the Reactive Streams interface, where a subscriber calls request(n) to accept nn more items.

  3. A policy for the producer when capacity is exhausted.

    • Block: wait until there is space. Nothing is lost, and the producer’s own delay grows.

    • Reject: pass the refusal to the producer’s own caller, which keeps the item and decides what to do.

    • Drop: discard the new item, or an old one, and count the loss. This fits data in which a newer value replaces an older one, such as sensor readings.

    • Retry: try again later, after a delay that grows with each failed try, called backoff, up to a fixed number of tries.

    • Slow the source: lower the rate at which work is created, for example by sampling less often.

Backpressure propagates. A producer that is blocked stops taking from its own input, which fills up, which blocks the stage before it, and so on toward the source. At the source the chain ends, and some sources cannot be made to wait: a radio packet arriving at a receiver, an interrupt §6.11 from a sensor, a person pressing a button. There, the system must drop or reject, and record that it did. Backpressure decides where loss happens. It does not make overload disappear.

Before letting a producer block, check what it holds. If the producer holds a lock while it waits for queue space, and the consumer needs the same lock to remove an item, neither can proceed: a deadlock §9.14. The same happens when two stages feed each other through two full queues, each waiting for the other to make space. Blocking is also forbidden in an interrupt handler §6.12, which must never wait, so an interrupt handler can only reject or drop.

Worked example

Admission. A bounded queue holds at most 4 items and currently holds 3. Two new requests arrive before the consumer removes anything. Accepting both would give 3+2=53 + 2 = 5 items, one more than the capacity of 4. Under a reject-new policy, the queue accepts the first request, reaching 3+1=43 + 1 = 4 items, and refuses the second, which goes back to its producer. When the consumer then removes 2 items, the queue holds 4−2=24 - 2 = 2 items, and new requests are accepted again.

Rates. Requests arrive at 5 per second, and the consumer serves 3 per second. An empty queue of capacity 100 fills at 5−3=25 - 3 = 2 requests per second, so it is full after 100/2=50100 / 2 = 50 seconds. From then on the consumer still serves 3 per second, and the other 2 of every 5 arrivals, that is 2/5=40%2 / 5 = 40\%, are blocked, refused or dropped, as the policy says.

Retries without delay. If every refused request is retried once, immediately, the queue receives 5+2=75 + 2 = 7 attempts per second, still serves 3, and refuses 7−3=47 - 3 = 4 per second. The refusals double, and not one extra request is served.

Stated precisely

Let λ\lambda be the rate at which the producer offers items and μ\mu the rate at which the consumer can serve them, both in items per second, and let KK be the capacity in items. For an interval of time, let qq be the number of items waiting at its start, aa the number admitted during it, and dd the number the consumer removed during it. The number waiting at the end is q′=q+a−d,q' = q + a - d, and backpressure keeps the number waiting between 0 and KK at all times. In the admission example, q=3q = 3, a=1a = 1 and d=0d = 0, so q′=3+1−0=4q' = 3 + 1 - 0 = 4. Counting the refused request as admitted would give 5, more than K=4K = 4, and would hide the refusal.

Every item the producer offers, or would offer at its own rate, ends in exactly one of four classes, so offered=admitted+refused+dropped+held,\text{offered} = \text{admitted} + \text{refused} + \text{dropped} + \text{held}, where held counts items not yet admitted because their producer is waiting or has slowed down. Over a period of TT seconds, every admitted item has either been served or is still among at most KK waiting items, so at most μT+K\mu T + K items are admitted. When λ>μ\lambda > \mu, on average at least λ−μ\lambda - \mu items per second must therefore be refused, dropped or held, once the period is long enough for KK to be negligible. Backpressure chooses among these three; it cannot shrink their total. The statement assumes that the producer obeys the signal. A producer that ignores it, through a fault or by intent, must be stopped by enforcement such as rate limiting §9.38.

Limits and common mistakes

Backpressure moves congestion; it does not remove it. Throughput stays at the consumer’s rate, and the waiting or the loss appears upstream.

Blocked producers form a hidden queue. Each blocked thread holds its item and its own stack, so letting an unlimited number of producers block recreates the unbounded queue in another place.

Blocking can deadlock, as described above, and is not allowed in an interrupt handler.

Retries need a delay that grows with each attempt, random variation of that delay (jitter) so that many producers do not retry in step, and a limit on attempts. Without them, refusals multiply the load, a failure known as a retry storm. A retried request must also be idempotent §8.31, harmless if applied twice.

With one shared queue, a single slow item or consumer can stall every producer behind it, a form of head-of-line blocking §9.19.

Backpressure assumes producers that cooperate. It does not authenticate a producer, and it does not stop a producer that ignores the signal; that needs rate limiting or admission control §19.4.5 where the work enters.

Backpressure bounds how much work waits, not how long an admitted item waits. A time bound also needs a bound on the consumer’s service time, as the Bounded queue entry shows.

Questions answered

Does the producer block, drop, reject, or retry when capacity is exhausted?

Any of the four is possible, and none is right everywhere: the design must choose one for each producer, document it, and make the outcome visible to the caller. Capacity is exhausted when the limited resource between producer and consumer, such as a bounded queue, is full.

Whatever the choice, a refused or dropped item must never be reported as delivered.

Related entries: Bounded queue (§9.31), Deadlock (§9.14), Rate limiting (§9.38), Head-of-line blocking (§9.19), LE credit-based flow control (§7.16), Idempotency (§8.31).