Repository navigation
fix(hydrogen): refresh a stale cache entry once per key while a refresh is in flight - #4389
debojyoti10CC wants to merge 3 commits into
Conversation
…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.
|
I have signed the CLA! |
|
Thanks for picking this up! One concern: 2025.7's |
…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.
|
Thanks for the catch. You're right: a lock created per 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. |
|
Thanks for the quick update! I checked: 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 |
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.
|
You're right, and thanks for checking 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. |
Fixes #4383
TL;DR:
createRunWithCachestarted 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'sswrLockdid.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
createRunWithCacheholds aSetof keys being refreshed.revalidateInBackgroundreturns early if the key is in the set, and removes it once the refresh'scompletepromise settles.Developer impact
Includes a patch changeset for
@shopify/hydrogen. No API changes.Risk
The lock is per
createRunWithCacheinstance, so per isolate, not shared across isolates. That is the same scope as 2025.7.Testing
vitest run src/core/cache/run-with-cache.test.tspasses, 18 of 18.tsc --noEmitandoxlint src/core/cacheare clean.oxfmtlocally (native binding missing on this machine), so formatting is unchecked.