opyebAPI Bench

Connection pool sizing without superstition

Capacity · 6 min read

Connection pools are where capacity plans meet reality. The formula says you need 125 concurrent requests; the database says it will accept 80 connections. Between those two numbers sits a decision that determines whether latency stays flat or collapses under load.

What a pool actually does

A pool caps how many requests can hold a database connection at once. Requests beyond the cap wait in a queue, which is invisible in application metrics unless you measure acquisition time. A pool that is too small throttles healthy traffic; a pool that is too large pushes the queue into the database, where it is harder to see and much harder to fix.

Pool sizeQueue locationObservable symptom
Too smallApplicationHigh pool wait time, low CPU
RightBrief bursts onlyStable latency, some waiting at peak
Too largeDatabaseRising query time, lock contention, CPU saturation

The arithmetic

Concurrency needed is request rate multiplied by the time a request holds a connection. If a request holds a connection for 40 ms at 500 requests per second, the pool must sustain 20 concurrent connections just for that query. Add the other queries in the request path, and the number quickly doubles. Divide by the number of instances to get the per-instance pool.

Formula: connections per instance = (rps × hold time in seconds) ÷ instances, plus a small buffer for bursts. If that exceeds what the database can serve, add a queue or a read replica rather than raising the pool.

Why oversized pools are slower

Beyond a certain point, more connections reduce throughput. Each connection costs memory on the database, complicates its scheduler, and increases contention on locks and latches. A database with 400 active connections often serves fewer queries per second than the same database with 80, because time is spent switching rather than executing. The pool is not a throughput knob; it is a queue placement knob.

Finding the number

Start from the database's own capacity, measured with a realistic query mix, then divide by the number of application instances and subtract a margin for administrative connections. Watch three metrics during a load test: pool acquisition time, database active connections and query latency. If acquisition time rises while database CPU is low, the pool is too small. If database CPU saturates before the application does, the pool is too large or the queries need work.

Finally, set a pool timeout and treat exceeding it as a capacity event, not an error to retry immediately. A request that waits two seconds for a connection and then retries makes the queue longer, which is the same amplification problem as an uncontrolled retry storm.

Pool behaviour under partial failure

Pools behave worst when a dependency is slow rather than down. If queries take three seconds instead of forty milliseconds, every connection is held seventy-five times longer, and a pool sized for the healthy case becomes a queue within seconds. Two protections help: a statement timeout that fails slow queries before they hold a connection indefinitely, and a pool acquisition timeout that surfaces the queue to the caller instead of hiding it.

Also separate pools by workload. Background jobs, admin queries and user traffic competing for the same connections means a batch job can starve the interactive path at exactly the moment users notice. Separate pools with separate limits make the contention explicit and let you decide who gets priority.

  • Set a statement timeout shorter than the request budget.
  • Give batch and admin work their own pool.
  • Alert on pool acquisition time, not only on query time.

A note on serverless

Serverless functions multiply instances faster than any pool can adapt, which is why connection proxies exist. If you run functions without a proxy, treat the database connection limit as the real concurrency limit and queue inside the function with a short timeout. Otherwise a traffic spike creates hundreds of short-lived connections, and the database spends its time accepting and closing rather than answering.

Open the calculators and put these numbers to work on your own API.

Open API Bench

Frequently asked questions

Is a pool of 100 per instance too large?

Usually yes. Multiply by your instance count before deciding: 20 instances with 100 connections each asks the database for 2,000 connections, which most databases handle badly. Size from the database side first.

Should I use a connection proxy?

For serverless or highly elastic workloads, a proxy that multiplexes connections is usually necessary. For a fixed fleet, a correctly sized pool is simpler and has fewer failure modes.

How do I know the pool is the bottleneck?

Pool acquisition time rises while CPU and query time stay normal. If query time rises with the pool, the database is the bottleneck and a bigger pool will make it worse.

Sources

Related guides