Fewer than you think. So where do all the extra ones come from, and what holds them?
Most "too many connections" incidents are not a database that is too small. They are many application instances each opening a generous pool, multiplied together. This page shows the arithmetic, what each engine pays per connection, and where a pooler fits.
A connection only does work while a query runs. If queries take 5 ms and you run 2,000 a second, then on average 2,000 × 0.005 = 10 connections are busy at any moment. That is Little's law, and it is the starting point for every pool size. Add headroom for bursts and slow moments, and a pool of 20 to 40 is usually plenty for that load.
The trouble is multiplication: 60 application instances with a pool of 50 each is 3,000 possible connections, most of them idle. So I size pools from the work, cap the total per service, and put a pooler (PgBouncer, ProxySQL, RDS Proxy) between the clients and the database when there are many clients, short-lived ones such as serverless functions, or frequent deploys.
In one line. Busy connections = queries per second × seconds per query. Size pools from that, not from the server's limit.
| PostgreSQL | MySQL | MongoDB | |
|---|---|---|---|
| Model | A separate operating system process per connection | A thread per connection (a thread pool in Percona Server, MariaDB and MySQL Enterprise) | A thread per connection on mongod; drivers keep their own pools |
| Default limit | max_connections = 100 | max_connections = 151 | Set by the operating system's file and process limits; drivers default to maxPoolSize 100 per host |
| Per-connection memory | Several MB at rest, more while sorting: work_mem is per sort or hash, not per connection | Per-thread buffers (sort, join, read) on top of a small base | Around 1 MB each on the server |
| When there are thousands | Memory, process scheduling and snapshot work grow; 14 made idle connections much cheaper, but a pooler still helps | Threads compete for CPU; Threads_running is what matters | Usually fine on mongod; mongos also pools to each shard |
| Pooler | Engine | Notes |
|---|---|---|
| PgBouncer | PostgreSQL | Session mode keeps one server connection per client. Transaction mode shares server connections between transactions, which is where the savings are, and breaks anything that relies on session state: SET without LOCAL, session advisory locks, LISTEN, temporary tables. Protocol-level prepared statements work in transaction mode since 1.21 with max_prepared_statements. |
| ProxySQL | MySQL | Multiplexes client connections onto fewer server connections, routes reads to replicas, and can rewrite or block queries. It stops multiplexing for a connection that uses session state, such as user variables or temporary tables. |
| RDS Proxy | PostgreSQL, MySQL on AWS | Managed pooling that also shortens failover for clients. Session state "pins" a client to one connection, and pinning removes the benefit. |
| The driver's pool | All, and the only one for MongoDB | Every application has one. Its size times the number of instances is what the database sees. |
The numbers on this page are arithmetic from the assumptions written next to them, not measurements. Put in your own sizes and speeds, and measure with your own traffic before you trust any of them.
| Step | Assumption | Result |
|---|---|---|
| Busy connections at average load | 3,000 queries a second at 4 ms each | 12 |
| At peak, with slower queries | 6,000 a second at 8 ms each | 48 |
| Spread over instances | 24 application instances | 2 busy each at peak |
| Pool per instance | busy at peak × 2, at least 5 | 5 each, 120 in total |
| What most teams configure | the framework default of 20 to 50 per instance | 480 to 1,200, mostly idle |
HikariCP's guide offers another starting point: about twice the database's CPU cores plus the number of disks. Both point the same way: tens of busy connections, not thousands.
| What happens | Why | What I do |
|---|---|---|
| "Too many connections" after a deploy | Old and new instances overlap, each with a full pool | Cap the total per service; a pooler in front; smaller pools. |
| Pool timeouts while the database is quiet | Connections held idle in a transaction while the app calls another service | Do the outside call before or after the transaction. idle_in_transaction_session_timeout in PostgreSQL. |
| Everything gets slower as the pool grows | More concurrent queries than cores: they fight for CPU and locks | Smaller pools queue in the app, where waiting is cheap. |
| A storm of reconnects after failover | Every client retries at once | Retries with backoff and jitter; a pooler or proxy absorbs it. |
| Strange bugs in transaction mode | Session settings leak between clients, or vanish | Use SET LOCAL; avoid session-level features behind a transaction pooler. |
| Serverless functions exhaust the database | Each invocation opens a connection | A proxy in front, or a data API. |
-- PostgreSQL: who holds connections, and in what state
SELECT application_name, state, count(*)
FROM pg_stat_activity GROUP BY 1, 2 ORDER BY 3 DESC;
-- PgBouncer admin console
SHOW POOLS; -- cl_waiting > 0 means clients are queueing
SHOW STATS;
-- MySQL: connected against running
SHOW GLOBAL STATUS WHERE Variable_name IN
('Threads_connected', 'Threads_running', 'Max_used_connections', 'Aborted_connects');
-- MongoDB
db.serverStatus().connectionsperformance_schema.processlist as the non-blocking way to list sessions.A service runs 40 pods, each with a pool of 30, against a PostgreSQL primary with max_connections 500. At peak it does 4,000 queries a second averaging 6 ms. A deploy starts 40 new pods before stopping the old ones. What happens, and what would you change?
Write your answer down first. Then open mine.
What happens: 40 × 30 = 1,200 possible connections already exceed 500. During the deploy, 80 pods can ask for 2,400. New pods fail to connect, health checks fail, the deploy stalls or rolls back.
What the work needs: 4,000 × 0.006 = 24 busy connections on average. Even with headroom for bursts, under 100 in total.
What I would change: pools of 3 to 5 per pod; PgBouncer in transaction mode in front, with a server pool of about 50; and a deploy that replaces pods in batches. Then check that nothing in the code depends on session state.
The same mechanism, caches and memory, replication and failover, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. Queries take 10 ms and you run 1,000 a second. On average, how many connections are busy?
1,000 × 0.010 = 10. That is Little's law.
2. Which feature breaks behind PgBouncer in transaction mode?
Consecutive transactions may run on different server connections, so session state does not follow the client.
3. The pool times out but the database CPU is at 10%. What is the likely cause?
The connections are taken but not working. Look for sessions idle in a transaction.
4. Why can a bigger pool make things slower?
Beyond the number the server can run at once, extra concurrency only adds waiting inside the database.
How do I run it in production? What to check when it is slow or broken, where it runs and what it costs, how big it must be, pooling, backups, security, upgrades and other changes made safely.