Skip to content

Make fewer database round trips per control-plane request - #294

Merged
tnkuehne merged 3 commits into
mainfrom
fewer-request-queries
Nov 22, 2026
Merged

tnkuehne merged 3 commits into
mainfrom
fewer-request-queries

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

On a hosted installation, a control-plane query such as getApps made 11 database queries in a row. Traces showed several that only repeated or wrapped other work:

  • the session and the user, each read twice
  • begin and commit around a single read

Changes

  • Account admission: with an allowUser check, the before hook on /get-session read the session and user and the user's admission state, and the endpoint then read the session and user again. Session lookups no longer run the check. An installation that blocks a user ends their sessions; the hosted ban does this through Better Auth's admin plugin. New sessions, /token and the account paths still check admission.

  • Reads without a transaction: readOnly runs a read with the same error mapping as transact, but without begin and commit. readableApp reads an app with the same visibility rules as managedApp, without FOR UPDATE. They replace the transaction and the row lock in the reads that page navigation makes:

    • the app list, details, history and access
    • the catalog
    • app access and secrets
    • connections, bindings and API descriptions
    • workflow definitions and runs
    • agents
    • connection requests

    Writes still use transact and lock the app row.

  • Schema validation on Cloudflare: Better Auth validates the schema the first time each auth instance is used. The Cloudflare runtime builds an instance per request, so the check ran on every request. It is turned off there. Node builds one instance per process and keeps the check, so tests still run it.

Queries per navigation

Measured locally against main. The local server has no allowUser check, so the three admission reads that hosted installations also save per request do not appear here.

Navigation main This branch
My apps (getApps) 6 4
App overview (getAppDetails) 15 9
Home (getCatalog) 6 4
Members (getMembers) 5 5

Validation

  • pnpm check
  • Control-plane Vitest suite: 956 passed, 656 skipped. Locally, tests that need missing binaries or services are skipped. This includes authentication-extension.integration.test.ts, whose unavailable account still gets 403 from /get-session.
  • Browser tests: 74 passed, 1 skipped. The CLI login suite did not run locally because the CLI binary was not built.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge with the stated requirement that extensions revoke blocked users’ sessions.

Summary

Reduces database round trips for control-plane reads.

  • Control-plane pages read app data with fewer database trips.
  • Blocked users' session lookups no longer repeat the admission check.
  • Cloudflare auth instances skip schema checks on each request.

No new actionable issues were found in the changes since the previous review.

Reviews (3) · Last reviewed commit: "Stop rechecking account admission on eve..." · Reviewed by Greptile

Every request reads the session. With an account admission check, a hook
read the session and user before the session endpoint read them again.
The check now runs on the session the endpoint returns.

Reads on page navigation ran in a transaction, and the app lookup locked
its row, so each read paid for begin and commit. They now run without a
transaction and read the app without a lock. Writes keep both.

On Cloudflare each request builds its own auth instance, so Better Auth
validated the schema on every request. Node still validates it once.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 22, 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
widefleet-docs fe83a35 Nov 22 2026, 02:21 PM

Passing validateSchema: true explicitly turns Better Auth's note about
adapters it cannot validate into a warning, which the social sign-in
tests rejected. Only the Cloudflare runtime now sets the option.
Admission ran before each session lookup and read the user's admission
state on every request. An installation that blocks a user ends their
sessions, and new sessions, tokens and account changes are still checked,
so session lookups no longer repeat the check.
@tnkuehne
tnkuehne merged commit 809d0a1 into main Nov 22, 2026
15 of 16 checks passed
@tnkuehne
tnkuehne deleted the fewer-request-queries branch November 22, 2026 14:31
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