Skip to main content

Posts

Showing posts with the label Streams

Node.js Memory Leaks: Handling Backpressure in Microservices with Stream Pipelines

  It is 3:00 AM. Your monitoring alerts are firing. Your Node.js microservice, responsible for processing large datasets or acting as a proxy between services, keeps crashing with   FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory . You restart the pod, but the memory graph forms a jagged saw-tooth pattern. It climbs steadily until it hits the container limit, crashes, and repeats. The culprit is rarely a "leak" in the traditional sense of uncollected garbage references. Instead, it is often a failure to handle  backpressure  in your I/O streams. In high-throughput microservices, assuming  source.pipe(dest)  will magically handle varying data speeds is a production-breaking mistake. This article details why Node.js buffers overflow and demonstrates a robust solution using modern Stream Pipelines and Async Iterators. The Root Cause: Why  .pipe()  isn't Enough To fix the OOM (Out of Memory) err...

Node.js Streams: Handling Backpressure to Prevent OOM Crashes in Large ETL Jobs

  You have built an ETL pipeline. It reads a 5GB CSV file, transforms the rows, and inserts them into a database or writes to a new format. In your local development environment with a sample dataset, it runs perfectly. You deploy to production, feed it the real dataset, and 45 seconds later, the process dies. FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory This is the classic "Fast Producer, Slow Consumer" problem. In Node.js, if you read data faster than you can write it, the excess data has to go somewhere. Without flow control, that "somewhere" is your RAM. Here is the root cause of the crash and the exact pattern to manage backpressure manually using modern Node.js APIs. The Root Cause: The Internal Buffer and  highWaterMark Node.js streams are not just event emitters; they are buffer managers. Every  Writable  stream has an internal buffer ( writableBuffer ). The size limit of this buffer is determined b...