perf(web): use read replica for gateway balance checks - #4972
Merged
Conversation
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryThe change routes only the read-only Notes (no action required)
Files Reviewed (2 files)
Reviewed by claude-opus-5 · Input: 42 · Output: 9.1K · Cached: 1.1M Review guidance: REVIEW.md from base branch |
RSO
approved these changes
Aug 3, 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.
Summary
readDbinto the OpenRouter gateway balance and organization settings lookupreadDbConsequences
This moves a high-volume, read-only gateway query off the primary PostgreSQL instance. In US regions it avoids the cross-region read to the Frankfurt primary and should reduce balance-check latency; in EU regions it distributes reads across replicas, reducing primary connection and query load. If no replica is configured,
readDbfalls back to the primary, so behavior is unchanged.The tradeoff is eventual consistency. Replica lag is normally slight (typically under 100 ms), but immediately after a write the gateway can briefly observe stale organization balances, per-user usage or limits, membership, settings, plan, seat requirements, auto-top-up state, or credit-expiration metadata. Depending on which value changed, a request may be briefly accepted or rejected using the prior state, or use the prior organization routing/data-collection policy. No writes are redirected: expiry processing, auto-top-up, usage accounting, and all other mutations continue to use the primary database.
Verification
pnpm --filter web exec jest --runInBand --runTestsByPath 'src/app/api/openrouter/[...path]/route.test.ts'(21 tests)scripts/typecheck-all.sh --changes-onlypnpm -w exec oxlint --config .oxlintrc.json 'apps/web/src/app/api/openrouter/[...path]/route.ts' 'apps/web/src/app/api/openrouter/[...path]/route.test.ts'pnpm -w exec oxfmt --write 'apps/web/src/app/api/openrouter/[...path]/route.ts' 'apps/web/src/app/api/openrouter/[...path]/route.test.ts'git diff --check