CodePark/ questions / Streams & backpressure
START PRACTICINGPRACTICE
Backend question · Node.js › Streams & backpressure

What is backpressure in streams, and how does Node handle it?

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) returns false once the internal buffer reaches that limit.
  • A well-behaved producer then stops writing. It waits for the drain event 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.
  • highWaterMark sets the buffer limit.
  • When write() returns false, stop and wait for the drain event.
  • pipe and pipeline manage backpressure automatically.