Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Connection Pool Sizer

A database connection pool sizer built on Little’s Law. Enter your peak queries per second, average query latency, and how many application instances you run, and get the concurrency you actually need, a per-instance pool size with headroom, and a check against your database’s max-connections limit — so you neither starve requests nor exhaust the database.

8connections / instance
48total across 6 instances
Concurrency (Little’s Law)30.0
✓ Within the DB capTotal 48 connections uses 24% of the database cap (200). Comfortable — bigger pools would add contention, not throughput.

A connection pool is sized by Little’s Law: the connections in use at once equal the query rate times how long each query holds a connection. This sizer applies that to your whole fleet, splits it across app instances, adds burst headroom, and checks the total against the database’s connection limit, because every instance brings its own pool to the same database.

What it computes

concurrency  = QPS × latency_ms ÷ 1,000
per_instance = max(1, ⌈concurrency ÷ instances × (1 + headroom ÷ 100)⌉)
total        = per_instance × instances
cap_used     = round(total ÷ db_max × 100) %
  • QPS is peak queries per second across the whole fleet, not per instance.
  • latency_ms is the average time a query holds a connection: execution plus the round trip. If code holds a connection for a whole transaction, use the transaction time.
  • instances is the number of app processes that each own a pool; the math treats anything below 1 as 1.
  • headroom is a percentage added to each instance’s share for bursts. The result rounds up, and no pool is smaller than one connection.
  • db_max is the database’s connection limit, such as PostgreSQL’s max_connections. A total above it turns the status to “Over the DB cap”; otherwise the status reports the share used.

A worked example

The defaults describe 2,000 queries per second at 15 ms, spread over 6 app instances, with 50% headroom and a database limit of 200.

  1. Concurrency: 2,000 × 15 ÷ 1,000 = 30.0 connections busy on average across the fleet.
  2. Per instance: 30 ÷ 6 = 5, then 5 × 1.5 = 7.5, rounded up to 8 connections per instance.
  3. Total: 8 × 6 = 48 connections, or 48 ÷ 200 = 24% of the cap, so the status reads “Within the DB cap”.
Change from the defaultsConcurrencyPer instanceTotalCap used
None30.084824%
Queries slow to 60 ms120.03018090%
Queries slow to 100 ms200.050300over the cap
Traffic doubles to 4,000 QPS60.0159045%
Scale out to 30 instances30.026030%

Two lessons sit in that table. Latency moves the pool as much as traffic does, so a database that slows from 15 ms to 100 ms needs more than six times the connections at the same QPS, exactly when it can least afford them. And scaling out inflates the total through rounding, because each of 30 instances rounds its 1.5-connection share up to 2.

Assumptions and limits

  • Even load. Traffic is assumed to split evenly across instances. An instance that receives more than its share needs a bigger pool than the average.
  • Average hold time. Little’s Law works on averages. Slow queries hold connections longer, and code that calls other services while a connection is checked out holds it far longer than the query itself.
  • Only this app. Migrations, cron jobs, admin consoles and other services draw from the same limit, and PostgreSQL reserves 3 connections for superusers by default.
  • No warning band. The status is either over or within the cap, so 99% still reads as within. Watch the percentage too.
  • A floor, not a performance target. The tool finds the connections your load needs. Whether the database can run that many queries at once without contention depends on its cores, disks and queries.

How to use the result

  • Set each pool’s maximum near the per-instance number. Once a pool covers real concurrency, extra connections add lock contention and memory pressure rather than throughput, and latency can get worse.
  • Budget for the most instances you will run. Use the autoscaler’s ceiling plus the extra instances a rolling deploy starts during the changeover, not today’s count.
  • Keep slack under the cap. A total that uses most of the limit leaves no room for the other clients above, and the next one to connect is refused.
  • Over the cap? Put a pooler in front. PgBouncer or a similar proxy multiplexes many client connections onto a few real ones, so app instances can scale without the database limit scaling with them.

When to use it, and when not

Use it before launch, before raising an autoscaler’s ceiling, and whenever query latency changes: it answers “will the fleet still fit under max_connections?” before a deploy finds out. It is also the right check for a pooler’s own server-side pool, with the pooler as the single instance.

It cannot tell you how many queries the database can run at once. If the database is already CPU- or disk-bound, more connections only queue the same work inside it; shorten queries or add capacity instead. Nor does it fit short-lived serverless functions, where every concurrent invocation may open its own connection, so the instance count is not a number you set. Put a pooler in front first, then size that.

Trade-offs and limits

Too small, and requests wait at the pool for a free connection until the acquire timeout fires. Too large, and the database spends its time on contention and memory instead of queries. Headroom is the dial between the two. At the defaults, where the fleet needs 30 connections on average:

HeadroomPer instanceTotalAverage use
0%530100%: any burst queues
25%74271%
50%84863%
100%106050%

Little’s Law gives the average, and arrivals are not even, so a pool at 100% average use queues for part of every second. The model also ignores a feedback loop: when the database slows, each query holds its connection longer, so the same traffic needs more connections, which adds load and slows it further. The pool’s maximum is what stops that loop at the database’s door.

Common mistakes

  • Counting containers, not processes. A server running 4 worker processes per container has 4 pools per container. Six containers with a pool of 8 per worker is 192 connections, 96% of a 200 cap, where the tool showed 48.
  • Forgetting the deploy surge. Pool size is fixed config, so a rolling deploy that briefly runs 12 instances at 8 each opens 96 connections, twice the steady state.
  • Holding a connection across a remote call. Code that opens a transaction, calls a 200 ms API and then commits holds the connection for 215 ms, not 15. At the defaults that needs 108 per instance and 648 in total, more than three times the cap.
  • Mixing up fleet and per-instance QPS. The QPS input is the whole fleet. Entering one instance’s rate understates the need by the instance count.
  • Setting the pool to the database limit. One instance taking all 200 connections leaves nothing for the others, for migrations or for an engineer’s console in an incident.

Check yourself

3,000 QPS at 20 ms over 10 instances, 25% headroom, cap 100. What is the pool and does it fit?

Concurrency is 3,000 × 0.020 = 60. Each instance gets 60 ÷ 10 × 1.25 = 7.5, rounded up to 8, so the total is 80: 80% of the cap. It fits, but the deploy surge or one cron job would push it near the limit.

Each of 6 containers runs 4 worker processes. What should you enter as instances, and what does the defaults’ load then need?

Enter 24, the number of pools. Each gets 30 ÷ 24 × 1.5 = 1.875, rounded up to 2, for a total of 48. Entering 6 and giving every worker a pool of 8 opens 192.

Query time doubles from 15 ms to 30 ms at the same traffic. What happens to the pool?

Concurrency doubles to 60, so each instance needs 60 ÷ 6 × 1.5 = 15 and the total is 90. Latency moves the pool exactly as much as traffic does.

2,000 q/s × 15 ms = 30 busypool 8pool 8pool 8pool 8pool 8pool 8database, max 2006 × 8 = 48 open (24%)each pool = ⌈30 ÷ 6 × 1.5⌉ = 8
Every instance brings its own pool to the same database. The per-instance number comes from Little’s Law; the number the database sees is that times the instance count.

How it works

  • Concurrency = QPS × latency (Little’s Law), across the whole fleet.
  • Per-instance pool = concurrency ÷ instances × (1 + headroom), rounded up, minimum 1.
  • Total = per-instance pool × instances, checked against the DB max-connections cap.
  • Bigger is not safer: size to real concurrency, not to the DB limit.

Frequently asked questions

How do you calculate database connection pool size?

Use Little’s Law: connections in use at once equal queries per second times the time each query holds a connection, in seconds. At 2,000 queries per second and 15 ms, that is 30 busy connections across the fleet. Divide by the number of app instances, add headroom for bursts and round up: 30 ÷ 6 instances × 1.5 is 7.5, so each instance gets a pool of 8. Multiply back by the instance count, 48 here, to see what the database has to accept.

Why check the total against the database max_connections?

Every application instance has its own pool, but they all connect to the same database. Twenty instances with a pool of 50 is up to 1,000 connections, and once the limit is reached the database refuses new ones. Connections also cost the server memory: in PostgreSQL each one is a separate backend process. The default max_connections in PostgreSQL is 100, lower than many teams assume, so check the multiplied total before a deploy or an autoscaling event finds the limit for you.

Is a bigger connection pool better?

No. Once the pool covers your real concurrency, extra connections add no throughput. They add lock contention, memory use and context switching on the database, and can make latency worse. The HikariCP project’s pool-sizing guidance makes the same argument, starting from a formula of the database server’s core count times two plus its effective spindle count for total active connections. Size to measured concurrency plus modest headroom, not to the largest number the database will accept.

What latency should I enter for pool sizing?

Enter how long a connection stays checked out of the pool, not just how long the query runs on the server. That includes the network round trip and any time your code holds the connection between statements. If requests open a transaction, call another service and then commit, the hold time is the whole transaction. Use the average at peak traffic, because query latency usually rises under load, and rerun the numbers with a slower value to see how close you get to the cap.

When should I use a pooler like PgBouncer?

When the multiplied total exceeds the database limit, or when instance counts swing widely with autoscaling or serverless functions. A pooler such as PgBouncer or ProxySQL accepts many client connections and multiplexes them onto a small number of real database connections, so app instances can scale without the database limit scaling with them. Transaction pooling multiplexes best, but it breaks session-level features such as SET commands and advisory locks held across transactions, so check what your driver and code rely on first.

How does autoscaling change the pool size?

The total is the per-instance pool times the instance count, so size for the most instances you will ever run, including the extra ones a rolling deploy starts before the old ones stop. Scaling out also inflates the total through rounding: at the defaults, 30 instances each round a 1.5-connection share up to 2, giving 60 connections where 6 instances needed 48. For large fleets, small per-instance pools behind a pooler usually beat larger ones that connect directly.