Node.js Developer interview question · Most asked
What are streams in Node.js and what is backpressure?
Summary
Streams process data in chunks instead of loading it all into memory. There are Readable, Writable, Duplex and Transform streams. Backpressure is the mechanism that slows a fast producer when a slower consumer's buffer fills (write() returns false until 'drain').
Longer Answer
Use stream.pipeline() (or await pipeline() from stream/promises) to connect streams with proper error propagation and cleanup; plain .pipe() doesn't forward errors. Async iteration (for await (const chunk of readable)) is a modern consumption style. highWaterMark controls buffer sizes.
Code Example
import { createReadStream, createWriteStream } from "node:fs";
import { createGzip } from "node:zlib";
import { pipeline } from "node:stream/promises";
await pipeline(
createReadStream("access.log"),
createGzip(),
createWriteStream("access.log.gz"),
);
Key Points
- Chunked processing, constant memory
- Readable/Writable/Duplex/Transform
- Backpressure via
write()return +drain - Use
pipeline()for error handling
More most-asked Node.js Developer questions
- Why is CPU-bound work a problem in Node.js and how do you handle it?
- What are the differences between CommonJS and ES modules in Node.js?
- What is the difference between `process.nextTick`, Promise microtasks, `setImmediate` and `setTimeout(fn, 0)`?
- Explain the phases of the Node.js event loop.
- What is Node.js and why is it well suited to I/O-heavy servers?
- How has asynchronous code evolved in Node.js: callbacks, Promises and async/await?
- Walk through how an Express app handles a request, including errors from async handlers.
- How does middleware work in Express?