Backpressure
Control de flujo · Colas acotadas
Escrito por Darjus Kazakevic · revisado el
A bounded queue that says no is safer than an unbounded one that says yes.
Every pipeline has a slowest stage. Everything before it produces faster than it can consume, and the difference piles up somewhere: an array, a socket buffer, a queue with no limit. The system looks healthy until it runs out of memory and loses everything it held. An unbounded buffer adds no capacity. It turns a visible slowdown into a later crash.
Backpressure makes the slowdown visible. A queue has a maximum size. When it is full, the producer waits, or gets a 503 and retries later. The cost is that “not now” becomes a normal answer that callers must handle, and someone must choose the limit.
En esencia — When a consumer is slower than its producer, push the slowdown upstream with bounded queues and refusals, instead of buffering without limit until memory runs out.
Cuándo recurrir a él
Recurre a él cuando
- A pipeline stage is slower than the stage that feeds it
- Bursts arrive faster than workers drain them, and memory is finite
- A queue between two services has no maximum size today
Reconsidéralo cuando
- Producers that can't slow or retry — buffer durably or sample instead
- Per-client abuse — limit each caller at the edge with a rate limiter
- Bursts that a durable log, like Kafka, can hold on disk for hours
Conceptos clave
- Bounded queue
- A buffer with a fixed maximum size. When full, it rejects or blocks new items instead of growing. Its size is really a wait time: capacity ÷ drain rate.
- Block, reject or drop
- Three answers to a full queue. Block the producer until there is room, reject with an error such as 503, or drop items. Choose by whether the producer can wait and whether the data can be lost.
- Pull-based flow (Credit-based flow control)
- The consumer asks for the next N items when it has room, so the producer never sends more than it can take. Kafka consumers poll, HTTP/2 uses flow-control windows, and Reactive Streams uses request(n).
- Load shedding
- Refusing work at the edge, before it enters the system, when queues or latency pass a limit. A request refused in 1 ms costs far less than one that times out after 30 seconds.
Reglas de diseño
- Bound every queue
- Give every queue, buffer and pool a maximum size, including the ones hidden in libraries. When it is full, refuse with a 503 and a Retry-After header, so the producer slows down.
- Size the bound as a wait time
- Pick the longest wait a caller accepts, then multiply it by the drain rate. Ten seconds at 100 items per second gives a cap of 1,000.
- Reject early, at the edge
- Refuse work at the first hop, before it uses a connection, a thread or a database query. Work you accept and then time out on costs the most of all.
- Watch the age of the oldest item
- Alert on how long the oldest item has waited, not only on the count. A queue of 500 is half a second at 1,000 per second, and over 8 minutes at 1 per second.
Errores habituales
- A bounded queue in front of an unbounded one. The limit moves, but memory still grows in the hidden buffer.
- Producers that retry a refusal at once. They hammer the full queue; use Retry-After, backoff and jitter.
- A bound sized by memory, not by time. The queue holds a million items, and each one waits three hours.
- Async code with no limit on concurrency, such as Promise.all over 50,000 items. Every task starts at once.