27. 11. 2026
How a tweak in Node.js memory allocation blew up our Next.js app
Author: Tomáš Bořek
Tags: engineering-diary, software-engineering
This article is about how a seemingly innocent Node.js change surfaced a latent Next.js issue, which nearly took down our app, and how we mitigated it.
Node.js uses a buffer pool system to improve the performance of memory allocations. Instead of asking the system to allocate heap memory for every entry, Node creates a pre-allocated pool whose slices are then used to store the entries; multiple entries share one pool.
This improves performance by decreasing the amount of new allocations we need to make and decreases the context-switching overhead, but it can also be wasteful. A pool is kept alive until all of its slices had been freed. For years, the default size for this pre-allocated slab was 8KiB, which is not that much, and on top of that, Node wouldn't use this pre-allocated pool if the size of the allocation is larger than
Node version 24.18.0 featured an eight folds increase in this default pool size. Many common allocations, like the HTTP/2 frame size or modern stream chunk sizes surpass Node's 4KiB threshold and miss out on the performance boosts offered by the pooling system. The new 64KiB exhibited performance boost exactly on these 4-16KiB chunks.
To be honest, I didn't even know about this change until one morning I got a message that our Next.js app Pods' memory consumption triggered an alert, and had to be restarted. This followed an upgrade to Node > 24.18.0.
We handle a lot of traffic and have high-cardinality cache keys – there's a lot of stuff we cache to make the users' experience as smooth as possible. But hold on... Next.js promises to cap the cache's memory usage to 50MiB and then trigger LRU eviction mechanism! This is is what we relied on, and made us confident that we can afford to cache so many things. It was clear that the Next.js's cache eviction is failing.
Turns out Next.js considers only the raw size of the cache entry + its key when deciding whether the total cache size exceeds its eviction threshold. In the worst case scenario, each cache entry lives alone in an abandoned 64KiB pool, where all other entries had already been freed. Because of how Node works, this solitary entry keeps the whole pool alive. Next.js doesn't see this and its eviction mechanism doesn't kick in until the raw memory usage of the cached entries surpasses the 50MiB.
If you consider smaller cache entries where each 1KiB on average, this is a disaster. It would take over 50,000 entries to reach the 50MiB memory cap, and if each resides in the 64KiB pool, that's already over 3,1GiB of memory used before Next.js even wakes up. The smaller the entries, the worse it gets.
We solved this on few different fronts: a quick fix was to decrease the Buffer.poolSize back to 8KiB in our codebase. We didn't want to downgrade Node.js, as the primary reason for upgrading were security patches; this way we kept what we needed and reverted what harmed us.
It's important to realize that the described issue occurs even on Node.js < 24.18.0. It's just that the 8KiB buffer pool size doesn't create as much wasted memory and is harder to notice; for illustration, the case with 1KiB cache entries described earlier would only take up 390MiB on the 8KiB pool size before the eviction starts. Still almost eight times more than Next.js promises, but not catastrophic.
The only long-term solution is for the Next.js team to implement a check considering the underlying ArrayBuffer of every cached entry. Although there are PRs opened for that, the issue was not yet addressed as of writing this article.
To mitigate the issue even further, we significantly decreased the amount of high-cardinality dynamic items we cache. To offset potential performance hit, we delegated some of this caching to the CDN. This has the added benefit that this cache is shared across all of our Pods, whereas for the in-app memory, each Pod has its own. The memory usage can now rise above the 50MiB mark, but it can't keep rising unchecked and OOM our Pods anymore.
To conclude, it was fun to solve this issue, and a nice remind of the deal with the devil we make by using JavaScript.