Skip to content

Coalesce concurrent connect gate lookups so D1 stops overloading - #5884

Merged
ymichael merged 1 commit into
mainfrom
bb/connect-gate-coalesce-lookups
Oct 20, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/connect-gate-coalesce-lookups

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Since about Oct 19, sends and page loads over bb Connect keep failing with 502 {"code":"connect_gate_error"} ("bb connect hit a temporary problem"). The failures come in fleet-wide waves across every colo and host, up to 11,218 a minute at 17:24 UTC on Oct 20. Stored Workers Logs record 65,273 of them on Oct 19 and 98,036 on Oct 20, against 5k–15k a day the week before. They take about 5 ms and also hit /api/connect/desktop-session, which touches only D1 (no Durable Object), in the same minutes. So the throwing dependency is D1. The exception text is D1_ERROR: D1 DB is overloaded. Requests queued for too long. (staging tail of the same failure; production logged {} for the error, which this PR also fixes).

D1 is overloaded because the gate's own query rate grew. #5142 (deployed Oct 7 23:05 UTC) replaced the single-flight lookup caches with SettledLookupCache so a cancelled request could not hang others. That also dropped coalescing, so every request that misses the label, session, or machine-credential cache at the same moment runs its own D1 query. A page load fires 20–30 parallel requests, so each cache expiry became a 20–30-query herd per isolate. The herds also grow as D1 slows down, because lookups stay in flight longer. Production D1 reads per 1,000 gate requests:

Day (UTC) Gate requests D1 reads Reads per 1k requests Avg D1 queries/s
Oct 5 12.9M 5.13M 397 70
Oct 7 12.8M 5.50M 431 76
Oct 8 (first full day after #5142) 13.5M 7.32M 542 97
Oct 13 15.6M 9.64M 616 127
Oct 17 17.2M 12.21M 710 160
Oct 19 20.5M 14.75M 720 192
Oct 20 (partial) 15.9M 12.04M 758 156

D1 serves about 200–250 queries/s serially, measured on Oct 7. On Oct 20 the gate's per-minute peaks reach 12–15k reads (about 250 qps), so bursts overflow D1's queue and every in-flight lookup fails.

What changed

  • apps/connect/src/session.ts: SettledLookupCache coalesces concurrent misses again without sharing a promise across requests. The first request for a key runs the lookup and records its settled outcome on a plain object. Concurrent requests for the same key poll that object on their own 10 ms timers and return its value, or rethrow its error. No request ever awaits a promise created by another request, so Cache only settled bb connect gate lookups so a cancelled request cannot hang others #5142's cancelled-request hang cannot come back. If a lookup has not settled within LOOKUP_JOIN_TIMEOUT_MS (2 s), for example because its request was cancelled, the next request takes over with a fresh lookup that later waiters join. fresh lookups (tunnel dials) still always query D1. TTLs and revocation behavior are unchanged.
  • apps/connect/src/worker.ts: the catch-all logs String(error). It used to log the Error object, which Workers Logs stores as {}, so production has no exception text.

No wire, CLI, or schema changes. Merging deploys the gate (one tunnel reconnect wave, like any gate deploy).

Not changed: D1 stays the bottleneck as traffic grows (gate requests went up 60% in two weeks). This PR undoes the #5142 regression; the planned PlanetScale move is the structural fix.

How you verified

  • New tests in apps/connect/src/session.test.ts, all failing on main: a 20-request burst runs 1 D1 lookup instead of 20; a failed lookup is shared with its waiters instead of re-run; a lookup that never settles is replaced after the join timeout by exactly one new lookup. The three existing "earlier lookup never settled" tests still pass, so the Cache only settled bb connect gate lookups so a cancelled request cannot hang others #5142 hang stays fixed.
  • pnpm exec turbo run typecheck lint test --filter=@bb/connect: 270 tests pass.
  • Staging (bb-connect-staging, deployed main then this branch, same scripts, same unauthenticated GETs):
Staging run main (9ecb9bc) this branch
6 bursts of 40 concurrent requests to one label, 16 s apart 240 D1 reads 47 D1 reads
20 hosts × bursts of 20 every 4 s, 60 s (6,000 requests) 3,822 D1 reads 1,026 D1 reads
60 hosts × bursts of 20 every 4 s, 60 s (~17.5k requests, ~300 req/s) 9,604 gate 502s (55%) 0 gate 502s

The 502s on main are D1_ERROR: D1 DB is overloaded. Requests queued for too long. At 100 hosts (~430 req/s) both builds saturate staging D1 (about 60% 502s each), so this is headroom, not immunity. Staging is back on main's gate.

AGENT GENERATED

🤖 Generated with Claude Code

@ymichael
ymichael merged commit 6b3fa9d into main Oct 20, 2026
30 of 31 checks passed
@ymichael
ymichael deleted the bb/connect-gate-coalesce-lookups branch October 20, 2026 18:29
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