Database Connection Pool Exhaustion: How to Find It Before Production Does
Pool exhaustion looks like latency, then timeouts, with few errors in between. Size pools with Little's Law and watch wait time as well as active connections.
A connection pool is a small, fixed set of expensive resources shared by a large and variable number of requests. When demand fits inside the pool, nobody notices it. When demand exceeds it, requests wait for a connection, and the wait shows up as latency. The system does not throw errors at first. It gets slower, evenly, across every endpoint that touches the database. Then the waits cross a timeout, and the picture changes from slow to broken in a few seconds.
That progression is why exhaustion is easy to miss in a test. Error rate is flat while latency climbs, and a dashboard that tracks only errors and CPU shows nothing wrong until the cliff. This article covers how to size a pool from first principles, what to measure, and how to design a run that finds the edge.
Size from Little’s Law
Little’s Law says the average number of items in a system equals the arrival rate times the average time each item spends in it. For a pool, the items are connections in use, the arrival rate is database calls per second, and the time is how long each call holds a connection.
Take an example. A service handles 300 requests per second. Each request runs two queries, and each query holds a connection for 15 ms. Database calls arrive at 600 per second, each holding a connection for 15 ms, so about 600 times 0.015, which is 9 connections are busy on average. A pool of 10 is nearly full at average load. Bursts above average will queue.
Those numbers are only an illustration, but the structure is the useful part. Three things move the answer:
- Request rate, which grows with traffic.
- Queries per request, which grows with features and with N+1 patterns.
- Hold time per query, which grows when the database slows, which grows when more queries arrive.
The last one makes this unstable. If hold time doubles to 30 ms, average busy connections double to 18, and a pool of 10 now cannot serve the offered load at all. Requests queue, response time grows, and more requests are in flight at once, which makes the situation worse. The Little’s Law article walks through the arithmetic in more detail.
Hold time is easy to underestimate. A connection is held from checkout to return, beyond the time a query runs. If your code opens a transaction, calls an external HTTP service, then writes, the connection is idle but held during the external call. A 200 ms call inside a transaction turns a 5 ms query into a 205 ms hold. Check the code path for work done while holding a connection, and move it outside the transaction.
Count every pool
The database has its own ceiling on connections. PostgreSQL’s default max_connections is 100, for example, and each connection costs server memory. A pool of 20 per instance on 10 instances is 200 potential connections, above that default. Multiply per-instance pool size by instance count, including the extra instances an autoscaler adds under load, and compare the total with the database limit and with any connection proxy in front of it.
Autoscaling interacts with this in a way tests often skip. When the application scales out because latency rose, the new instances open new pools, which raises total database connections just as the database is slow. Test the scaled-out state, as well as the starting one.
A connection pooler such as PgBouncer changes where the queue forms. The application pool, the pooler and the database each have a limit, and a request can wait at any of them. Record wait time at each layer you operate.
What to watch
Active connection count is the first number everyone looks at, and it saturates at the pool maximum, then stays flat. A flat line at the maximum tells you the pool is full, but not whether anyone is waiting. The more informative signals are about waiting:
- Pool wait or acquire time: how long a request spends obtaining a connection. In libraries that expose it, this is the most direct exhaustion signal. HikariCP in the Java ecosystem, for instance, reports connection acquisition time and a count of pending threads.
- Pending requests or queue depth at the pool.
- The acquire timeout: how long the pool waits before giving up. Many pools default to tens of seconds, and some to unbounded waits, so check yours.
- Database-side connection count and, if available, the number of sessions waiting.
- End-to-end latency by endpoint. Exhaustion hits endpoints that use the database more, so a read-only cached endpoint may stay fast while a query-heavy one slows.
If your pool library does not export wait time, add a timer around the checkout call. It is a few lines of code and it answers the question the other metrics cannot.
Design the run to find the edge
A flat-rate test at expected load will not find exhaustion unless the pool is already near its limit. Use shapes that probe the margin.
Step the arrival rate upward in stages and hold each stage long enough to see a stable value for wait time, say several minutes. Plot pool wait against arrival rate. Wait time stays near zero while the pool has headroom, then bends upward. The bend is your practical limit, and it tends to come earlier than the point where active connections hit the maximum.
Slow the database on purpose. Hold the load constant and add latency to queries, for example with a throttled replica, a slow-query injection, or a lock held briefly on a table. Since hold time is a term in Little’s Law, a modest slowdown of the database can exhaust a pool that looked comfortable. This is the scenario that matches real incidents, where traffic was normal and a dependency got slower.
Burst, then recover. Send a short spike above the pool’s capacity and watch two things: how deep the queue gets, and how long it takes to drain after the spike ends. A pool that drains slowly, or not at all, suggests retries or long transactions keeping it saturated.
Include the timeout boundary. Raise load until some requests wait longer than the acquire timeout, and look at what the application does: does it return a clean error, retry, or hang a request thread? Retries amplify exhaustion, so connect this to your retry policy.
Reading the transition
A typical failing run shows a sequence. Pool wait is near zero. As load rises, wait becomes visible, perhaps tens of milliseconds, and p95 rises with it while the error count stays at zero. Then a threshold is crossed: wait exceeds the request timeout upstream, and errors jump. In the graphs, the latency line bends first, and the error line follows by seconds or minutes.
If your pass criteria only look at errors, the run passes during the bend. Add a criterion on latency or on pool wait time, so that the run fails when the queue forms, not when it overflows. For example: p95 pool wait stays under 20 ms at the target rate and under 100 ms during the injected database slowdown. Pick numbers from your own latency budget.
Tuning without guessing
Resist the urge to raise the pool size whenever it saturates. A larger pool sends more concurrent queries to a database that may already be the bottleneck, and past a point more concurrency lowers throughput through contention. The right size is usually small, close to what the database can run in parallel, and the queue in front of it is a feature: waiting briefly for a connection is cheaper than overloading the database.
Work through the causes in this order:
- Shorten hold time: remove external calls from transactions, add missing indexes, fix N+1 queries, cache repeated reads.
- Reduce queries per request where possible.
- Right-size per-instance pools so that the sum across instances fits the database limit with headroom for scale-out.
- Set an acquire timeout shorter than the request timeout, so a request fails fast and cleanly instead of consuming a thread while it waits.
- Add load shedding or a bulkhead so one slow endpoint cannot take the entire pool.
Re-run the same stepped test after each change and compare wait time at the same arrival rates. The database bottlenecks article covers the storage-side checks that belong beside this one.
Running the test on MaxoPerf
On MaxoPerf, an arrival-rate step profile is part of the script you upload, and the run’s latency and error series show the bend as it develops. Failure criteria on latency stop the run from passing when the queue forms. Pool wait time lives in your application’s metrics, so send it to the monitoring you already use and read it beside the run, as the observability article describes.