Backpressure
A way for a receiver to slow down incoming work when it cannot keep up. It helps keep queues and waiting times from growing unchecked.
Also known as: обратное давление
What backpressure means
Imagine a service reading records from a file and writing them to a database. Reading is fast, but the database can handle only so many writes at once. If the reader keeps going at full speed, the records waiting to be written pile up. A buffer can absorb a brief spike, but it only buys time when the writer is consistently slower than the reader.
Backpressure is what happens when the busy stage causes an earlier stage to slow down. Data flows toward the consumer; information about available capacity flows back toward the producer. That feedback can be explicit, such as a request for more items, or implicit, such as a write operation that waits for space.
How it works
- Request only what you can handle. In Reactive Streams, a subscriber calls
request(n)to allow the publisher to send up tonadditional items. It can ask for more as it frees up capacity. - Wait when a queue is full. A bounded queue can make a producer wait until a consumer removes an item. In an asynchronous system, the wait can suspend a task without tying up a thread. This works only if the producer actually waits instead of creating more pending writes elsewhere.
- Reject work that cannot wait. An overloaded API can return
503 Service Unavailablerather than accept more requests. Rejection protects the server immediately, but it slows the caller only if the caller backs off. Retrying immediately can make the overload worse.
Example: importing rows into a database
An importer reads at 1,000 rows per second, but the database writes only 600. A queue with room for 4,000 rows fills in about 10 seconds if both rates stay steady. Once the queue is full, the importer waits for space before reading another batch. Over time, it reads at roughly the rate the database can sustain. The import does not finish faster; the benefit is that pending rows no longer accumulate without bound.
Where it shows up
Backpressure is common in stream processing, background job systems, and network protocols. In TCP, the advertised receive window tells the sender how much more data the receiver can accept. That protects the TCP receive buffer, not every queue in the application: an app that drains its socket into an unbounded work queue can still run out of memory.
A fixed rate limit is different. It caps how much traffic can arrive in a given period, whether the receiver is idle or overloaded. Backpressure responds to the receiver's current capacity. The two can be used together.
What interviewers look for
In a system design interview, expect to explain what happens during a short burst and what happens if the overload lasts for minutes. Say where work can accumulate, how you bound both queued and in-flight work, and which component pauses or rejects it. If you propose dropping data, explain why that's acceptable. Queue depth, the age of the oldest item, and rejection rate help show whether the system is catching up or falling further behind.