Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(relay): run the relay Worker next to its database #22065

Mergedjuliusmarmingemerged 1 commit intomain fromrefs/pull/22065/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

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:

RequestQueries (p50)DB time p50 / p90
connectEnvironment30.47 s / 0.69 s
publishAgentActivity60.58 s / 1.18 s
linkEnvironment6+0.86 s / 1.25 s
recoverManagedEndpoint60.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

    1. juliusmarminge added labelsmacroscope-review

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Path: .coderabbit.config.ts
      • Review profile: CHILL
      • Plan: Team
      • Run ID: 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.ts

      Included 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.region to aws:us-west-2.

      Changes: Relay Worker Configuration

      Layer / File(s)Summary
      Set Worker placement region
      infra/relay/src/worker.ts
      The Worker configuration sets placement.region to aws:us-west-2.

      Priority: ➖ Normal

      Estimated code review effort: 1 (Trivial) | ~5 minutes

      Change: Bug fix

      Suggested reviewers: maria-rcks

      Merge Risk: ⚪ Minimal · up to b8463

      The placement change is mergeable with normal deployment verification.

      Pre-merge checks: Passed 3 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Description checkWarningThe 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 `## Pr…Restructure the description using all required headings. Add a ## Scope and approval section with the triaged issue or explicit maintainer approval, or explain why this focused configuration change qualifies for an exemption. Add a `## Ve…
      Passed Passed checks: 3 passed
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the main change: placing the relay Worker near its database.
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      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 ## Verification headings, and it does not provide the required approval or focused verification details.

      Resolution

      Restructure the description using all required headings. Add a ## Scope and approval section with the triaged issue or explicit maintainer approval, or explain why this focused configuration change qualifies for an exemption. Add a ## Verification section 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.

      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      📝 Generate docstrings
      • Commit to this branch
      • Create a new PR
      🧪 Generate unit tests (beta)
      • Commit to this branch
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Comment `@coderabbitai help` to get the list of available commands.
    3. Approvability

      Verdict: Needs Human Approval

      Macroscope's review found this PR requires human approval — This change explicitly relocates all relay Worker fetch handling to us-west-2, altering production request routing and latency tradeoffs even though no application handler code changes. Its small diff is clear, but the production deployment and runtime-default impact merits human review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. juliusmarminge merged this758d773

    5. juliusmarminge closed this

Sign in to join this conversation.