Skip to content

perf(api): fewer D1 round trips per admin request - #68

Merged
Madraka merged 1 commit into
mainfrom
perf/admin-api-round-trips
Oct 21, 2026
Merged

Madraka merged 1 commit into
mainfrom
perf/admin-api-round-trips

Conversation

@Madraka

@Madraka Madraka commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Fix 4 of the internal performance review (admin API).

Measured on studio.cloudhal.com: 180–380 ms per admin call alone, 700–1200 ms when a screen fires its 10–13 calls together. Each signed-in call made about 5 serial D1 round trips (session, membership ×2, getSite, data), and D1 serves one query at a time per database.

  • Session cookie cache (auth/instance.ts): session.cookieCache: { enabled: true, maxAge: 60 }. The session comes from a signed cookie, not D1. Trade-off accepted by the maintainer and recorded as an ADR-0010 addendum: a session revoked from another device stays usable for up to 60 s. The person's own sign-out clears the cookie at once, Better Auth's sensitive endpoints bypass the cache, and membership and roles are still read every request, so removals and role changes apply at once.
  • One membership query (staff.ts member): LEFT JOIN site_members; the now unused siteRolesOf is removed.
  • Rate limiter: customRules: { "/get-session": false }. The limiter stores in D1, so it added a read and a write to every session check; every other auth endpoint stays limited.
  • Comments sweep: due comment purges are flushed at most every 10 s per isolate (commentsSweepDue) instead of on every admin request; moderation still wakes its own targeted flush.

Tests: @cloudhal/api 371/371, @cloudhal/cloudflare (workerd) 53/53.

🤖 Generated with Claude Code

A signed-in admin API call made about five serial D1 round trips before
and inside its handler, and Studio fires 10–13 calls per screen into a
database that serves one query at a time.

- Better Auth `session.cookieCache` (maxAge 60 s): the session is read
  from a signed cookie instead of D1. A session revoked elsewhere stays
  usable for up to 60 s; membership and roles still apply at once
  (ADR-0010 addendum).
- `staffStore.member`: the member and their site roles in one query.
- The auth rate limiter (stored in D1) no longer runs on `/get-session`.
- Due comment purges are swept at most every 10 s per isolate instead of
  on every admin request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Madraka <[email protected]>
@Madraka
Madraka merged commit e58055e into main Oct 21, 2026
4 checks passed
@Madraka
Madraka deleted the perf/admin-api-round-trips branch October 21, 2026 22:28
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.

1 participant