How Connection Pooling Actually Works (and How You Might Be DDoSing Your Own Database)
Connection pooling is not magic, and it’s necessary, it exists for a very subtle but crucial reason.
What a DB connection pool really is
- It’s a cache of open, reusable connections.
- Instead of opening/closing on every query, they’re reused.
- In .NET ecosystem, EF Core doesn’t manage it, ADO.NET does.
- When your
DbContextis disposed of, the connection isn’t closed, it’s returned to the pool.
What happens when 1000 requests hit, but you only have 5 connections?
- The first 5 get a DB connection.
- The next 995? They wait (or time out).
- The pool can grow until it hits
MaxPoolSize, then everyone queues.
Now for the fun part: horizontal scaling gone wrong
Let’s say:
- Your DB allows 1000 connections.
- Each app instance has a pool of 100.
- You scale to 20 application instances.
Now you’ve got 2000 potential connections trying to connect to the DB server, when the server has a limit of 1000 connections. Only half of them get in, the rest are met with errors. Then your retry logic joins the party and you’ve DDoSed yourself.
flowchart LR
subgraph apps["20 app instances"]
i1["Instance 1, pool: 100"]
i2["Instance 2, pool: 100"]
more["…"]
i20["Instance 20, pool: 100"]
end
db[("Database, max_connections: 1000")]
i1 --> db
i2 --> db
more --> db
i20 --> dbWhy this happens
Each pool lives in process. So when you scale horizontally, you multiply pools and total active connections. The DB doesn’t care how many instances you have; it just sees 2000 open sockets asking to connect.
How to not set your DB on fire
- Tune pools smartly:
instance_count * pool_size_per_instance <= DB max_connections - Use a connection proxy to multiplex and stabilize load.
- Cache aggressively, minimize DB calls when you can.
- Don’t fill up pools to maximum capacity on startup, increase connections gradually.
- Monitor active connections, and set DB usage alerts.
In .NET, the pool size lives in the connection string, and most people never touch it. With Npgsql, Maximum Pool Size defaults to 100 per process. That default is exactly how the math above goes wrong: nobody sets it, so every instance gets 100. Do the division yourself and leave some room for migrations, admin tools and background jobs.
// 20 instances × 40 = 800 connections, out of 1000 on the databasevar connectionString = "Host=db;Database=app;Username=app;Password=...;" + "Maximum Pool Size=40;Minimum Pool Size=0;Timeout=15";When the pool runs dry, requests don’t fail right away. They wait in line for Timeout seconds, and then Npgsql throws this:
The connection pool has been exhausted, either raise 'Max Pool Size' (currently 40)or 'Timeout' (currently 15 seconds) in your connection string.Raising either one is usually the wrong fix, it just moves the queue to the database. Ask the database what’s actually going on first:
SHOW max_connections;
SELECT state, count(*)FROM pg_stat_activityGROUP BY state;The hidden cost: cloud bills
Every database connection uses memory, CPU, and process overhead even when it’s idle. In managed environments like RDS, Azure SQL or Cloud SQL, that overhead isn’t free, quite literally you pay for it.
Efficient pooling and connection multiplexing don’t just improve performance; they also keep your cloud bill from quietly inflating in the background.