The relay Worker sets no placement, so it runs at each user's Cloudflare edge while its Postgres is PlanetScale us-west (AWS us-west-2, Oregon). Every query pays the edge-to-Oregon round trip. Over the last hour in prod: 1.78M queries, p5 14 ms (west-coast edges, roughly the real query cost), p50 103 ms, p90 197 ms, p99 351 ms.
Requests run their queries in sequence, so it compounds:
| Request | Queries (p50) | DB time p50 / p90 |
|---|
connectEnvironment | 3 | 0.47 s / 0.69 s |
publishAgentActivity | 6 | 0.58 s / 1.18 s |
linkEnvironment | 6+ | 0.86 s / 1.25 s |
recoverManagedEndpoint | 6 | 0.98 s / 1.40 s |
This is also why links from far edges (JNB, ACC, DAR) hit the 9 s deadline in #16810 even without Cloudflare rate limiting: the Oct 7 trace 4684d7d1… spent ~15 sequential queries at ~310 ms each.
Fix#
Set placement: { region: "aws:us-west-2" } on the Worker. Cloudflare runs fetch handling in the data center closest to that region, so a request pays the user-to-Oregon distance once instead of once per query.
Notes:
- Placement only affects fetch handlers. Queue consumers (APNs/FCM) and the cron reaper stay unplaced, so they still pay the round trip on every query. That's no worse than today, but a follow-up could move them.
- Calls that go on to the user's tunnel after their queries (
status, connect health and credential mint, hook forwarding) now leave from Oregon instead of the user's edge. When the user and host are both far from Oregon and close to each other, status (2 queries) roughly breaks even; connect, link and recovery still come out ahead. The relay never carries the client↔host byte stream, so the data path is unaffected.
- Routes that never touch Postgres (
/health, OpenAPI) get one hop slower.
- Every stage uses branches of the same
us-west database (PlanetScale us-west = AWS us-west-2, per PlanetScale's region docs), so one region fits all stages. This also matches Cloudflare's Hyperdrive guidance for Workers that run more than one query per request.
- To verify after deploy: the per-query
postgres span p50 in t3-code-relay-traces-prod should drop to roughly 5–15 ms.
Tested with the relay typecheck. Placement only takes effect on deploy.
Done with Claude Code (Opus 5.5) in T3 Code.
🤖 Generated with Claude Code
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
0ee07481-dee0-4553-a8ce-6d23069cda7b📥 Commits
Reviewing files that changed from the base of the PR and between fb990765a49414a5bfbdc73365455fc8b5fbe691 and b8463105f81a5567e0aa227a50fd9ce1e33b74b8.
📒 Files selected for processing: 1
infra/relay/src/worker.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.
📝 Walkthrough
The Cloudflare Worker configuration now sets
placement.regiontoaws:us-west-2.Changes: Relay Worker Configuration
infra/relay/src/worker.tsplacement.regiontoaws:us-west-2.Priority: ➖ Normal
Estimated code review effort: 1 (Trivial) | ~5 minutes
Change: Bug fix
Suggested reviewers:
maria-rcksMerge Risk: ⚪ Minimal · up to
b8463The placement change is mergeable with normal deployment verification.
Pre-merge checks:
3 · 
1
## Scope and approvalsection with the triaged issue or explicit maintainer approval, or explain why this focused configuration change qualifies for an exemption. Add a `## Ve…Full details: Description check
Explanation
The description clearly explains the latency problem, proposed Worker placement change, trade-offs, expected verification, and tool usage. It does not follow the required template: it omits the
## Problem,## Change,## Scope and approval, and## Verificationheadings, and it does not provide the required approval or focused verification details.Resolution
Restructure the description using all required headings. Add a
## Scope and approvalsection with the triaged issue or explicit maintainer approval, or explain why this focused configuration change qualifies for an exemption. Add a## Verificationsection with the exact typecheck command and observed result, deployment verification steps, and any checks that were not performed. Keep the existing problem, fix, trade-off, and agent details under the appropriate sections.✨ Finishing Touches
📝 Generate docstrings
🧪 Generate unit tests (beta)
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment `@coderabbitai help` to get the list of available commands.