DeployU
Interviews / Backend Engineering / What are streams and why are they useful in Node.js?

What are streams and why are they useful in Node.js?

conceptual Core Concepts Interactive Quiz Code Examples

The Scenario

You are a backend engineer at a video streaming company. You are building a new service that will allow users to upload and stream large video files.

You need to find a way to handle these large files efficiently, without loading the entire file into memory at once.

The Challenge

Explain what streams are in Node.js and why they are useful for this use case. What are the different types of streams, and how would you use them to build the video streaming service?

Wrong Approach

A junior engineer might try to solve this problem by reading the entire file into memory using `fs.readFileSync()`. This would be very inefficient and would likely cause the application to crash for large files.

Right Approach

A senior engineer would know that streams are the perfect tool for this job. They would be able to explain the different types of streams and would have a clear plan for how to use them to build an efficient and scalable video streaming service.

Step 1: Understand What Streams Are

Streams are a way of reading and writing data in chunks, without having to load the entire file into memory at once. This makes them very efficient for handling large amounts of data.

Step 2: The Different Types of Streams

There are four main types of streams in Node.js:

TypeDescription
ReadableA stream from which data can be read (e.g., fs.createReadStream()).
WritableA stream to which data can be written (e.g., fs.createWriteStream()).
DuplexA stream that is both readable and writable (e.g., a TCP socket).
TransformA stream that can be used to modify or transform the data as it is being read or written (e.g., a zlib stream).

Step 3: Build the Video Streaming Service

Here’s how we can use streams to build the video streaming service:

1. Create a readable stream for the video file:

const fs = require('fs');

const readStream = fs.createReadStream('my_video.mp4');

2. Create a writable stream for the HTTP response:

const http = require('http');

const server = http.createServer((req, res) => {
  // ...
});

3. Use pipe() to connect the two streams:

The pipe() method is a convenient way to connect a readable stream to a writable stream. It automatically handles the flow of data, so you don’t have to worry about backpressure.

readStream.pipe(res);

By using streams, we can efficiently stream the video file to the user without having to load the entire file into memory at once.

Practice Question

You are building a service that needs to compress a large file before sending it to the user. Which type of stream would you use?