Connection Pool
ELI5 — The Vibe Check
A connection pool pre-opens a bunch of database connections and reuses them instead of opening a new one for every request. Opening a connection takes time — like 50-100ms. The pool keeps them warm and ready. It's like having a fleet of taxis waiting instead of calling one every time.
Real Talk
A connection pool maintains a cache of reusable database connections to minimize the overhead of creating and destroying connections. Pool configurations include minimum/maximum connections, idle timeout, and connection lifetime. The pool manager handles connection checkout, return, validation, and eviction. Critical for performance in high-concurrency applications. Implemented by pg-pool, HikariCP, SQLAlchemy pool, and PgBouncer.
Show Me The Code
const pool = new Pool({
host: 'localhost',
database: 'myapp',
max: 20, // Max connections
min: 5, // Min idle connections
idleTimeoutMillis: 30000
});
const result = await pool.query('SELECT * FROM users WHERE id = $1', [1]);
When You'll Hear This
"Set the pool max to 20 connections — our Postgres can handle 100 total." / "The API was slow because every request opened a new database connection instead of using the pool."
Related Terms
Concurrency
Concurrency is juggling multiple tasks at once — not necessarily at the exact same instant, but switching between them fast enough that they all seem to be...
Database
A database is like a super-organized filing cabinet for your app's data.
Latency
Latency is the delay before data starts moving — the time it takes for a request to go from your device to the server and back.
PgBouncer
PgBouncer is a lightweight connection pooler that sits in front of PostgreSQL and recycles database connections like a good environmentalist.