Reuse a small set of database connections instead of opening a new one every time.
Connection pooling means your app keeps a small set of open database connections and lends them out to requests, instead of opening a brand new connection every time.
A database connection is a live line between your app and the database. Before you can send a query, the line must be built. This takes several steps: open a network link (TCP), often set up encryption (TLS), log in with a user and password, and let the database get ready for you.
On the database side a connection is also not free. PostgreSQL, for example, starts a separate process for every connection, and each one uses memory. MySQL uses a thread per connection. Many databases also have a hard limit (PostgreSQL commonly defaults to 100) on how many connections can exist at once.
A connection pool solves both problems. It opens some connections ahead of time and keeps them alive. A request borrows one, runs its queries, and returns it. The next request reuses the same connection.
| Mode | A real connection is held for | Good and bad |
|---|---|---|
| Session pooling | The whole time a client stays connected | Safest and simple. Saves the least, as idle clients hold connections. |
| Transaction pooling | Only while a transaction runs | Very efficient. Session features (temporary tables, some prepared statements, session settings) can break. |
| Statement pooling | One single statement | Most aggressive. Multi-statement transactions are not allowed. |
| Setting | What it means | Why it matters |
|---|---|---|
| Maximum size | The most connections the pool will open | Too big hurts the database. Too small makes requests wait. |
| Minimum idle | Connections kept open even when quiet | Avoids a slow first request after a quiet period. |
| Acquire / connection timeout | How long a request waits for a free connection | Turns an endless wait into a clear error. |
| Idle timeout | Close connections that were unused for a while | Frees resources, but a pool may shrink and need to regrow. |
| Max lifetime | Retire a connection after some time and open a new one | Avoids stale connections and works with database or firewall limits. |
| Validation / keep-alive | Check that a connection still works before use | Stops errors from connections that were silently cut. |
Requests borrow one of a fixed number of open connections. If all are busy they wait in a queue, and give up after 2 seconds.
A simplified model with a simulated clock (no random numbers). The database has 8 cores: when more queries run than cores, each one slows down in proportion. A rough capacity for this setup is 160 requests per second. Real numbers depend on your hardware and queries.
| Term | Meaning in easy words |
|---|---|
| Connection | A live link between the app and the database, used to send queries |
| Handshake | The back-and-forth steps (TCP, TLS, login) needed to set up a connection |
| Connection pool | A set of ready connections that requests borrow and return |
| Borrow / acquire | Take a connection from the pool to use it |
| Release / return | Give the connection back so someone else can use it |
| Pool exhaustion | Every connection is busy, so new requests have to wait or fail |
| Connection leak | A connection that is borrowed but never returned |
| max_connections | A database setting for the most connections it accepts (PostgreSQL commonly defaults to 100) |
| Pooler / proxy | A separate service (PgBouncer, ProxySQL, RDS Proxy) that pools for many apps |
| Idle connection | An open connection that nobody is using right now |
| Keep-alive | A small ping that stops firewalls or servers from closing a quiet connection |
| Choice | We gain | We lose |
|---|---|---|
| Bigger pool | More queries can run at once | More memory and contention on the database, slower queries past a point |
| Smaller pool | A calm, predictable database | Requests may wait in line during a burst |
| Long wait timeout | Fewer errors during short spikes | Users and threads wait longer, and the backlog can grow |
| Short wait timeout | Fail fast, quick feedback | More errors when load is only briefly high |
| Pool in each app | Simple, no extra service | Total connections grow with the number of instances |
| Shared pooler | Few real connections for many clients | Extra hop and one more component to keep alive |