InterviewHubPRO
Section Overview
Q23

Question 23 of 60

Part 3: Streams, Buffers & Core Modules (21 - 35)

38%
Node.js & Libuv•Part 3: Streams, Buffers & Core Modules (21 - 35)

Stream Backpressure and `pipeline()`.

In Simple Words:

  • Backpressure: Occurs when a Readable stream produces data faster than a Writable stream can consume and flush it to disk/network, leading to RAM exhaustion.
  • Solution: Use pipeline from stream/promises. It manages backpressure automatically and properly destroys all streams if an error occurs.
javascript
const { pipeline } = require('stream/promises');
const fs = require('fs');
const zlib = require('zlib');

async function compressFile(input, output) {
  await pipeline(
    fs.createReadStream(input),
    zlib.createGzip(),
    fs.createWriteStream(output)
  );
  console.log('Compression complete with zero memory leak!');
}