Opening a database connection is expensive: a network handshake, TLS, authentication and server-side setup. A connection pool keeps a set of open connections and lends them to requests, which return them when done. Pools make apps fast, and they also create one of the most common failure modes in web systems: everyone waiting for a connection.
How a pool behaves
- Checkout: a request asks the pool for a connection. If one is idle, it gets it immediately.
- Wait: if all connections are in use and the pool is at its maximum, the request waits, up to an acquire timeout.
- Timeout: if none frees up in time, the request fails with "timeout acquiring a connection" or similar. See connection pool exhausted.
- Return: when the request finishes, the connection goes back to the pool.
Sizing: two constraints
Inside the app: the pool should cover the number of requests that need the database at the same time. For a thread-per-request server, that is roughly the thread count; for async servers, the concurrency you allow.
At the database: the total across all instances must stay below the database's limit:
| Instances | Pool size each | Total | Postgres max_connections |
|---|---|---|---|
| 4 | 20 | 80 | 100 ✓ |
| 12 | 20 | 240 | 100 ✗ |
| 12 | 20 → via PgBouncer | 240 client, 40 server | 100 ✓ |
As you scale out, per-instance pools multiply. That is where a pooler or proxy helps.
Bigger is not faster
A database has a limited number of CPU cores and disks. Beyond a certain number of concurrent queries, adding connections increases contention and makes every query slower. A common rule of thumb for PostgreSQL is a server-side pool of a few times the number of CPU cores, then let clients queue. Measure: if pool wait time is high but database CPU is low, the pool is too small; if database CPU is pegged, more connections will not help.
Poolers and proxies
- PgBouncer (PostgreSQL) in transaction mode lets many client connections share a small number of server connections. Note that session features such as prepared statements and session-level settings need care in transaction mode.
- RDS Proxy, ProxySQL and similar services do the same for managed databases and MySQL.
Settings that matter
- Maximum size: as discussed.
- Minimum idle: keep a few connections warm to avoid latency after quiet periods.
- Acquire timeout: fail fast (a few seconds) rather than holding a request for 30 seconds.
- Idle timeout and max lifetime: recycle connections before the database or a firewall drops them silently.
- Validation: test connections before use if networks drop idle connections.
- Leak detection: log a stack trace when a connection is held too long (HikariCP has
leakDetectionThreshold).
The metrics to watch
| Metric | Meaning | Action |
|---|---|---|
| Active connections | In use now | Near max for long: investigate slow queries |
| Pending / waiting requests | Requests waiting for a connection | Above zero: pool too small or connections held too long |
| Acquire time | Time to get a connection | Rising: saturation |
| Connection errors | Failed to open | Database limit or network |
In traces, pool waiting shows as a gap at the start of a request before the first database span. VitalRix APM traces make that gap visible next to the queries themselves.
Why connections are held too long
- Slow queries (find them).
- Transactions left open while calling external APIs.
- N+1 query patterns making many round trips per request.
- Code paths that forget to release connections on errors.
Fix those first; resizing the pool comes after. Related: PostgreSQL monitoring and MySQL too many connections.
A worked example
A Rails app ran 10 Puma workers per pod with 5 threads each, but pool: 5 in database.yml was shared per process, which was fine. The problem appeared after scaling to 16 pods: 16 × 10 × 5 = 800 potential connections against a PostgreSQL limit of 200. At peak, new connections were refused and errors spread across every endpoint. Adding PgBouncer in transaction mode with a server pool of 60 connections let all 800 clients share 60 real connections. Database CPU fell, because fewer concurrent queries competed, and p95 latency improved.
Language specifics
- Java (HikariCP):
maximumPoolSize,connectionTimeout(acquire timeout),leakDetectionThreshold. - Node.js (pg):
max,idleTimeoutMillis,connectionTimeoutMillis; one Pool per process, not per request. - Python (SQLAlchemy):
pool_size,max_overflow,pool_timeout,pool_pre_ping. - Go (database/sql):
SetMaxOpenConns,SetMaxIdleConns,SetConnMaxLifetime.
Alerts to set
- Pending connection requests above zero for 5 minutes per instance.
- Acquire time p95 above 100 ms.
- Database connections above 80% of max_connections.
- A sudden rise in connection errors after a deploy (often a leak).
How big should my connection pool be?
Big enough for the concurrent database work in one instance, and small enough that all instances together stay well under the database limit.
Do serverless functions need pooling?
Yes, more than most: each function instance can open its own connections. Use a proxy such as RDS Proxy or PgBouncer.
What causes a connection leak?
Code that checks out a connection and does not release it on every path, especially error paths.
Try VitalRix free
Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.