Repository navigation
perf(worker): cache the public summary per colo and read D1 one query fewer - #50
Merged
Merged
Conversation
…query summarizeUsage made three sequential round trips to D1, which lives in WEUR: the user, the timezone of its most recently seen machine, then the GROUP BY. The first two only depend on the login, so a LEFT JOIN answers both and keeps the same last_seen and machine_id tie-break and the UTC default.
…utes The summary route answered with `Cache-Control: public, s-maxage=300`, but a Worker on a Custom Domain runs in front of the edge cache, so every request read D1: about 4,500 summary queries a day on 2026-10-08, ~6,000 requests with 54% aborted by a caller timing out at 500 ms. cachedSummary keeps a summary in the Cache API per colo for the five minutes the route already advertises, keyed by the lowercased login and the range, and never caches a login without an account. The landing page can call it too instead of summarizeUsage.
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.
What
The summary route advertised
Cache-Control: public, s-maxage=300, but a Worker on a Custom Domain runs in front of the edge cache, so nothing was ever cached. Measured on 2026-10-08:SELECT ... GROUP BYexecutions a day inwrangler d1 insights.Change
summarizeUsagereads the user and the timezone of its most recently seen machine in oneLEFT JOIN, keeping the samelast_seen, machine_idtie-break and theUTCdefault. A summary now costs two round trips instead of three.cachedSummaryinsrc/server/summary-cache.tskeeps a summary in the Cache API per colo for the five minutes the route already advertises. It keys by the lowercased login and the range, and never caches a login without an account. The route uses it, and the landing page can call it instead ofsummarizeUsage.Verification
summary-cache.spec.tscovers a repeated summary served without touching D1, one key per lowercased login and range atmax-age=300, and no entry for an unknown login. Ausage.spec.tscase asserts twopreparecalls per summary.caches.openwith an in-memory cache throughsrc/test/memory-cache.ts.format,lint:slop,check-types,test,test:dependency-policy,test:package,audit:production,buildandwrangler deploy --dry-runall pass locally.