perf: defer regional cache writes off the response path - #1345
Open
km-tr wants to merge 2 commits into
Open
Conversation
Next.js awaits incrementalCache.set while producing the response, so the store write and the regional cache update were both on the critical path. The return value is unused, so they can run in ctx.waitUntil - the read path already defers its cache updates the same way.
🦋 Changeset detectedLatest commit: 56a49dc The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
AGENTS.md and CONTRIBUTING.md list feature | fix | refactor | docs | chore.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1343
What
RegionalCache.setnow hands both writes toctx.waitUntilinstead of awaiting them.Why
Next.js awaits
incrementalCache.setwhile producing the response:So the store round-trip lands in the TTFB.
response-cache/index.jsdoes not use thereturned value — it returns the same entry it passed in — so the writes can be deferred
without changing what is served.
The read path already defers its cache updates the same way:
This makes
setconsistent with it —putToCachewas previously deferred when reachedfrom
getand awaited when reached fromset.Measured impact
Production app on Workers with the R2 incremental cache, using Workers automatic tracing.
r2_putspans: median 976 ms, up to 2,048 ms (14 of 20 sampled traces)We ran this as a local override on a staging deployment, four requests each on ISR misses:
Repeated requests to the same page still returned
x-opennext-cache: HITat 0.32–0.69 s,so cache behaviour is unchanged.
Trade-off
Deferring the write widens the window in which a concurrent request for the same page
misses the cache and re-renders. That window already exists for the duration of the write;
deferring shifts its start rather than creating it.
Notes
surface as an unhandled rejection.
regional-cache.spec.tscovering the deferred path:setresolves while the storewrite is still pending, both writes run in the background, and a rejected store write does
not reject the promise handed to
waitUntil. The tests fail against the awaiting version.