Skip to content

perf(core): cache site:* settings prefix scan across requests - #839

Merged
ascorbic merged 5 commits into
mainfrom
perf/site-settings-cache
Apr 29, 2026
Merged

ascorbic merged 5 commits into
mainfrom
perf/site-settings-cache

Conversation

@ascorbic

Copy link
Copy Markdown
Member

What does this PR do?

Adds a globalThis-keyed isolate-wide cache for the site:* settings prefix scan. The catalogue showed SELECT name, value FROM options WHERE name LIKE 'site:%' firing once per public read on every route — the cache reduces that to once per isolate lifetime. Cross-isolate staleness is bounded by isolate lifetime (minutes on workerd), which is acceptable for chrome (site title, tagline, logo, SEO defaults).

Implementation in packages/core/src/settings/index.ts:

  • Holder at Symbol.for("emdash:site-settings") on globalThis, storing { cached: Promise | null, version: number }.
  • getSiteSettings() checks the holder; on hit, returns the cached promise (no DB).
  • On miss, fetches via getSiteSettingsWithDb(), populates both the request-cache promise wrapper and the globalThis holder.
  • setSiteSettings() calls invalidateSiteSettingsCache() which bumps the version and clears holder.cached.
  • The catch handler only clears the holder if holder.cached still references the failed promise — robust against in-flight overwrite.

The invalidation is wrapped in try/finally so a partial-write failure inside OptionsRepository.setMany (which iterates row-by-row, non-transactionally) still drops the cache. Without this, a mid-loop throw would leave the cache pointing at pre-write data while the DB has post-write data on some rows.

Snapshot diff (sqlite, fixtures/perf-site)

Every public read drops 1 query in both cold and warm phases:

Route Before After
GET / 14/14 13/13
GET /posts 14/14 13/13
GET /posts/building-for-the-long-term 33/33 32/32
GET /pages/about 13/13 12/12
GET /category/development 18/17 17/16
GET /tag/webdev 16/16 15/15
GET /rss.xml 6/6 5/5
GET /search 14/14 13/13

-16 queries per harness run. The d1 snapshot will be refreshed by the query-counts-apply workflow.

Tests added

Five tests in packages/core/tests/unit/settings/settings.test.ts using a real in-memory SQLite + Kysely log hook to count actual SQL — no mocks:

  • getSiteSetting() does not hit the DB after getSiteSettings() in the same request.
  • globalThis cache survives across requests within an isolate.
  • setSiteSettings() invalidates the globalThis cache.
  • setSiteSettings() invalidates the cache even when the write throws (covers the partial-write failure path).
  • invalidateSiteSettingsCache() drops the cached value (manual-invalidation path).

Write-path audit

Verified by exhaustive grep of packages/: only setSiteSettings() writes the site:* prefix. Other namespaces (emdash:*, plugin:<id>:*, site_* snake-case in WordPress import) don't invalidate this cache and shouldn't.

invalidateSiteSettingsCache is internal — not exported from packages/core/src/index.ts.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code

Site settings change rarely but were re-fetched on every public page
load. A globalThis-scoped holder caches the resolved settings until the
next site:* write bumps its version, dropping one prefix-scan query per
public route. Cross-isolate staleness is bounded by isolate lifetime.
OptionsRepository.setMany iterates row-by-row without a transaction. If
a write fails mid-loop, earlier rows are committed but
invalidateSiteSettingsCache() never ran — leaving the globalThis cache
pointing at pre-write data while the DB has post-write data on some
rows. Wrapping the call in try/finally guarantees invalidation runs
whether the write succeeded or threw.

Adversarial-review finding from PR #800-class perf branch.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 29, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-perf-coordinator 9f841d1 Apr 29 2026, 09:53 PM

@changeset-bot

changeset-bot Bot commented Apr 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9f841d1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions Bot added the query-count changed PR diff modifies query-count snapshot files label Apr 29, 2026
@github-actions

github-actions Bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Query-count snapshot changes

24 routes changed, total Δ -24 queries.

SQLite

Route Before After Δ
GET / (cold) 12 11 -1
GET / (warm) 12 11 -1
GET /category/development (cold) 16 15 -1
GET /category/development (warm) 15 14 -1
GET /pages/about (cold) 11 10 -1
GET /pages/about (warm) 11 10 -1
GET /posts (cold) 12 11 -1
GET /posts (warm) 12 11 -1
GET /posts/building-for-the-long-term (cold) 29 28 -1
GET /posts/building-for-the-long-term (warm) 29 28 -1
GET /rss.xml (cold) 5 4 -1
GET /rss.xml (warm) 5 4 -1
GET /search (cold) 13 12 -1
GET /search (warm) 13 12 -1
GET /tag/webdev (cold) 15 14 -1
GET /tag/webdev (warm) 15 14 -1

D1

Route Before After Δ
GET / (warm) 12 11 -1
GET /category/development (warm) 15 14 -1
GET /pages/about (warm) 11 10 -1
GET /posts (warm) 12 11 -1
GET /posts/building-for-the-long-term (warm) 28 27 -1
GET /rss.xml (warm) 5 4 -1
GET /search (warm) 13 12 -1
GET /tag/webdev (warm) 15 14 -1

Comparing snapshot files between base and head. Updated automatically on each push.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 29, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-i18n 9f841d1 Apr 29 2026, 09:53 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 29, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
docs 9f841d1 Apr 29 2026, 09:54 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 29, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-playground 9f841d1 Apr 29 2026, 09:55 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 29, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-cache 9f841d1 Apr 29 2026, 09:56 PM

@pkg-pr-new

pkg-pr-new Bot commented Apr 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@839

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@839

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@839

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@839

emdash

npm i https://pkg.pr.new/emdash@839

create-emdash

npm i https://pkg.pr.new/create-emdash@839

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@839

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@839

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@839

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@839

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@839

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@839

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@839

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@839

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@839

commit: 9f841d1

@ascorbic

Copy link
Copy Markdown
Member Author

/review

@ask-bonk

ask-bonk Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

@ascorbic
ascorbic enabled auto-merge (squash) April 29, 2026 21:58
@ascorbic
ascorbic merged commit 0d98c62 into main Apr 29, 2026
34 checks passed
@ascorbic
ascorbic deleted the perf/site-settings-cache branch April 29, 2026 22:04
@emdashbot emdashbot Bot mentioned this pull request Apr 29, 2026
ascorbic added a commit that referenced this pull request Apr 30, 2026
Adds the query-dump harness and analysis scripts that produced the
catalogue used to drive PRs #838, #839, #840:

- scripts/query-counts-dump.mjs — sibling of query-counts.mjs that
  emits per-route × phase NDJSON dumps under scripts/query-dumps/.
- scripts/build-perf-d1.mjs — standalone "build the perf fixture for
  d1" wrapper, useful when iterating with the dump harness without
  re-running the full counts harness.
- scripts/query-dumps/{classify,cold-only,inspect-other}.mjs — analysis
  helpers for slicing the dumps. classify.mjs writes a markdown
  classification report; cold-only.mjs surfaces the d1 cold-isolate tax;
  inspect-other.mjs prints distinct SQL for a class.
- scripts/query-dumps/README.md — workflow doc.

The dump JSON itself and the generated classification.{target}.md
reports stay gitignored — they're analysis artefacts that regenerate
from the harness in seconds. The query-dumps analysis scripts are
excluded from oxlint (one-off tooling, not production code).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core cla: signed query-count changed PR diff modifies query-count snapshot files size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant