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

Why Interviewers Ask This

Advanced questions like this reveal whether a candidate has internalized Node.js deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.

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.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Node.js project, I used this when...' immediately makes your answer more credible and memorable.