What is Node.js streams backpressure and how does pipe() handle it?

Answer

When a fast readable stream produces data faster than a slow writable stream can consume it, data accumulates in memory — this is the backpressure problem. Without handling it, the process runs out of memory. The writable.write(chunk) method returns false when its internal buffer exceeds highWaterMark — signaling the source to slow down. The writable emits "drain" when it is ready for more data. Manual handling: const ok = writable.write(chunk); if (!ok) { readable.pause(); writable.once("drain", () => readable.resume()); }. readable.pipe(writable) handles this automatically and correctly: it subscribes to the writable's "drain" event and pauses/resumes the readable stream accordingly. For complex pipelines, use stream.pipeline(src, transform, dest, callback) (Node.js 10+) instead of pipe() — it properly handles errors from any stage and cleans up all streams, whereas pipe() leaves streams open on error. Always prefer pipeline() over pipe() in production code.