Node.js API Gateway Memory Leak Fixed by Scoping Event Listeners Per Request
A Node.js API gateway handling TLS termination, authentication, and microservice fan-out developed a gradual memory leak over several weeks, causing Kubernetes to OOM-kill pods under peak traffic. Heap snapshots revealed accumulating closures, request metadata, and event listeners tied to an internal event bus where an early-exit authentication path skipped cleanup, leaving listeners holding live references. Engineers resolved the issue by creating short-lived, request-scoped EventEmitters, enforcing teardown through try/finally blocks, and adding leak-aware CI tests that asserted bounded object counts across thousands of simulated requests. Operational safeguards including backpressure on accept queues, a soft memory threshold, and crash-loop rollback halting were also introduced. Following these changes, memory usage stabilized, pod restarts ceased, and autoscaling returned to managing load rather than concealing the underlying bug.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in