Node.js Internals8h estimated

Streams & Backpressure

Difficulty
Importance

The problem

Reading an entire large file or request body into memory before processing it wastes memory proportional to the data's size and delays any processing until the whole thing has arrived — streams let data be processed in small chunks as it arrives, but that only works safely if a fast producer can be told to slow down for a slow consumer.

Why now

File systems and I/O already established that reading and writing happens through the OS, often in chunks rather than all at once; streams are Node's abstraction over exactly that chunked reality, and libuv's event loop is what delivers each chunk asynchronously without blocking — backpressure is the missing piece neither of those alone provides: a signal that flows backward from consumer to producer.

Mental model

A stream is a sequence of chunks arriving over time instead of one big value arriving all at once — read one piece, do something with it, ask for the next. Backpressure is the stream's internal buffer telling the source 'stop sending, I'm full' the moment a consumer can't keep up, and 'resume' once there's room again — without it, a fast producer (a fast disk) writing to a slow consumer (a slow network client) would buffer unboundedly in memory until the process crashes.

Requires

Used in

Projects

  • Stream a large file directly to an HTTP response using .pipe() and confirm via memory profiling that the whole file is never held in memory at once
  • Deliberately write to a stream faster than its destination can drain, observe backpressure (write() returning false), and correctly pause/resume based on it

Examples

  • fs.createReadStream(largeFile).pipe(response) streams a multi-gigabyte file to a client using a small, constant amount of memory, regardless of file size
  • A slow network client causes a fast file-read stream's internal buffer to fill; without honoring backpressure, an unthrottled writer would keep buffering in memory until the process runs out

Resources

Mastery checklist