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

← All Node.js Developer questions