Relationships
#5091 API Mongo request pool exhausted: Attio sync uses the request lane, and slow /api/v1/users/:name bursts
Opened by swamp_lord · 11/25/2026
Problem
On 2026-11-24 the swamp-club API ran out of Mongo request-pool connections three times (~10:30, ~11:05 and ~16:50 UTC, each 5–6 minutes). Requests waited the full 5 s waitQueueTimeoutMS for a connection, failed with "Timed out while checking out a connection from connection pool", and some returned db-pool-busy 503s. Atlas itself was fine: in every window about 20 different infrastructure.mongo.* operations took over 1 s while the median stayed at 8–12 ms. That is requests queueing for connections, not slow queries.
Why it happens
There are two causes. Both lead to the same shared resource, the per-pod request pool (lib/db.ts: maxPoolSize: 150, waitQueueTimeoutMS: 5000).
1. Attio CRM sync runs its work on the request pool (the recurring part)
lib/db.ts gives background loops their own pool (backgroundMongoClient, maxPoolSize: 10). The stated reason is "timer-driven loops only, so a slow sweep can never hold a request's connection", and lib/app/repos.ts repeats it ("background loops take the "background" lane…").
Attio sync only half follows that rule:
- Its queue is on the background lane:
getAttioSyncQueue("background")inmain.ts. - The work it does per entry is on the request lane.
getAttioSyncDeps(),getAttioPersonSweepDeps(),getAttioStageSweepDeps()andgetAttioPersonCoverageDeps()all callgetDatabase()(lib/app/repos.ts, around lines 2500–2590).
So every Attio sync burst competes with user requests for request-pool connections. The bursts match the pool timeouts (counts per 5 minutes, from the swamp_club_prod traces):
| 5 min from (UTC) | queue.attio-sync.* spans |
pool-checkout timeouts |
|---|---|---|
| 10:00 | 17,737 | 0 |
| 11:00 | 20,232 | 10 |
| 16:45 | 6,475 | 2 |
| 16:50 | 8,259 | 8 |
| 17:00 | 7,099 | 2 |
2. Bursts on GET /api/v1/users/:name (the 10:20–10:40 window, the only one with 503s)
Between 10:20 and 10:40 total traffic was flat: 13.4k requests, against 12.7k in the previous 20 minutes. But /api/v1/users/:name went from 266 to 1,941 requests (7×):
- 979 of them took over 2 s, and the endpoint's p99 went from 2.1 s to 5.4 s;
- they held request-pool connections long enough to cause 21 pool-checkout timeouts and 12 × 503;
- no other endpoint's latency changed until the pool ran out.
The traffic looks like one client walking through user profiles one after another (later on the same day the same pattern was rate-limited with 429s). An endpoint that takes 2–5 s per call is enough to drain the pool under that kind of load.
Why it's necessary to fix
- User-facing failures: pool exhaustion fails unrelated requests (login,
whoami, extensions, the CLI's calls) with 503s or 5 s stalls. It isn't limited to the endpoint or job that caused it. - It recurs: Attio sync runs in bursts several times a day, so this keeps happening.
- The design already says this mustn't happen: background work must not hold request connections. The Attio deps just don't follow the rule.
- Scaling out doesn't fix it: each pod's Attio work and slow profile reads still draw from that pod's request pool, and the total is capped by Atlas' per-node connection limit (see the
lib/db.tssizing comment).
Suggested fix
- Put all Attio sync and sweep dependencies on the background lane: in
getAttioSyncDeps,getAttioPersonSweepDeps,getAttioStageSweepDepsandgetAttioPersonCoverageDeps, uselaneDatabase("background")instead ofgetDatabase(). Then add a test or lint check that nothing a background loop starts callsgetDatabase(). - Make
GET /api/v1/users/:namecheaper: find out which query takes 2–5 s, then index or cache it. Consider also a tighter per-client rate limit on that path, so walking through profiles can't drain the pool.
Acceptance
- During an Attio sync burst, the request pool's checked-out count and the API's p99 stay at baseline, and there are no "checking out a connection" timeouts.
/api/v1/users/:namep99 stays under 1 s at several times its normal request rate.- No
db-pool-busy503s in a week of normal traffic.
Open
No activity in this phase yet.
Sign in to post a ripple.