What is a Node.js Stream and why use one
Understanding chunked processing and memory efficiency.
A stream processes data in chunks over time, so memory stays bounded and work starts before all data arrives; ideal for large files and network IO.
WHY IT EXISTS: Reading a large file or response fully into memory with something like readFile does not scale: a multi-gigabyte file would exhaust RAM, and you cannot start processing until the entire read finishes. Streams exist to process data incrementally, so memory stays bounded and work can begin immediately.
THE MENTAL MODEL: A stream is a sequence of data chunks flowing over time, like water through a pipe rather than a bucket you fill before pouring. Node has four kinds: Readable (source), Writable (sink), Duplex (both), and Transform (a Duplex that modifies chunks as they pass). You connect them with pipe or the pipeline helper to build processing chains.
HOW IT WORKS: A Readable emits chunks as data becomes available; consumers read them and the stream applies backpressure if the consumer is slower, pausing the source so buffers do not grow unbounded. A Writable accepts chunks and signals when it is ready for more. Piping wires a readable to a writable and manages this flow automatically, including backpressure and, with pipeline, error propagation and cleanup.
WHEN IT MATTERS: Use streams for large file IO, serving or uploading files, parsing huge logs or CSVs, proxying HTTP, compression, and encryption, anywhere the data may be large or unbounded or where you want low time-to-first-byte. For small, known-small payloads, a one-shot read is simpler and fine.
ONE CONCRETE EXAMPLE: To send a 5 GB video, createReadStream(file).pipe(res) streams it chunk by chunk; memory stays at a few buffers regardless of file size, the client starts receiving bytes immediately, and backpressure throttles disk reads to match network speed. Reading the whole file first would risk an out-of-memory crash and delay the first byte until the full read completed.
Read the original → nodejs.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.