How to Choose a Database Connection Pool Size for Your Traffic

Database connection pool size is the number of connections your application keeps open and ready to hand to a request. Set it from the concurrency your database can actually serve, not from the number of users you have, and most sizing problems disappear before they start.

The confusion comes from thinking of pool size as a capacity dial. It is not. Each connection is a server-side process or thread holding memory, a transaction context and locks, and the database will spend time switching between them. Adding connections past the point where the server is saturated makes every query slower, not faster. The right number is the smallest one that keeps requests from waiting on the pool, and it is usually lower than people guess.

How the connection handshake constrains your database connection pool size

A connection is not free to create. With PostgreSQL the client opens a TCP socket, sends a startup message, the server spawns a backend process, and authentication runs, which with scram-sha-256 means a round trip for the challenge and another for the proof. That is why pools exist: reuse beats reconnecting per request. On MySQL and MariaDB the equivalent is a thread per connection plus a handshake, and TLS adds a full negotiation on top.

Every backend holds a fixed slice of memory. PostgreSQL exposes this directly, so you can compute the ceiling rather than guess it:

psql -c "show max_connections;"
psql -c "show shared_buffers;"
psql -c "select count(*), state from pg_stat_activity group by state;"

The first two give you the configured limit and the shared memory footprint. The third tells you how many connections exist and what they are doing. If most rows are idle, your pool is too large for the work arriving. If you see many active rows and a queue at the application, either the pool is too small or individual queries are too slow. Those are different problems and the fix differs.

Calculating a starting database connection pool size from traffic

Start from concurrency, not user count. A useful approximation is the number of requests in flight multiplied by the fraction of each request's time that is spent on the database. A request that takes 200 ms and spends 20 ms in queries occupies a connection for roughly a tenth of its life, so ten concurrent requests need about one connection on average. Leave headroom for bursts and skew, and you land at a small number.

Then cap it against the server. Reserve connections for administration, replication and migrations, and divide the remainder across application instances. If a database allows a few hundred connections and you run several app servers plus a pooler, each app instance should get a modest share, not an equal split of the maximum.

# postgresql.conf, illustrative shape only
max_connections = 200
superuser_reserved_connections = 3

# app instance pool
pool:
  max_open: 20
  max_idle: 20
  max_lifetime: 30m

Keep max_idle close to max_open. A pool that opens connections and then closes them under load churns through handshakes exactly when you can least afford it. Set max_lifetime so connections are recycled periodically, which lets you rotate credentials and fail over cleanly.

When a pooler belongs in front

If many app instances share one database, put a pooler between them. Transaction-level pooling multiplexes many client connections onto few server connections, which decouples your application's concurrency from the database's connection limit. The tradeoff is that session state does not survive between transactions, so anything relying on session-level settings, advisory locks held across statements, or prepared statements needs care. Check your driver and pooler documentation for what is supported before you rely on it.

Symptoms of a database connection pool size that is wrong

A pool that is too small shows up as requests waiting for a connection rather than waiting on the database. Look for pool acquisition timeouts, growing latency with flat database CPU, and errors like connection pool exhausted or timeout acquiring connection. The database looks healthy while the application stalls, which is the signature. Confirm it by watching the pool's wait time metric next to query latency: if wait time rises while query time is steady, the pool is the bottleneck.

A pool that is too large shows up on the server. Connection counts climb, context switching rises, and throughput falls as you add connections. Queries that were fast become slow under the same load. On PostgreSQL you can watch this directly:

select state, wait_event_type, count(*)
from pg_stat_activity
group by 1, 2 order by 3 desc;

Lots of idle connections doing nothing is wasted server memory. Lots of active connections all waiting on Lock or IO means the database is the constraint and more connections will not help. Check max_connections against actual peak usage over a full traffic cycle, including batch jobs and migrations, before you change anything.

Measuring instead of guessing

Instrument three numbers per pool: current open connections, the time a request waits to acquire one, and query duration. If acquisition wait is near zero and query duration is stable, the pool is fine. If acquisition wait grows, raise the pool slightly and watch whether database throughput improves. If it does not, the bottleneck is elsewhere and you should look at indexes, query plans and lock contention. Change one variable at a time and give each change a full traffic cycle before judging it.

Next, take a baseline. Run the pg_stat_activity query at peak and at quiet times, record the counts, and compare them with what your pool reports. If the two disagree, your pool is not doing what you think, and that is worth fixing before you touch the size.

Related articles

Subscribe to our newsletter

Get the latest hosting tips, performance insights, and industry news.