Backpressure
Also called: back pressure, flow control.
A way for a slow consumer to make a fast producer slow down, instead of letting work pile up until memory runs out. The consumer only accepts what it can handle, and the producer waits, keeps a bounded buffer or drops the extra.
The producer reads a CSV file at 10 rows a second, the consumer inserts 2 rows a second. Import with backpressure off, then turn it on and import again.
In the buffer: 0 · Inserted: 0 · Peak: 0
Nothing imported yet. Backpressure is off.
Say it in a prompt
Add backpressure to the CSV import: read rows as a stream into a bounded buffer of 1,000 rows, pause reading while the buffer is full and resume when it drains below 500. Insert in batches of 200 with at most 4 inserts in flight. Vague vs precise prompt
Vague prompt
read the CSV as a stream so the import stops crashing Typical resultReads the file as a stream but passes each row on without waiting. It reads 10,000 rows a second and the database saves 2,000, so unsaved rows pile up in memory and a big file still crashes it.
Precise prompt
Stream the CSV with backpressure: a bounded buffer of 1,000 rows, pause reading when it is full, resume below 500. Insert in batches of 200 with at most 4 in flight. Typical resultMemory stays flat whatever the file size, reading pauses whenever the database falls behind, and a 10 GB file imports like a small one, only slower.
Seen on
- Node.js: Its streams guide explains backpressure: write() returns false once the buffer passes highWaterMark (16 KB by default), and the source should wait for the 'drain' event.
- Reactive Streams: A standard for asynchronous stream processing with non-blocking back pressure, so the queues between threads can stay bounded.
You might describe it as
- the reader is faster than the database can save
- slow down the sender when the receiver can't keep up
- work piles up faster than we can process it
Not to be confused with
- Rate limiting
Backpressure slows the sender down based on how busy the receiver is right now; rate limiting is a fixed allowance per client, and extra requests are refused.
- Connection pool
Backpressure is the slow side telling the fast side to slow down; a connection pool caps how many requests can use the database at the same time.
- Streaming results
Backpressure makes a fast producer wait for a slow consumer; streaming results reads big data in small pieces so memory stays small. A stream with no backpressure can still fill memory, because rows read fast pile up before the slow side handles them.