Backpressure in JavaScript
4 months ago
- #JavaScript
- #Streams
- #Backpressure
- Backpressure is a system-level feedback mechanism that allows a consumer to slow down a producer when data is produced faster than it can be handled.
- Ignoring backpressure in JavaScript can lead to memory spikes, latency collapse, and crashes due to unbounded queues.
- Backpressure appears in Node.js streams, Fetch API, Web Streams, and async loops, but requires explicit handling.
- Common mistakes include ignoring `write()` return values, using `Promise.all()` on large datasets, and reading entire files into memory.
- Unix pipes, TCP flow control, and messaging systems like RabbitMQ also implement backpressure mechanisms.
- Node.js streams use `highWaterMark` to control buffer size and `drain` events to manage flow.
- `async/await` can mask backpressure issues if asynchronous tasks inside loops are not properly controlled.
- Web Streams in browsers use pull-based reading and `write()` promises to enforce backpressure.
- Buffers help manage short-term speed mismatches but can hide problems if misused or unbounded.
- Symptoms of backpressure issues include memory growth, latency spikes, and high CPU with low throughput.
- Designing backpressure-friendly code involves bounding concurrency, using pull-based designs, and monitoring flow.