Connection Pooling Under Bursty Traffic
Sizing pools for bursts, not averages, and why a longer queue often beats a larger pool.
- Databases
- PostgreSQL
- Reliability
- Scaling
A pool sized for the average request rate fails at the exact moment it is needed: a deploy restarts instances, caches miss, and fifty requests arrive together. The database can serve more connections than you think; the application usually cannot afford them.
Count the whole fleet
Pool limits are per process. Twelve instances with a max of twenty connections each can open 240 server connections, which may exceed the database budget. Decide the global ceiling first, then divide it by the number of instances.
per_instance = floor(global_budget / instance_count)
Queue before you grow
Waiting in an application queue is cheap; waiting inside PostgreSQL on a lock is not. A slightly smaller pool with a short timeout turns a burst into a visible queue, and a visible queue can be monitored, shed, or retried with jitter.
Watch wait time, not pool size
An idle pool with high wait time usually means a slow query holding a connection, not a capacity problem. Log acquisition latency separately from query latency. The two have different fixes and are often conflated in dashboards.
Release connections around slow work
Never hold a pooled connection while calling a third party or rendering a template. Fetch the data, close the transaction, then do the slow work. The shortest useful transaction is the one that only touches the database.