Connection Pooling
Understand why a database refuses clients when too many connect, and how a pool reuses a few connections safely, with its size and its traps.
Reuse connections instead of opening new ones
Before an app can run a query it needs a connection to the database. PostgreSQL gives every connection its own backend process: the manual says every client process connects to exactly one backend process, and a supervisor called the postmaster starts a new one every time a connection is requested.
A connection pool is a small set of connections kept open by your app. A request borrows one, runs its query, and gives it back for the next request to use. If all are busy, the request waits in line.
A limit, and a cost per connection
The database accepts only so many connections at once: max_connections, typically 100 by default, and it can only be changed by restarting the server. Each connection is a process with its own memory. And opening a connection takes time before the first query can run.
A busy site can easily have more simultaneous requests than the database has connections. Without a pool, the extra ones are turned away.
Twelve requests, six connections allowed
const pool = new Pool({ max: 4 });
await pool.query("SELECT ...", [id]);In the lab, 12 requests arrive in a burst and the database allows 6 connections. With no pool, each request opens its own connection, and only 6 get in. The rest fail with sorry, too many clients already.
With a pool of 4, the same 12 requests are all served. Four connections are opened once and reused, and the other requests wait their turn, first in, first out. Nobody is refused, and the database never sees more than 4 connections.
pool.query(...) borrows a connection, runs the query and gives it back for you. For several statements that must belong together, such as a transaction, borrow one connection with pool.connect() and run every statement on it, then release it.
Burst of requests, with and without a pool
Start with No pool and read the refused requests. Then try Pool of 2, 4 and 8, and finally switch on Forget to release.
Connections over time (ticks 0 to 13)
Grey: opening the connection. Teal: the query. Rose: the connection was never released.
The 12 requests
Served 12, refused 0, timed out 0. Connections opened: 4. Average time per served request: 7.0 ticks.
The pool opens 4 connections once and reuses them. Nobody is refused. Requests that arrive when all are busy wait in line, first in, first out.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- Each PostgreSQL connection is a backend process, and max_connections (typically 100) limits how many can exist.
- A pool keeps a few connections open and reuses them. Extra requests wait in line.
- Always release a borrowed connection, in finally. A leak drains the pool.
- Add up the pools of every app instance, and keep the total below max_connections. Use one connection for a whole transaction.
Done with this concept?
Mark it complete to track your progress. No login needed.