Connection Pool
In one line
A connection pool maintains a set of open database connections that are reused across requests, avoiding the cost of opening new connections.
In simple words
A connection pool is a set of pre-opened database connections that the application reuses. Instead of opening a new connection for every request (which is slow and resource-intensive), the application borrows a connection from the pool, uses it, and returns it.
Opening a database connection involves TCP handshake, authentication, and session setup — this can take 10-100ms. With a pool, the first request pays this cost; subsequent requests reuse the warm connection in microseconds.
The pool has a configurable size (e.g., 10 connections). If all connections are in use, new requests either wait or fail. Pool sizing is a key performance tuning decision — too few connections cause waiting, too many can overwhelm the database.
Example
// Using pg Pool in Node.js
const { Pool } = require("pg");
const pool = new Pool({
connectionString: "postgres://localhost/mydb",
max: 20, // max 20 connections
idleTimeoutMillis: 30000,
});
// Borrow a connection, run a query, return it
const res = await pool.query("SELECT * FROM users WHERE id = $1", [1]);The Pool manages connections. Each pool.query borrows, uses, and returns a connection automatically.
Related terms
Related Resources
Related Lessons
- Connection Pooling
Learn how connection pools work.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.