What it is
A connection pool keeps a set of open database connections and lends them to requests: a request borrows a connection, runs its queries and gives it back. Pools live inside the application (HikariCP, the pools built into Npgsql, psycopg, node-postgres, SQLAlchemy and others) or in a separate pooler process in front of the database (PgBouncer, PgCat, Odyssey, managed proxies such as RDS Proxy).
Why it exists
Opening a connection is expensive. For Postgres it means a TCP handshake, usually a TLS handshake, an authentication exchange (SCRAM-SHA-256 takes two round trips) and the server forking a new backend process with its own memory. Doing that per request adds milliseconds of latency and burns database CPU on process management. On the other side, every open connection costs the database memory, and active ones compete for CPU, locks and I/O, so the number of connections has to be controlled, not just made cheap.
How it works
Borrow and return. The pool opens connections up front or on demand up to a maximum size. A request that finds all connections busy waits in the pool's queue; if it waits longer than the acquire timeout (connectionTimeout in HikariCP, Timeout in Npgsql), it fails. Connections are validated, recycled after a maximum lifetime, and closed when idle too long.
Capacity. A pool of N connections holding each for t seconds can serve at most N ÷ t requests per second. Near that limit, waiting time grows quickly (queueing); past it, the wait grows until requests time out. The wait does not show up in the database's query statistics, only in the application's latency.
The database's limit. A database works best with a limited number of active queries, roughly a small multiple of its CPU cores, more when queries spend most of their time waiting on disk or locks. Beyond that, extra concurrency adds context switching, lock contention and cache pressure: each query gets slower and total throughput can fall.
Multiplication. Pools are per process. With k instances of a service, each with a pool of n, the database sees up to k × n connections, plus those of every other service. Postgres refuses connections beyond max_connections (100 by default) with "sorry, too many clients already".
Poolers. PgBouncer accepts many cheap client connections and multiplexes them onto a small number of server connections per database and user (default_pool_size). Modes: session (a server connection per client session; little gain), transaction (a server connection only for the duration of a transaction; the common choice) and statement (per statement; no multi-statement transactions). Transaction mode breaks features that rely on session state: session-level SET, session advisory locks, LISTEN, WITH HOLD cursors, temporary tables kept across transactions. Protocol-level prepared statements are supported in transaction mode since PgBouncer 1.21 (max_prepared_statements).
When to use it
- Always use an application-side pool for a database that speaks a connection-oriented protocol.
- Add a pooler when the number of application processes is large or variable (autoscaling, many services, serverless functions, short-lived jobs), or when connection storms on deploys hurt the database.
- Use a managed proxy when the platform provides one and its limits fit (connection pinning rules, added latency).
When not to
- Do not size pools "big for safety": a pool larger than the database's useful concurrency only moves queueing into the database, where it is more expensive.
- Do not put session-dependent workloads (LISTEN/NOTIFY listeners, session advisory locks, long-lived cursors) behind a transaction-mode pooler; give them a direct connection or a session-mode pool.
- Very low-traffic scripts and admin tools can connect directly.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Small pool | Low contention in the database, predictable latency | Requests queue in the app at peaks |
| Large pool | Fewer app-side waits at low load | Contention, memory per connection, collapse under overload |
| Short acquire timeout | Overload fails fast, threads freed | More visible errors at peaks |
| PgBouncer in transaction mode | Many clients on few server connections | Another hop and component; session features restricted |
| Per-service pools without a pooler | Simple | Totals grow with instances and services; max_connections limits scale-out |
Alternatives
- Fewer, cheaper queries (see Query Performance): shorter hold times raise pool capacity more than extra connections do.
- HTTP-based data APIs for serverless environments, where the provider pools behind the API.
- Read replicas to spread read concurrency over more servers, each with its own pool limit.
- Queues to smooth bursts of write work into a steady rate the pool can handle.
Failure modes
- Pool exhaustion: slow queries, long transactions or a slow external call inside a transaction hold connections; everything else waits and times out.
- Oversized pools amplifying overload: the database thrashes, every query slows, timeouts and retries add more load.
- Connection storms: after a deploy or failover, every instance opens its pool at once.
- Scale-out hitting
max_connections: new instances fail to start or fail every request. - Leaked connections: code paths that never return a connection slowly drain the pool (use leak detection).
- Stale connections through firewalls or load balancers that drop idle TCP sessions; use keepalives and a maximum lifetime shorter than the idle timeout.
In production
Size the total, not the instance: decide how many active queries the database should run, then divide among instances and services, or put a pooler in front. Measure and alert on pool wait time and pool utilisation, not just query time. Set an acquire timeout below the request timeout. Keep transactions short so connections return quickly. Reserve connections for administration (superuser_reserved_connections, or a separate role limit) so you can still log in during an incident. Run PgBouncer redundantly and monitor its SHOW POOLS waiting clients.
Common misconceptions
- "More connections means more throughput." Only up to what the database can run in parallel; then it reverses.
- "Pool timeouts mean the network is broken." They mean demand exceeded what the connections can finish.
- "Pool size is per database." It is per process; the database sees the sum.
- "PgBouncer makes queries faster." It makes connections cheaper and limits concurrency; the queries cost the same.
Interview angle
When a design puts many stateless instances in front of one relational database, interviewers expect you to bring up connection limits: instances × pool size against max_connections, a pooler such as PgBouncer or a managed proxy, and pool sizing from throughput × query time. Mention that pool wait is part of latency and that the cure for exhausted pools is usually shorter queries and transactions, not bigger pools.
Model assumptions
Simulated, not a benchmark. The engine models one pool per database node: its size is the total number of queries the database runs at once (the sum of every instance's pool), waiting for a free connection counts toward latency, and waiting more than 500 ms fails the request. Per-instance pools, max_connections refusals and connection setup are not modelled separately; setup appears as an extra ~25 ms of database time per request, and its handshake packets are a scripted close-up. Contention is a cost model set by hand: about 40 ms per request's database work with up to ~24 active connections, about 120 ms at 60 and 250 ms at 100 (illustrative); the engine does not derive it. PgBouncer is modelled as a server with 24 workers, each held while its query runs, like transaction pooling. Arrivals are random (Poisson), durations lognormal.