What are streams in Node.js, and what are the four kinds? Why are they better than reading a whole file into memory, and what is backpressure?
Show the explanation
Model answer. A stream processes data piece by piece (chunks) as it arrives, instead of loading everything into memory first.
The four kinds:
- Readable: a data source (
fs.createReadStream, an HTTP request body). - Writable: a destination (
fs.createWriteStream, an HTTP response). - Duplex: both readable and writable (a TCP socket).
- Transform: a duplex stream that changes data as it passes (gzip, CSV parsing, encryption).
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';
await pipeline(createReadStream('logs.txt'), createGzip(), createWriteStream('logs.txt.gz'));Benefits: memory stays flat even for multi-gigabyte files, and the first bytes reach the client sooner.
Backpressure is what happens when the destination is slower than the source. A writable stream signals "I am full" (write() returns false), and the readable side should pause until 'drain'. pipe() and pipeline() handle this automatically. Ignoring it makes data pile up in memory. Prefer pipeline, because it also forwards errors and cleans up every stream if one fails.
Original UniCircle question. Last reviewed 22 Oct 2026.
