What are Streams in Node.js?
Learn what Node.js streams are, the four stream types, how pipe() and backpressure work, and real production use cases with practical code examples.
Expected Interview Answer
Streams are Node's abstraction for processing data piece by piece (in chunks) as it arrives, rather than loading an entire file or response into memory at once, which keeps memory usage low for large or continuous data.
Node exposes four stream types: Readable (source of data, e.g. fs.createReadStream), Writable (destination, e.g. fs.createWriteStream or an HTTP response), Duplex (both, like a TCP socket), and Transform (modifies data as it passes through, like zlib gzip). Streams emit events such as 'data', 'end', and 'error', and can be chained with .pipe() to move data from a readable to a writable while automatically managing backpressure. Backpressure handling is critical: pipe() pauses the source if the destination can't keep up, preventing memory blowup. This makes streams ideal for large file transfers, video serving, and log processing.
- Low memory footprint for large data
- Processes data incrementally as it arrives
- Built-in backpressure handling via pipe()
- Composable via chaining (e.g. read -> gzip -> write)
- Powers HTTP requests/responses under the hood
AI Mentor Explanation
Streams are like a bucket brigade passing water pail by pail from the boundary rope to the pitch instead of one person hauling an entire tank at once. Each pail moves the moment it's filled, and if the person at the pitch end slows down, the chain naturally pauses upstream so nobody drowns in backed-up buckets.
Step-by-Step Explanation
Step 1
Choose a stream type
Pick Readable, Writable, Duplex, or Transform based on the direction of data flow needed.
Step 2
Create the source stream
e.g. fs.createReadStream(path) reads a file in chunks instead of all at once.
Step 3
Attach or pipe a destination
Use .pipe(writableStream) to send chunks downstream automatically.
Step 4
Handle backpressure
pipe() pauses the source automatically if the destination's internal buffer fills up.
Step 5
Listen for events
Handle 'data', 'end', and especially 'error' events to avoid silent failures.
Step 6
Chain transforms if needed
Insert a Transform stream (e.g. zlib.createGzip()) between source and destination to modify data in transit.
What Interviewer Expects
- Names the four stream types (Readable, Writable, Duplex, Transform)
- Explains why streams save memory vs loading full files
- Understands .pipe() and backpressure
- Can give a real use case (file upload, video, logs)
- Knows streams emit events like 'data' and 'end'
Common Mistakes
- Reading an entire large file into memory instead of streaming it
- Forgetting to handle the 'error' event on streams
- Not understanding backpressure and manually overwhelming a writable stream
- Confusing Transform streams with simple event listeners
Best Answer (HR Friendly)
“Streams let Node.js handle large amounts of data — like big files or videos — piece by piece instead of loading everything into memory at once. This keeps applications fast and efficient even when dealing with huge files or continuous data feeds.”
Code Example
const fs = require('fs');
const zlib = require('zlib');
fs.createReadStream('large-log.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('large-log.txt.gz'))
.on('finish', () => console.log('Compression complete'));
// Output:
// Compression complete
// (large-log.txt.gz written incrementally, low memory usage)Follow-up Questions
- What are the four types of streams in Node.js?
- How does backpressure work with .pipe()?
- When would you use a Transform stream?
- How do streams help with large file uploads in Express?
- What events does a Readable stream emit?
MCQ Practice
1. Why are streams preferred for large files?
Streams process data incrementally in chunks, avoiding loading the whole file into memory.
2. Which stream type both reads and transforms data?
A Transform stream is a Duplex stream that modifies data as it passes through, like gzip compression.
3. What mechanism prevents a fast source from overwhelming a slow destination?
Backpressure pauses the readable source when the writable destination's buffer is full.
Flash Cards
What is a stream in Node.js? — An abstraction for processing data incrementally in chunks rather than all at once.
Name the four stream types. — Readable, Writable, Duplex, and Transform.
What does .pipe() do? — Connects a readable source to a writable destination, handling backpressure automatically.
Give one real-world use of streams. — Streaming large file uploads/downloads or compressing files with zlib.