Skip to content

fix(hydrogen): refresh a stale cache entry once per key while a refresh is in flight - #4389

Open
debojyoti10CC wants to merge 3 commits into
Shopify:mainfrom
debojyoti10CC:fix/run-with-cache-stale-refresh
Open

debojyoti10CC wants to merge 3 commits into
Shopify:mainfrom
debojyoti10CC:fix/run-with-cache-stale-refresh

Conversation

@debojyoti10CC

Copy link
Copy Markdown

Fixes #4383

TL;DR: createRunWithCache started a background refresh for every request that hit a stale entry, so while the first refresh was in flight, each extra request started another origin read. It now keeps one refresh per cache key at a time in each isolate, like 2025.7's swrLock did.

Before

Three concurrent stale hits for one key called the callback three times with { background: true }.

After

Only the first stale hit starts a refresh. The key is released when that refresh finishes or fails, so the next stale window can refresh again.

What this changes

  • createRunWithCache holds a Set of keys being refreshed. revalidateInBackground returns early if the key is in the set, and removes it once the refresh's complete promise settles.
  • Adds the regression test from the issue, plus one that checks a failed refresh releases the key.
  • Updates "serves stale data and schedules background revalidation for stale hits". It expected two refreshes for two concurrent hits; it now expects one.

Developer impact

Includes a patch changeset for @shopify/hydrogen. No API changes.

Risk

The lock is per createRunWithCache instance, so per isolate, not shared across isolates. That is the same scope as 2025.7.

Testing

  • Before the fix, the new tests failed (3 background calls instead of 1).
  • After: vitest run src/core/cache/run-with-cache.test.ts passes, 18 of 18. tsc --noEmit and oxlint src/core/cache are clean.
  • Could not run oxfmt locally (native binding missing on this machine), so formatting is unchecked.

…sh is in flight

runWithCache started a background refresh for every stale hit, so origin reads grew with traffic while the first refresh was still running. Keep an in-memory per-key lock in createRunWithCache, released when the refresh completes or fails, as 2025.7 did with swrLock.
@debojyoti10CC
debojyoti10CC requested a review from a team as a code owner November 17, 2026 19:33
@debojyoti10CC

Copy link
Copy Markdown
Author

I have signed the CLA!

@Peerapatfc

Copy link
Copy Markdown

Thanks for picking this up! One concern: revalidating is created inside each createRunWithCache call, but the Storefront client builds its own runner, and the React Router template creates a client per request (in root.tsx middleware). So concurrent stale hits from different requests would each still start a refresh; this only dedupes within a single request.

2025.7's swrLock was module-level for this reason. Would a module-level lock that holds only keys and start times (released when the refresh settles, with a ~30s expiry in case a waitUntil context is cancelled) work here? Happy to share a test for the per-request case.

…e cache

The lock was created per createRunWithCache call. Clients built per request (for example, a Storefront client per request) each got their own lock, so concurrent stale hits still started one background refresh per client. The lock is now kept per cache object at module scope, so runners that share a cache in an isolate share one lock.
@debojyoti10CC

Copy link
Copy Markdown
Author

Thanks for the catch. You're right: a lock created per createRunWithCache call doesn't cover clients built per request. I moved it to module scope, keyed by the cache object, so runners that share the same cache in an isolate share one lock. Pushed in 4a5e5ba, with a test that two runners on one cache start only one background refresh.

One caveat: if each request wraps a new cache object around the same underlying cache, those runners won't share the lock. Could you check whether your setup reuses the cache object? If not, I can change this.

@Peerapatfc

Copy link
Copy Markdown

Thanks for the quick update! I checked: caches.open(name) returns a new Cache object on every call, even for the same name within one isolate (I ran it on miniflare/workerd 1.20260730.1, and workerd's CacheStorage::open allocates a fresh Cache each time; I haven't tested on Oxygen production itself). The React Router template calls caches.open("hydrogen-v1") inside the fetch handler, so every request gets a new object and a WeakMap keyed by it won't share the lock. A test with a new cache wrapper per request over the same store starts 3 background refreshes on 4a5e5ba.

Keying by the cache key alone (module-level, key-only, released when the refresh settles) works regardless of the cache object. I'd also keep a short expiry (e.g. 30s) so a cancelled waitUntil can't lock a key forever. Happy to share the test.

caches.open returns a new Cache object on each call, so a lock keyed by the cache object is not shared by runners created per request. The lock is now keyed by cache key at module scope, and expires after 30 seconds so a refresh whose waitUntil was cancelled cannot block its key forever.
@debojyoti10CC

Copy link
Copy Markdown
Author

You're right, and thanks for checking caches.open. I switched to the approach you suggested in 485ee1fc: the lock is keyed by cache key at module scope, and a lock expires after 30 seconds so a cancelled waitUntil can't block a key forever.

I added a test where each request builds a new cache object over the same store. It starts one background refresh, not three. A second test checks that a new refresh can start once a stuck lock expires.

Known trade-off: two different caches in the same isolate that use the same key will share the lock. The effect is that one refresh gets skipped until the next stale window, not a wrong result. Happy to narrow that if you have a cache identity to key on.

I haven't run it on Workers or Oxygen, only in vitest (63 cache tests pass). Your workerd run would be the real check.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

runWithCache starts a background refresh for every stale hit, where 2025.7 started one per key

2 participants