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
- DynamoDB Hot Partition That Throttled a Flash Sale
- Failover to a Lagging Replica Lost 4 Minutes of Orders
- Internal DNS Cache Poisoning That Redirected API Traffic
- Malicious npm Package Exfiltrated Environment Variables at Build Time
- N+1 Query Pattern That Collapsed Under Load
- NAT Gateway Port Exhaustion Under Bursty Lambda Traffic
- Public S3 Bucket Exposed Customer PII for 8 Months
- Silent Data Corruption from an Off-By-One Migration
- Single-Region Dependency That Caused a Global Outage
- The Cascade That Took Down Three Services
- Third-Party API Outage That Was Mistaken for Own Code
- Thundering Herd After a Cache Cluster Restart
Start free on Cariara · Coding problems · System design studio