Connection pooling
Why opening a database connection costs more than the query it carries, and how a small set of connections kept open and shared makes that cost disappear.
In 30 seconds
Opening a connection to a database is slow: the network link, the encryption and the login all take round trips before any query can run.
A connection pool opens a few connections once and keeps them. Each request borrows one, runs its query and returns it, so the setup cost is paid only at start-up.
The pool is deliberately small. When all its connections are busy, new requests wait, and give up with an error if the wait is too long.
Connection pooling, step by step
The timings are illustrative: setup is much shorter on a local network without encryption and longer across regions. With an external pooler in transaction mode, per-session settings do not carry over.
- Step 1: Opening is the slow part. Before an application can send its first query it must open a network connection to the database, set up encryption, and log in so the database can start a session for it. Each stage is a round trip or more. Here that setup takes 50 ms, ten times as long as the 5 ms query it was opened for.
- Step 2: Keep a few open: the pool. Throwing a connection away after one query means paying the setup again for the next. A connection pool opens a small set of connections when the application starts and keeps them open. The 150 ms is paid once, at start-up, instead of 50 ms on every request.
- Step 3: Borrow, use, return. A request borrows an open connection from the pool, runs its query, and hands the connection straight back so the next request can use it. Nothing is opened or closed. The request takes the 5 ms of real work instead of 55 ms, and the same three connections serve request after request.
- Step 4: How big a pool?. One connection running 5 ms queries back to back can carry 200 a second, so a pool of three carries up to 600. Bigger is not better: every open connection costs the database memory and a worker of its own, and the database has only so many processor cores. Pools are usually kept small, often a few connections per core.
- Step 5: Waiting and timeouts. When every connection is in use, the next request waits in line until one is returned, usually a few milliseconds. The pool sets a limit on that wait, here 2 seconds, and gives up with an error rather than hang forever. Frequent timeouts mean slow queries, too small a pool, or code that borrows a connection and never returns it.
- Step 6: Many servers, many pools. A pool lives inside one application process. Run four copies of the application and there are four pools, each holding its connections open whether busy or not. Their total can exceed what the database allows, and the extra connections are refused. Adding application servers quietly multiplies the load on the database.
- Step 7: An external pooler. An external pooler is a small server placed between all the applications and the database, such as PgBouncer for PostgreSQL. Applications connect to it cheaply and in large numbers. It lends one of a few real database connections only while a transaction runs, then gives it to the next client. Twelve connections in, four out.
Try it yourself
Learn this first
Spotted a mistake on this page?