DevAcademy
IntermediateNode.js

Streams

Process data piece by piece instead of loading it all into memory at once.

Reading Time

14 min

Lesson

Lesson 11 of 34

Why Streams Exist

Reading a 2GB file with readFile() loads the entire file into memory before you can do anything with it. A stream instead processes data in small chunks as they arrive, keeping memory usage low regardless of the total size.

Four Types of Streams

TypeExample
ReadableReading a file, an incoming HTTP request body
WritableWriting to a file, an HTTP response
DuplexBoth readable and writable, like a TCP socket
TransformA duplex stream that modifies data as it passes through, like gzip compression

Reading a File as a Stream

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Piping Streams Together

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Backpressure

If a writable stream can't keep up with an incoming readable stream (like a slow network connection receiving a fast file read), .pipe() automatically pauses the readable side until the writable side catches up — this is called backpressure, and it prevents memory from ballooning unbounded.

Best Practice

Use streams (and .pipe()) for anything involving large files or network data — uploads, downloads, video, log processing — rather than loading everything into memory with the non-streaming fs methods.

Interview Questions

Quick Quiz

1. What advantage do streams have over reading an entire file into memory at once?

2. What does .pipe() do?

3. What is backpressure in the context of streams?