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.
Previous
What is the difference between horizontal and vertical scaling in Node.js?
Next
What is the Twelve-Factor App methodology as it applies to Node.js?
More Node.js Questions
View all →- Advanced How does Node.js handle concurrency without multiple threads?
- Advanced What is the Node.js memory model and how does garbage collection work?
- Advanced What are memory leaks in Node.js and how do you detect them?
- Advanced What is the difference between process.exit() and throwing an uncaught exception?
- Advanced What is the N+1 query problem and how do you solve it in Node.js?