Repository navigation
Conversation
The calculateSize callback in LRUCache only received the value, omitting the key (URL string). In the filesystem router, this meant that the URL key – which can be long in path-heavy apps – was never counted toward the cache's memory budget, causing under-eviction and excessive heap retention on long-lived servers. Changes: - Extend LRUCache.calculateSize signature to pass key as the second argument (backward-compatible: existing callbacks that ignore it continue to work unchanged). - Update getItemsLru in filesystem.ts to include key.length in the returned size for both positive and negative (null) cache entries. Fixes vercel#94890
Stats cancelledCommit: 661384e |
Failing test suitesCommit: 661384e | About building and testing Next.js
Expand output● deterministic build - changing deployment id › build output API - standard adapter › should produce identical build outputs even when changing deployment id ... truncated ... |
| ? new LRUCache<FsOutput | null>(1024 * 1024, function length(value, key) { | ||
| if (!value) { | ||
| // Null entries (negative cache) still need a non-zero size for LRU eviction | ||
| return 1 | ||
| // Null entries (negative cache) still need a non-zero size for LRU eviction. | ||
| // Include the URL key length to accurately account for heap usage. | ||
| return 1 + key.length | ||
| } | ||
| return ( | ||
| key.length + |
There was a problem hiding this comment.
I'm a bit concerned here that we now cache less even though the existing heap usage was apparently fine. We should investigate how much key space we currently use and adjust the size accordingly.
There was a problem hiding this comment.
Good point — including the key length will cause the LRU to evict more aggressively, so the cache will hold fewer entries for the same size budget.
For the fix to be correct we'd want to either (a) investigate how much key space is actually consumed in production and adjust the maxSize upward to compensate, or (b) include the key length and update the tests to reflect the new effective capacity.
The issue reporter (#94890) described heap growth under a large unique-URL workload, which suggested the budget was being underestimated, but you're right that we should verify that with data rather than assuming.
Do you have a suggested approach for measuring key-space usage, or would a maxSize adjustment alongside the key-length accounting work for you?
There was a problem hiding this comment.
Looking at actual production data. I'm not entirely sure this isn't purely a theoretical issue at this point.
There was a problem hiding this comment.
That's really valuable — if production key space shows URL lengths are small relative to the value sizes, the accounting gap may indeed be negligible.
Happy to wait on your findings. If the data suggests this is purely theoretical, I'd rather close the PR than merge a change that makes the cache more aggressive without a measurable benefit. Alternatively, if there's a threshold or target max URL length you'd like us to guard against, I can adjust the implementation accordingly.
There was a problem hiding this comment.
I won't have time to dig into this. As far as we can tell, this is not an issue. If you have observed otherwise in real world data, you should share that data.
|
Closing per @eps1lon's note — production data doesn't show this as a real issue. Thanks for checking! |
Fixes vercel#94890 Adopted from vercel#96187 and vercel#94699, with both authors credited on the commit. The router server keeps an in-memory cache that maps request URL paths to what they resolve to: a page, a static file, or nothing. It's bounded by a size budget, but the size of each entry was computed from the cached value only. The URL string used as the key was free, and "resolves to nothing" entries counted as size 1. So a long-lived server receiving many unique URLs — bot crawls, long slugs, `/order/:id`-style traffic — could accumulate up to ~1M URL strings while the cache believed it was within budget. That looks like a slow memory leak that tracks how many distinct URLs you've served rather than how much traffic you get. vercel#94890 has several production reports, including two deployments that patched key accounting in and saw memory go flat. The idea of the fix is to make the cache's bookkeeping match what an entry actually keeps alive, so the budget becomes a real memory bound. That takes three things: 1. Count the key string, not just the value. 2. Count the fixed cost an entry has regardless of string lengths — the per-entry bookkeeping objects, measured at ~120 bytes. Without this, a flood of *short* unique URLs stays "cheap" on paper while the bookkeeping itself grows unbounded. 3. Copy strings before caching them. When V8 takes a substring of a long string (which is how the pathname is extracted from the request URL), it doesn't copy the characters — it creates a small object that points into the original string, and the original can't be garbage-collected while the substring is alive. So caching a 30-char pathname could actually hold the entire URL in memory, kilobyte query string and all, and the bookkeeping only sees the 30 chars. Copying the string before caching makes it hold only its own characters. With the bookkeeping honest, the budget is set to 8 MiB, roughly the real memory the cache could already use for legitimate entries before this change, so capacity isn't reduced — the previous attempt at this fix (vercel#94942) stalled on that concern. ## Test Plan A/B on a production `next start` serving 60k requests with unique long URLs (the reported traffic shape): canary grows +79 MB and keeps growing; this branch grows +9 MB and plateaus — the budget working as intended. The per-entry costs above were measured directly on the cache with forced GC (they fit `118 + key length` bytes within a few percent, which is also what predicts the A/B numbers). `lru-cache` unit tests (26/26) and `pnpm --filter=next types` pass. --------- Co-authored-by: Marcos Hernanz <96699542+marcoshernanz@users.noreply.github.com> Co-authored-by: dmchoi77 <dm.choi.dev@gmail.com>
Fixes vercel#94890 Adopted from vercel#96187 and vercel#94699, with both authors credited on the commit. The router server keeps an in-memory cache that maps request URL paths to what they resolve to: a page, a static file, or nothing. It's bounded by a size budget, but the size of each entry was computed from the cached value only. The URL string used as the key was free, and "resolves to nothing" entries counted as size 1. So a long-lived server receiving many unique URLs — bot crawls, long slugs, `/order/:id`-style traffic — could accumulate up to ~1M URL strings while the cache believed it was within budget. That looks like a slow memory leak that tracks how many distinct URLs you've served rather than how much traffic you get. vercel#94890 has several production reports, including two deployments that patched key accounting in and saw memory go flat. The idea of the fix is to make the cache's bookkeeping match what an entry actually keeps alive, so the budget becomes a real memory bound. That takes three things: 1. Count the key string, not just the value. 2. Count the fixed cost an entry has regardless of string lengths — the per-entry bookkeeping objects, measured at ~120 bytes. Without this, a flood of *short* unique URLs stays "cheap" on paper while the bookkeeping itself grows unbounded. 3. Copy strings before caching them. When V8 takes a substring of a long string (which is how the pathname is extracted from the request URL), it doesn't copy the characters — it creates a small object that points into the original string, and the original can't be garbage-collected while the substring is alive. So caching a 30-char pathname could actually hold the entire URL in memory, kilobyte query string and all, and the bookkeeping only sees the 30 chars. Copying the string before caching makes it hold only its own characters. With the bookkeeping honest, the budget is set to 8 MiB, roughly the real memory the cache could already use for legitimate entries before this change, so capacity isn't reduced — the previous attempt at this fix (vercel#94942) stalled on that concern. ## Test Plan A/B on a production `next start` serving 60k requests with unique long URLs (the reported traffic shape): canary grows +79 MB and keeps growing; this branch grows +9 MB and plateaus — the budget working as intended. The per-entry costs above were measured directly on the cache with forced GC (they fit `118 + key length` bytes within a few percent, which is also what predicts the A/B numbers). `lru-cache` unit tests (26/26) and `pnpm --filter=next types` pass. --------- Co-authored-by: Marcos Hernanz <96699542+marcoshernanz@users.noreply.github.com> Co-authored-by: dmchoi77 <dm.choi.dev@gmail.com>
What?
The
calculateSizecallback inLRUCacheonly received the cached value, omitting the key (URL string). In the filesystem router, the URL key – which can be long in path-heavy apps – was never counted toward the cache's memory budget.Why?
This causes under-eviction: the LRU cache believes entries are smaller than they really are, so it evicts too infrequently, leading to excessive heap retention in long-lived servers handling many unique URLs.
How?
lru-cache.ts— Extend thecalculateSizecallback signature to passkeyas a second argument. This is fully backward-compatible: all existing callers that only usevaluecontinue to work unchanged (TypeScript allows functions with fewer parameters to satisfy a type requiring more).filesystem.ts— Update thegetItemsLrusize function to includekey.lengthfor both positive cache entries and negative (null) entries, so the full URL string is counted toward the cache budget.Fixes #94890