Node.js Memory Leak That Required Daily Restarts

A closures-based event listener accumulation caused heap growth of 40 MB/hour, requiring nightly automated restarts to prevent OOM crashes.

Overview

A Node.js API crashed every night around 3 AM for months because every request added a listener to a shared Redis client and never removed it.

What happened

The nightly restart took only 30 seconds and hit low traffic, so engineers accepted it as normal. Scaling out made the pattern impossible to ignore. The story ran in five stages.

  • For months, the service became unresponsive around 3 AM.
  • A health check restarted it each time. The restart took 30 seconds and had low impact, so the team accepted it as normal.
  • The service was scaled from 2 to 8 instances. The instances had started at different times, so the crashes were now staggered all through the day instead of bunched at night.
  • The team took a heap snapshot at startup and another one 4 hours later, and compared them.
  • The objects that kept growing were listener arrays on the shared Redis client.

Key concepts

  • Heap snapshot
  • Event listener accumulation
  • Closure reference leak
  • MaxListeners warning

Interview questions

  • A Node.js service is growing in memory usage by 50 MB per hour. How do you diagnose and fix it?

Sign up free to read the full topic

More War Stories topics

Start free on Cariara · Coding problems · System design studio