Database connection pool sizing: balance wait time and connection limits
A connection pool reuses database connections so each request does not need to create a new one. A larger pool does not automatically increase throughput. Set a modest cap and tune it under load while considering the database's concurrency budget, application instance count, connection hold time, and acquisition timeout.
A connection pool reuses database connections so each request does not need to create a new one. A larger pool does not automatically increase throughput. Set a modest cap and tune it under load while considering the database's concurrency budget, application instance count, connection hold time, and acquisition timeout.
How a pool works
A request borrows a connection, performs a short operation or transaction, then closes it to return it to the pool. In pooled APIs, close usually means return the logical connection, not close the physical socket. Reuse avoids repeated connection setup, but long transactions or a missing close make other requests wait. Do not share one connection across simultaneous tasks; use transaction management or try/finally so failures still return it.
In JDBC, DataSource provides connections to a physical data source. Implementations can be basic, pooled, or distributed-transaction capable; the interface name alone does not mean pooling is enabled.
Size the fleet, not one process
The maximum pool setting must fit the combined budget across all app instances, while leaving capacity for other clients, administration, monitoring, and failover. PostgreSQL's max_connections caps concurrent server connections; increasing it can also increase resource allocation.
For example, six instances with a pool cap of 12 could request up to 72 connections. This is upper-bound arithmetic, not a recommendation or actual concurrency. Measure pool wait, database CPU/I/O, and query latency under target load. HikariCP says sizing guidance is a starting point and should be tested; do not treat a CPU-based formula as a substitute for testing your environment.
Waits, lifetime, and returns
In HikariCP, maximumPoolSize includes idle and in-use connections. When all are busy, borrowers wait up to connectionTimeout and then fail. An excessively long timeout can hide overload in a request queue; keep it consistent with the overall request deadline and retry policy.
maxLifetime controls a connection's maximum lifetime; an in-use connection is removed after it is returned. HikariCP recommends setting it a few seconds below any database or infrastructure lifetime limit. idleTimeout applies when minimumIdle is below maximumPoolSize. These meanings vary by pool, so check the version you use. JDBC 4 drivers may not need a separate validation query.
Example: calculate a capacity budget
This is arithmetic, not a configuration recommendation. If a database allows 100 connections, reserve 20 for operations, and the application can scale to five instances, the application-wide budget is at most 80, or an average cap of 16 per instance. Choose less if other clients, uneven scaling, or failure headroom require it.
| Check | What to inspect | Signal for adjustment |
|---|---|---|
| DB budget | Server cap minus other clients and reserve | Refusals or resource pressure: consider lowering the app-wide cap |
| Pool wait | Active/idle counts, waiters, acquisition latency | If DB is saturated, a larger pool may worsen it |
| Connection hold | Transaction duration, streams, missing close | Inspect code paths and transaction scope |
| DB load | CPU, I/O, locks, query latency | Improve queries/concurrency before raising cap |
Apply and troubleshoot
Calculate the total connection cap across every app instance, including autoscaling maximums.
Set a conservative pool cap and bounded acquisition timeout; check the pool's actual defaults and semantics.
Load-test expected demand. Record throughput, p95/p99 latency, pool waits, active/idle counts, database resources, and errors.
When waits rise, distinguish leaks, long transactions, slow queries, and DB saturation. Do not simply enlarge a pool when the DB is saturated.
Change one factor at a time in stages; roll back if error rates or DB connection pressure rise.
Sources and scope
Java SE 21, [DataSource](https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/javax/sql/DataSource.html)
HikariCP, [configuration](https://github.com/brettwooldridge/HikariCP)
HikariCP, [About Pool Sizing](https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing)
PostgreSQL 18, [Connections and Authentication](https://www.postgresql.org/docs/18/runtime-config-connection.html)
Product details are scoped to JDBC/HikariCP and PostgreSQL 18. The pool-sizing formula is a possible starting point, not a universal rule. No application performance test was run.