fix(worker): account for worker deploy overlap in DB connection budget (BIT-33530) #322

Merged
jos-alves merged 1 commits from fix/BIT-33530-worker-deploy-connection-overlap into main 2026-12-04 09:35:31 +00:00
Member

Root cause

dbConnectionBudget in capacity.ts reserved room for an API deploy overlap (apiReloadOverlap, BIT-31550) but not for a worker deploy overlap. PM2 startOrReload on the fork-mode photoreport-worker starts the new process before the old one finishes its 25 s drain (DRAIN_MS in worker.ts), so both Prisma pools and both broadcasters are open at once.

With defaults:

  • Worker Prisma pool: 47 (capped by the budget)
  • Worker broadcaster: 1
  • During worker overlap: 2 × (47 + 1) = 96
  • API steady state: 2 × (10 + 3) = 26
  • Total: 122 > 100 max_connections → PostgreSQL error 53300 "sorry, too many clients already"

This is the same class of bug as BIT-31550 (API reload overlap), just for the worker.

Fix

Add workerReloadOverlap (default 1) to dbConnectionBudget. The formula now divides the remaining budget by (1 + workerReloadOverlap), so the total stays under max_connections even when two workers coexist during a reload.

The worker's Prisma pool drops from 47 to 23 with defaults — still enough for the concurrent jobs the capacity planner sizes, and the headroom and ad-hoc connection margin are preserved.

// Before: available = max - superuserReserved - apiConnections - 1 - headroom = 47
// After:  available = floor((max - superuserReserved - apiConnections - headroom) / (1 + workerOverlap)) - 1 = 23

Verification

  • pnpm --filter @photoreport/api build — clean
  • pnpm --filter @photoreport/api typecheck — clean
  • vitest run --config vitest.unit.config.ts — 280 passed, 3 skipped
  • New test: total connections during a worker deploy overlap stay ≤ max_connections
  • Moved test/capacity.test.ts into the unit config (pure test, no database needed)

Paperclip-Issue: BIT-33530

## Root cause `dbConnectionBudget` in `capacity.ts` reserved room for an API deploy overlap (`apiReloadOverlap`, BIT-31550) but not for a **worker deploy overlap**. PM2 `startOrReload` on the fork-mode `photoreport-worker` starts the new process before the old one finishes its 25 s drain (`DRAIN_MS` in `worker.ts`), so both Prisma pools and both broadcasters are open at once. With defaults: - Worker Prisma pool: 47 (capped by the budget) - Worker broadcaster: 1 - During worker overlap: 2 × (47 + 1) = 96 - API steady state: 2 × (10 + 3) = 26 - **Total: 122 > 100 `max_connections`** → PostgreSQL error 53300 "sorry, too many clients already" This is the same class of bug as BIT-31550 (API reload overlap), just for the worker. ## Fix Add `workerReloadOverlap` (default 1) to `dbConnectionBudget`. The formula now divides the remaining budget by `(1 + workerReloadOverlap)`, so the total stays under `max_connections` even when two workers coexist during a reload. The worker's Prisma pool drops from 47 to 23 with defaults — still enough for the concurrent jobs the capacity planner sizes, and the headroom and ad-hoc connection margin are preserved. ``` // Before: available = max - superuserReserved - apiConnections - 1 - headroom = 47 // After: available = floor((max - superuserReserved - apiConnections - headroom) / (1 + workerOverlap)) - 1 = 23 ``` ## Verification - `pnpm --filter @photoreport/api build` — clean - `pnpm --filter @photoreport/api typecheck` — clean - `vitest run --config vitest.unit.config.ts` — 280 passed, 3 skipped - New test: total connections during a worker deploy overlap stay ≤ `max_connections` - Moved `test/capacity.test.ts` into the unit config (pure test, no database needed) Paperclip-Issue: BIT-33530
jos-alves added 1 commit 2026-12-04 09:20:50 +00:00
fix(worker): account for worker deploy overlap in DB connection budget (BIT-33530)
ci / typescript (push) Skipped
ci / rust (push) Skipped
ci / rust (pull_request) Successful in 6m9s
ci / typescript (pull_request) Successful in 11m47s
d2616ce539
dbConnectionBudget reserved room for an API deploy overlap (apiReloadOverlap,
BIT-31550) but not for a worker deploy overlap. PM2 startOrReload on the
fork-mode photoreport-worker starts the new process before the old one finishes
its 25 s drain, so both Prisma pools and both broadcasters are open at once.
With defaults that is 2 * (47 + 1) + 26 = 122 > 100 max_connections, tipping
Postgres into error 53300 "sorry, too many clients already".

Add workerReloadOverlap (default 1) that divides the remaining budget by the
number of concurrent workers, so the total stays under max_connections even
during a reload. The worker's Prisma pool drops from 47 to 23 with defaults —
enough for the concurrent jobs the capacity planner sizes, and the headroom
and ad-hoc connection margin are preserved.

Paperclip-Issue: BIT-33530
jos-alves merged commit 23c2f29a05 into main 2026-12-04 09:35:31 +00:00
Sign in to join this conversation.