WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Advanced · question 57 of 100

Explain the concept of ’backpressure’ in Node.js streams and how to handle it.?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

’Backpressure’ is a concept in Node.js streams that refers to a situation where a writable stream (e.g., the destination for the data) cannot process incoming data at the same rate as it is being produced by the readable stream (e.g., the source of the data). This imbalance can cause excessive memory consumption, as the data is buffered in memory until the writable stream can catch up, and also affect the performance of the entire system. To prevent these problems, it is essential to handle backpressure properly.

In order to manage backpressure, Node.js offers in-built mechanisms to regulate the data flow between streams without detrimental impact on the overall efficiency. Here’s how it works:

1. **Pausing and resuming the readable stream**: The core concept of dealing with backpressure involves pausing the readable stream when the writable stream signals that it cannot accept any more data, and resuming it when the writable stream is ready to accept data again.

When using the ‘pipe()‘ method, the pause and resume mechanism is built-in, so you don’t need to handle it manually. For example:

const readableStream = getReadableStreamSomehow();
const writableStream = getWritableStreamSomehow();

readableStream.pipe(writableStream);

However, when streaming data manually, you can use ’readable’ event and the ‘read()‘ method in combination with the writable stream’s ’drain’ event:

const readableStream = getReadableStreamSomehow();
const writableStream = getWritableStreamSomehow();

readableStream.on('readable', () => {
  let chunk;
  while (null !== (chunk = readableStream.read())) {
    const canWrite = writableStream.write(chunk);
    if (!canWrite) {
      readableStream.pause();
      writableStream.once('drain', () => {
        readableStream.resume();
      });
    }
  }
});

readableStream.on('end', () => {
  writableStream.end();
});

The flow works as follows:

- When the readable stream has data available (‘readable‘ event), we use the ‘read()‘ method to read chunks of data.

- We then attempt to write the chunk to the writable stream using the ‘write()‘ method. The ‘write()‘ method returns a boolean value (‘false‘ if the writable stream’s internal buffer has reached its high water mark).

- If ‘write()‘ returns ‘false‘, we know that the writable stream cannot accept any more data right now, so we pause the readable stream.

- To resume the flow of data, we listen for the ‘drain‘ event on the writable stream, which is emitted when it is able to accept data again.

- When the ‘drain‘ event is emitted, we resume the readable stream and continue the process.

2. **Optimizing buffer sizes**: Another way to manage backpressure is to configure the high water mark for both the readable and writable streams, using the ‘highWaterMark‘ option. This is the size of the internal buffer used by the stream, and setting it properly can help balance data flow between streams:

const { Readable, Writable } = require('stream');

const readableStream = new Readable({
  highWaterMark: 64 * 1024, // 64KB
});

const writableStream = new Writable({
  highWaterMark: 64 * 1024, // 64KB
});

Adjusting the ‘highWaterMark‘ values can help prevent excessive buffering and ensure smooth data transfer.

In summary, backpressure is an important concept in Node.js streams, and it is essential to handle it properly in order to maintain system efficiency and prevent excessive memory consumption. Built-in mechanisms in Node.js, such as pausing and resuming streams, as well as optimizing buffer sizes, can help alleviate backpressure issues.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics