When dealing with large files, video feeds, or high-throughput network packets, loading the entire payload into RAM via fs.readFile() will instantly exhaust server memory and crash the process. Node.js solves this with Buffers (raw binary memory) and Streams (processing data chunk-by-chunk in real time).
A Buffer is a fixed-size chunk of memory allocated outside the V8 JavaScript heap. It stores raw binary bytes (octets):
fs.createReadStream, http.IncomingMessage).fs.createWriteStream, http.ServerResponse).zlib.createGzip, crypto cipher).pipelineUsing .pipe() is convenient, but if a stream errors mid-transmission, old .pipe() calls often leak open file descriptors. Modern Node.js standardizes on stream.promises.pipeline to handle errors and cleanup automatically:
If a Readable stream produces data faster than the Writable destination can write to disk or network (e.g. reading from an NVMe SSD at 3 GB/s and sending over a 10 Mbps internet connection), incoming chunks accumulate in RAM. Backpressure pauses the readable stream until the writable stream drains its buffer, maintaining a constant, predictable memory footprint under 30 MB regardless of file size.
Buffer allocated?fs.readFile() when handling multi-gigabyte files in a backend API?zlib.createGzip(), which takes uncompressed bytes and outputs compressed bytes?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| EventEmitter Pattern & Custom Event Architecture | Native HTTP Server vs Express Framework Philosophy |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.