Repository navigation
Fix key accounting in byte-budgeted LRUs - #96187
marcoshernanz wants to merge 3 commits into
Conversation
Stats from current PR✅ No significant changes detected📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
⚡ Dev Server
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
⚡ Production Builds
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes⚡ TurbopackClient Main Bundles
Server Middleware
Build DetailsBuild Manifests
📦 WebpackClient Main Bundles
Polyfills
Pages
Server Edge SSR
Middleware
Build DetailsBuild Manifests
Build Cache
🔄 Shared (bundler-independent)Runtimes
📝 Changed Files (29 files)Files with changes:
View diffsapp-page-exp..ntime.dev.jsDiff too large to display app-page-exp..time.prod.jsDiff too large to display app-page-tur..ntime.dev.jsDiff too large to display app-page-tur..time.prod.jsDiff too large to display app-page-tur..ntime.dev.jsDiff too large to display app-page-tur..time.prod.jsDiff too large to display app-page.runtime.dev.jsDiff too large to display app-page.runtime.prod.jsDiff too large to display app-route-ex..ntime.dev.jsDiff too large to display app-route-ex..time.prod.jsDiff too large to display app-route-tu..ntime.dev.jsDiff too large to display app-route-tu..time.prod.jsDiff too large to display app-route-tu..ntime.dev.jsDiff too large to display app-route-tu..time.prod.jsDiff too large to display app-route.runtime.dev.jsDiff too large to display app-route.ru..time.prod.jsDiff too large to display pages-api-tu..ntime.dev.jsDiff too large to display pages-api-tu..time.prod.jsDiff too large to display pages-api.runtime.dev.jsDiff too large to display pages-api.ru..time.prod.jsDiff too large to display pages-turbo...ntime.dev.jsDiff too large to display pages-turbo...time.prod.jsDiff too large to display pages.runtime.dev.jsDiff too large to display pages.runtime.prod.jsDiff too large to display server.runtime.prod.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display use-cache-pr..ntime.dev.jsDiff too large to display 📎 Tarball URLCommit: 0b6e43c |
Tests PassedCommit: 0b6e43c |
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?
Include retained string keys in the size of byte-budgeted in-memory LRUs.
LRUCachenow passes the key to custom size calculators, and each in-memorybyte-budgeted caller includes that key in its estimate. Count-based caches and
the disk-byte LRU are intentionally unchanged.
#94699 covers the original filesystem call site. This draft also applies the
same API change to the other byte-budgeted in-memory callers found by the
all-caller audit.
Why?
Large unique keys were effectively free. In the filesystem negative cache,
one million long misses could fit under a nominal 1 MiB budget because every
entry counted as one byte while retaining the full path.
Measurement
Exact merge-base/head comparison on a 16-vCPU, 32-GiB DevBox with Node
24.14.1. After one warmup per revision, four runs each were interleaved, with
four forced GCs per sample:
Final heap and entry-count A/A ranges were zero for both revisions; final RSS
ranges were 0.55 MiB before and 1.03 MiB after. This is a 97.57% heap
reduction and a 99.50% retained-entry reduction.
Validation
pnpm exec turbo run build --filter=next...pnpm --filter=next typesgit diff --checkThe independent fixes for #94919 and #95094 are already covered by the smaller
#96173 and #96161, respectively, so they are intentionally excluded here.
Fixes #94890