function writeMany(stream, lines) {
let i = 0;
function go() {
while (i < lines.length) {
if (!stream.write(lines[i++])) {
return stream.once("drain", go);
}
}
stream.end();
}
go();
}
Backpressure is the problem when a writable destination can't take data as fast as the source produces it. Without control, the unwritten chunks pile up in memory until the process runs out of memory.
Node handles it like this:
- Each stream has a buffer limit called
highWaterMark. writable.write(chunk)returnsfalseonce the internal buffer reaches that limit.- A well-behaved producer then stops writing. It waits for the
drainevent before it writes again. - On the readable side, Node stops reading from the underlying source when the readable's buffer is full.
pipe() and pipeline() do this for you. They pause the readable when write returns false, and they resume it on drain. You only handle backpressure by hand when you call write yourself in a loop.
- When the producer is faster than the consumer, buffered data grows without limit.
highWaterMarksets the buffer limit.- When
write()returnsfalse, stop and wait for thedrainevent. pipeandpipelinemanage backpressure automatically.