A Bigger Connection Pool Is Not Faster: Concurrency, Queues, and Timeouts
When requests wait for a database connection, increasing maximumPoolSize feels like the obvious fix. It may only move the queue into PostgreSQL, where extra sessions compete for CPU, memory, I/O, and locks. Latency grows, timeouts overlap, and a rolling deployment can exhaust the server before traffic rises. A pool is not a throughput generator; it is a bounded admission queue in front of a finite database.
Start with a fleet budget
PostgreSQL’s max_connections is a hard server limit, and raising it can increase shared-memory requirements. Do not divide that number by one application’s current replica count. First reserve capacity for administration, migrations, monitoring, replication, failover, and other services. Then budget the remainder across every process and pool that can exist at the same time.
application budget = database limit - operational reserve - other clients
worst-case demand = instances × processes × pools × maximumPoolSize
The worst case includes autoscaling and both old and new replicas during a rolling deployment. A service with ten replicas, two worker processes, and separate read and write pools owns forty pools, not two. Minimum idle connections can also create a connection storm when many instances start together. Put the calculation in deployment configuration and fail a capacity check before rollout rather than discovering it in production.
Find the knee, not the largest number
HikariCP’s sizing guide uses roughly twice the physical core count plus effective spinning disks as a historical starting point, while warning that deployment-specific load tests are decisive. Treat that as a hypothesis, not a formula for managed databases, SSDs, replicas, or mixed workloads.
Sweep several pool sizes under representative traffic. Record throughput, request p95/p99, acquisition wait, database CPU, I/O, lock waits, and errors. The useful setting is near the smallest pool that reaches the throughput plateau without unstable tail latency. Beyond that knee, more active queries usually add scheduling and lock contention instead of useful work.
A pool full of busy connections is not automatically unhealthy. A bounded number of callers waiting briefly is how admission control works. An unbounded application queue is the real danger: work can wait past its usefulness while retaining memory and upstream resources.
Put every wait inside one deadline
The request deadline must contain connection acquisition, lock waiting, statement execution, serialization, and network response. Configure the acquisition timeout shorter than the remaining request budget so a caller cannot obtain a connection after its client has already given up.
request deadline: 2,000 ms
connection acquisition: 150 ms
lock wait: 200 ms
statement execution: 1,400 ms
response reserve: 250 ms
These values are an example, not defaults. PostgreSQL provides lock_timeout, statement_timeout, and idle_in_transaction_session_timeout; apply them by workload or role rather than one indiscriminate global value. A timeout must cancel server-side work, not merely abandon the client future while the query keeps occupying a connection. When acquisition expires, fail fast with an overload result and do not launch synchronized immediate retries.
Hold a connection only around database work
Acquire late and release early. Never hold a transaction while calling another service, waiting for a user, performing a long CPU task, or streaming a response. Use language-level scope guards, defer, or finally so cancellation and exceptions return the connection. Avoid nested acquisition: one request holding a connection while waiting for another can deadlock a pool even when the database is idle.
Long transactions amplify every capacity problem. They retain connections and locks, delay vacuum cleanup, and expand the retry surface. Split business workflows at explicit commit points; when a message must follow a commit, use an outbox or another durable handoff instead of keeping the transaction open across the network.
Separate online requests from background jobs when their service levels differ. Give each pool its own connection budget and bound the worker concurrency to match it. Separation prevents a report from consuming every interactive slot, but it does not create new database capacity—the combined maximum still has to fit the fleet budget.
A proxy changes semantics, not physics
PgBouncer session pooling assigns a server connection for the client’s whole session. Transaction pooling returns it after each transaction, increasing reuse but breaking session assumptions. Its current compatibility table marks session-level SET, LISTEN, session advisory locks, and several temporary-table behaviors as incompatible with transaction pooling. Protocol-level prepared statements require the documented configuration.
Audit drivers and application code before switching modes. Reset session state, make transaction boundaries explicit, and test migrations, prepared statements, advisory locks, and notification listeners. A proxy can multiplex clients onto fewer server sessions; it cannot make the database execute unlimited simultaneous queries.
Connection lifetime also needs coordination. Keep pool lifetime below an upstream proxy or server lifetime, add jitter so connections do not all recycle together, and validate a connection before reuse after network or failover events. During an outage, cap concurrent creation attempts and back off with jitter; otherwise every empty pool becomes a reconnect amplifier.
Observe ownership and prove the limit
At minimum, export active, idle, and pending counts; acquisition time and timeout count; connection hold duration; creation failures; and pool identity by service and workload. Correlate them with database CPU, active sessions, lock waits, and slow statements. A full pool with an idle database often points to leaked connections or non-database work inside transactions. Many idle connections across the fleet indicate an oversized minimum or excessive fragmentation.
Capacity tests should include normal peaks, a slow query, lock contention, one leaked connection, database restart, proxy termination, autoscaling, and a rolling deployment. Verify that the fleet stays below its budget, queues remain bounded, cancelled work releases resources, and recovery does not produce a login storm.
The right pool size is therefore a measured concurrency contract: the database receives only the work it can finish, callers wait only within a useful deadline, and deployment topology cannot silently multiply demand. Once the budget, queue, transaction scope, proxy semantics, and failure behavior are explicit, pool tuning becomes an engineering exercise instead of repeatedly raising a number.