I let our Node service cache HTTP responses in a Map with no size limit. Then I load-tested it and watched the RSS grow from 3.4 MB to 200.1 MB. Here is what happened and the fix that kept memory flat at 13.6 MB.
The test setup
The service was a small Node.js server with an in-memory response cache: a plain Map keyed by request URL. Every request that came in got its response stored so repeat requests could skip the work. Simple, and completely unbounded.
I hammered it with autocannon: 4,000 requests, each with a unique URL, so every single response got added to the cache and nothing was ever evicted.
What the memory did
The result was a textbook leak shape:
- Start: 3.4 MB RSS
- After 4,000 requests: 200.1 MB RSS
- No plateau. Memory grew linearly with request count, because the Map never let anything go.
Every unique key was a permanent allocation. The garbage collector could not touch any of it because the Map was still reachable and still growing.
The fix
Two changes:
- Bounded cache: a size cap on the cache. Once it hits the limit, the oldest entries get evicted.
- Same 4,000-request autocannon run against the fixed version.
Result: RSS stayed flat at 13.6 MB through the entire run. Same traffic, same throughput, no growth.
The takeaway
An unbounded in-memory cache is a memory leak with extra steps. It works fine in dev, passes tests, and then eats your server in production the first time real traffic shows up. If you cache in memory:
- Set a hard size limit and an eviction policy (LRU is usually the default you want)
- Add a TTL if the data goes stale
- Load-test it with unique keys before you ship, not after your pod gets OOMKilled
The full experiment, with the code and the live test runs, is in the video:
We are building Ship Stack: real experiments on real backends, with the actual numbers, no hand-waving. More of these are on the channel.
