Repository navigation
Coalesce concurrent connect gate lookups so D1 stops overloading - #5884
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
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 isD1_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
SettledLookupCacheso 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: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:SettledLookupCachecoalesces 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 withinLOOKUP_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.freshlookups (tunnel dials) still always query D1. TTLs and revocation behavior are unchanged.apps/connect/src/worker.ts: the catch-all logsString(error). It used to log theErrorobject, 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
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.bb-connect-staging, deployed main then this branch, same scripts, same unauthenticated GETs):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.🤖 Generated with Claude Code