Sub topic 5
Back Pressure: Slowing the Producer
This chapter assumes you can read short Python examples, know the producer/consumer pattern, and understand basic asyncio (async, await, gather). It builds on the previous chapter, where a bounded queue was introduced as a limit on waiting work.
What back pressure is
Back pressure is a signal that travels from a slow consumer back to the producer that feeds it. The signal tells the producer to slow down or wait. The producer's rate then matches what the consumer can actually finish.
Without back pressure, a fast producer keeps adding work and the gap grows. With it, the producer is forced to wait, so the system stays at a steady size.
Back pressure with a bounded queue
The simplest mechanism is a bounded queue whose put() waits when the queue is full. The producer is suspended until the consumer takes an item and frees a slot.
import asyncio
async def producer(queue: asyncio.Queue, count: int) -> None:
for i in range(count):
await queue.put(i) # suspends while the queue is full
await queue.put(None) # sentinel: no more work
async def consumer(queue: asyncio.Queue) -> None:
while True:
item = await queue.get()
if item is None:
break
await asyncio.sleep(0.01) # simulate slow work
async def main() -> None:
queue = asyncio.Queue(maxsize=10)
await asyncio.gather(producer(queue, 100), consumer(queue))
asyncio.run(main())The producer could generate 100 items almost instantly. Instead, it fills the queue with 10 items and then waits. From then on it adds one item for each item the consumer finishes. The consumer sets the pace, and memory use never exceeds 10 queued items.
Why it works
Back pressure relies on two facts:
- The wait happens in the producer. The producer decides what to do while it waits, and it stops creating new work.
- The wait propagates. If the producer is itself a consumer of an upstream stage, its slowdown pushes the signal one stage further back.
In a pipeline of several stages, each with a bounded queue, a slow last stage eventually slows the first one. Nothing in the middle needs to know the global rate.
Where it fits and where it does not
Back pressure only helps when the producer can actually wait. That is true for:
- Tasks reading from a file, a database cursor, or a message broker, because the source keeps the data until you ask for it.
- Internal pipeline stages that you control.
It is a poor fit when the producer cannot wait. An end user clicking "submit" will not hold still, and a thousand independent clients do not coordinate with each other. If you make their requests wait in a queue, the waiting requests still use memory and connections, and their latency grows. For that case you need load shedding, which a later chapter covers.
Common errors
- Unbounded queue by default.
asyncio.Queue()hasmaxsize=0, which means no limit. Always setmaxsize. - Blocking with no timeout. A producer that waits forever on a stuck consumer is stuck too. Pair
put()with a deadline, for exampleasyncio.wait_for(queue.put(item), timeout=5). - Too large a bound. A queue of 1,000,000 items is bounded in name only. The delay it adds equals its length divided by the consumer's rate, as Little's law predicts.
- Hiding the wait. If the wait is not measured, a slow producer looks like a healthy one. Record how long
put()blocks.
Exercise
A producer calls await queue.put(item) on asyncio.Queue(maxsize=20). The consumer takes 50 ms per item. About how many items per second does the producer manage once the queue is full, and what sets that number?
Show answer
About 20 items per second (1 / 0.05 s). Once the queue is full, the producer can add an item only when the consumer removes one, so the consumer's speed sets the producer's rate.
Exercise
Why is back pressure a poor fit for requests from many independent web clients?
Show answer
The clients cannot be made to wait in a coordinated way. Queued requests still hold connections and memory, and their latency grows, so the service should reject some of them instead (load shedding).