Repository navigation
worker: coalesce concurrent stale-hits so one background refresh fires per stale window - #449
Merged
Merged
Conversation
…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]>
tend-agent
approved these changes
May 13, 2026
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.
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
/activityentry 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-atforward byREFRESH_GRACE_MS(30 s) and writes it back to the colo cache; concurrent stale-hits arriving within that window read the bumped entry asisStale → falseand 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 onlyx-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 intofreshResponseand 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
coalesceAndRefreshand 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.