Connection pool
Also called: pool, database pool, pool size.
The app opens a few database connections once and keeps them in a pool. Each request borrows one, runs its queries and gives it back, so many requests share a few connections; when all are in use, new requests wait in line. A request that never gives its connection back leaks it, and when every connection has leaked, new requests time out.
The app keeps a pool of 4 Postgres connections. Send 8 requests, then leak a few connections and send again.
Served: 0 · Waited: 0 · Failed: 0 · Free: 4 of 4
The pool has 4 open connections, all free.
Say it in a prompt
Use a node-postgres Pool with max 10 connections per server and connectionTimeoutMillis 5000. In every handler, get a client with pool.connect(), run the queries in try, and call client.release() in finally so a connection is returned even on errors. Log the pool's totalCount, idleCount and waitingCount every 30 seconds. Vague vs precise prompt
Vague prompt
the app gets slow and then stops answering under load, fix the database Typical resultOpens a new database connection for every request. Under load Postgres hits max_connections and starts refusing them, and the bug that never returns connections is still there.
Precise prompt
Use a pg Pool with max 10 per server and a 5 s connectionTimeoutMillis. Borrow with pool.connect(), always client.release() in finally, and log totalCount, idleCount and waitingCount every 30 s. Typical resultEach server keeps at most 10 connections open. Extra requests wait a moment instead of overloading Postgres, connections are never leaked, and the logs show when the pool is too small.
Seen on
- node-postgres: Its pooling guide says you must always release a client back to the pool, even after an error; otherwise the pool leaks clients until it is empty and new requests wait forever.
- HikariCP wiki: About Pool Sizing argues for a small pool, with the app's threads waiting in line for a connection, instead of hundreds of connections fighting over the database.
You might describe it as
- reuse a few database connections for many requests
- requests wait in line for a free database connection
- the app hangs with timed out waiting for a connection
Not to be confused with
- Backpressure
A connection pool caps how many requests use the database at the same time; backpressure is the slow side telling the fast side to slow down.