Skip to content

worker: coalesce concurrent stale-hits so one background refresh fires per stale window - #449

Merged
max-sixty merged 1 commit into
mainfrom
cache-coalesce
May 13, 2026
Merged

max-sixty merged 1 commit into
mainfrom
cache-coalesce

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

PR #445 (stale-while-revalidate) called out an open trade-off: a burst of concurrent stale-hits could each fan out their own background refresh, so K viewers landing on a stale /activity entry triggered K Search fanouts — 4·N·K Search calls against GitHub's 30/min cap. The short fallback budget capped the fallout but the burst was wasteful.

This coalesces the burst. Before firing the real refresh in the stale-hit branch, the worker pushes the cached entry's x-tend-stale-at forward by REFRESH_GRACE_MS (30 s) and writes it back to the colo cache; concurrent stale-hits arriving within that window read the bumped entry as isStale → false and skip starting their own refresh. The real refresh's put overwrites the bumped entry with fresh data when it completes. One refresh per stale window per colo, instead of one per viewer.

Mechanism

coalesceAndRefresh (new) sequences two puts: a bump-put that copies the cached body and overrides only x-tend-stale-at, then a fresh-put with the refresh result (or fallback payload on throw — fallback budget semantics preserved, since the bump only extends staleness up to the moment the real put lands). refreshAndCache (cold path) is unchanged in behavior; the refresh-or-fallback logic is extracted into freshResponse and shared.

Trade-offs left in

Not perfectly one refresh per stale window. K viewers reading the stale entry within the colo's cache-propagation window (a few ms in practice) all still enter coalesceAndRefresh and fire a refresh — once a bump-put propagates colo-wide, subsequent viewers skip. So K drops from "all viewers in the stale window" to "viewers in the propagation window", a meaningful improvement without needing Durable Objects. Decision-time jitter (xfetch) would tighten this further; deferred until traffic actually surfaces it.

…s per stale window

PR #445 (stale-while-revalidate) called out an open trade-off: a burst of concurrent stale-hits could each fan out their own background refresh, so K viewers landing on a stale `/activity` entry triggered K Search fanouts — 4·N·K Search calls against GitHub's 30/min cap. The short fallback budget capped the fallout but the burst was wasteful.

This coalesces the burst. Before firing the real refresh in the stale-hit branch, the worker pushes the cached entry's `x-tend-stale-at` forward by `REFRESH_GRACE_MS` (30 s) and writes it back to the colo cache; concurrent stale-hits arriving within that window read the bumped entry as `isStale → false` and skip starting their own refresh. The real refresh's put overwrites the bumped entry with fresh data when it completes. One refresh per stale window per colo, instead of one per viewer.

### Mechanism

`coalesceAndRefresh` (new) sequences two puts: a bump-put that copies the cached body and overrides only `x-tend-stale-at`, then a fresh-put with the refresh result (or fallback payload on throw — fallback budget semantics preserved, since the bump only extends staleness up to the moment the real put lands). `refreshAndCache` (cold path) is unchanged in behavior; the refresh-or-fallback logic is extracted into `freshResponse` and shared.

### Trade-offs left in

Not perfectly one refresh per stale window. K viewers reading the stale entry within the colo's cache-propagation window (a few ms in practice) all still enter `coalesceAndRefresh` and fire a refresh — once a bump-put propagates colo-wide, subsequent viewers skip. So K drops from "all viewers in the stale window" to "viewers in the propagation window", a meaningful improvement without needing Durable Objects. Decision-time jitter (xfetch) would tighten this further; deferred until traffic actually surfaces it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit a75c518 into main May 13, 2026
6 checks passed
@max-sixty
max-sixty deleted the cache-coalesce branch May 13, 2026 17:17
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.

2 participants