Database connection pooling explained: PgBouncer, managed poolers and serverless
Vinnie on why database connections are expensive (PostgreSQL runs a process per connection, and max_connections is typically 100), how a pool lets many clients share a few connections, PgBouncer's session, transaction and statement modes, app-side pools, why serverless functions are the classic case, and the managed poolers on AWS, Azure and Google Cloud.
Diagrams in this video · 1Free in archhy
The diagrams, full size
Copy any of them into archhy, free, and change it for your own system.
After this lesson you can
- Explain why opening a database connection is expensive
- Describe how a connection pool shares a few connections among many clients
- Choose between PgBouncer's session, transaction and statement modes
- Know when to use a managed pooler, especially with serverless functions, and what each cloud calls it
Transcript
Database connection pooling explained: PgBouncer, managed poolers and serverless
Vinnie here. Rung three. Connection pooling. Your queries are fast now. So why does the database still fall over when traffic spikes? Let's sniff it out.
Why connections are expensive
Opening a database connection isn't free. There's the network connection, a handshake, and signing in, before a single query runs. And PostgreSQL gives every connection its own backend process on the server. A thousand connections means a thousand processes. That's why it has a limit, max connections. The default is typically a hundred. MySQL is similar, with a thread for each connection, and its own max connections. Go over the limit, and new connections are simply refused.
How a connection pool works
Here's the classic case. Serverless functions. One. A traffic spike, and five hundred copies of your function start. Each one wants its own connection. Two. Without a pool, that's five hundred connection attempts, against a limit of a hundred. Most are refused. Three. A pool sits in between. A client borrows a connection, runs its work, and hands it straight back. Five hundred clients share twenty connections. Four. The database sees a few busy connections, instead of a crowd of idle ones.
PgBouncer in transaction mode
The best-known pooler for PostgreSQL is PgBouncer. Your app connects to PgBouncer, and PgBouncer connects to the database. Pool mode decides when a connection goes back. Session mode, the default, holds it until the client disconnects. Transaction mode, only until the transaction ends. Statement mode, after every statement, so no multi-statement transactions. Here, up to a thousand clients can connect, and they share twenty server connections for each database and user. Transaction mode is the usual choice: it shares connections far better than session mode. But the next transaction may run on a different connection. So session features, like set, listen and session advisory locks, don't work. Check your app before you switch.
The other pools
PgBouncer isn't the only pool. There are app-side pools, inside your code. SQL Server's dot net driver pools by default, up to a hundred connections. MongoDB's drivers keep a pool of up to a hundred too. But an app-side pool lives inside one process. Five hundred functions, each with its own small pool, is still a crowd. That's why serverless usually needs a pooler outside the app. And you don't always have to run that pooler yourself. Every big cloud will run one for you, at least for PostgreSQL.
In the cloud
Here's the same idea on the three big clouds. On AWS, it's Amazon RDS Proxy. Relational Database Service Proxy. For RDS and Aurora, and it keeps app connections open during a failover. But when it sees session state, like a set command, it pins that client to one connection, which cuts the sharing. On Azure, PostgreSQL flexible server has PgBouncer built in. Just switch it on. Not on the Burstable tier, though. And for MySQL on Azure, there's no managed pooler: you run your own, like ProxySQL. On Google Cloud, Cloud SQL for PostgreSQL and AlloyDB have managed connection pooling. On Cloud SQL, you need the Enterprise Plus edition. On Cloud SQL for MySQL, the feature is thread pooling, so every client still counts against max connections. And careful: the Cloud SQL Auth Proxy is not a pooler. It only secures the connection. Different names, same idea. Many clients in, a few connections out.
The trade-off
A pooler outside the app is one more hop on every query. And unless your cloud runs it, one more thing to run. Transaction mode can break apps that rely on session state. And a managed pooler may pin a client to one connection when it sees session state, which cuts the sharing. Long transactions hold a connection the whole time, so keep them short. But if you have many short-lived clients, a pool is almost always worth it. My nose says yes.
Remember
Let's recap. Every connection costs the database memory and setup time, so it caps how many it accepts. A pool lets many clients share a few connections: your driver's own pool, and PgBouncer or your cloud's managed pooler in front of the database. And for serverless, put the pooler outside the app.
The full article, and what's next
Next time, rung four. Caching. Keep the popular answers in memory, so the database isn't asked every time. See you there.
Read the article
PostgreSQL: How Connections Are Established: the whole idea in writing, free.
Read it ↗




