Full answer
Every stream has an internal buffer. The size limit is highWaterMark (64 KB for byte streams since Node 22, 16 KB before that, and 16 objects in object mode). The limit is a hint, not a hard stop.
When you call writable.write(chunk), Node adds the chunk to the buffer and returns a boolean. true means "the buffer is below the limit, keep going". false means "the buffer is full, please stop". If you ignore false and keep writing, the data piles up in memory. A fast file reader feeding a slow network client can fill gigabytes of RAM that way.
The correct pattern is:
- Call write().
- If it returns false, stop writing.
- Wait for the 'drain' event, then continue.
On the reading side the same idea exists. When pipe or pipeline sees false from the destination, it pauses the readable. On drain it resumes it. You get this for free when you use pipe, pipeline or async iteration with await.
Backpressure matters most when the producer is faster than the consumer, for example reading a local disk and writing to a slow HTTP client, or reading a CSV and inserting rows into a database that handles 500 rows per second.
const { Writable } = require('node:stream');
const { once } = require('node:events');
// A slow consumer: each chunk needs 5 ms. The buffer limit is 10 bytes.
function makeSlowWriter() {
return new Writable({
highWaterMark: 10,
write(chunk, encoding, callback) {
setTimeout(callback, 5);
},
});
}
// 1. Watch the return value.
const probe = makeSlowWriter();
console.log('write #1 returned', probe.write('12345678'));
console.log('write #2 returned', probe.write('12345678'));
// 2. A producer that respects backpressure.
async function produce(writer, count) {
let pauses = 0;
for (let i = 0; i < count; i++) {
if (!writer.write('12345678')) {
pauses++;
await once(writer, 'drain'); // wait until the buffer empties
}
}
writer.end();
await once(writer, 'finish');
return pauses;
}
produce(makeSlowWriter(), 6).then((pauses) => {
console.log('producer paused', pauses, 'times while writing 6 chunks');
});Each chunk is 8 bytes and the limit is 10 bytes, so every second write fills the buffer and makes the producer wait. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): write #1 returned true write #2 returned false producer paused 3 times while writing 6 chunks
Key points
- write() returns false when the buffer reaches highWaterMark.
- Stop writing on false and continue on 'drain'.
- pipe and pipeline apply backpressure automatically.
- Ignoring it causes unbounded memory growth.
Common mistakes
- Calling write() in a loop and never checking the return value.
- Thinking highWaterMark is a hard memory cap. It only triggers the false signal.
- Listening for 'drain' without stopping the loop first.
- Reading a whole file into memory and then calling res.write on it in one call.
Follow-ups
- How does pipeline apply backpressure between three stages?
- Why did Node raise the default byte highWaterMark from 16 KB to 64 KB in version 22?
- What happens to a Readable when its pipe destination is slow?
Tips
- Explain the three-step pattern: write, check false, wait for drain.
- Mention that async iteration (for await) and pipeline handle it for you.
- Name a real symptom: memory climbing while a slow client downloads a file.