What Is Connection Pooling? Database Performance Guide
Creating a new database connection for every query? Getting "too many connections" errors? Connection pooling reuses connections and can improve performance 10x.
The Problem
Creating a database connection is expensive: TCP handshake, SSL, auth — ~10ms overhead when the query itself takes ~1ms.
How Pooling Works
App start: [conn1] [conn2] [conn3] (ready)
Request 1: borrow conn1 → query → return conn1
Request 2: borrow conn2 → query → return conn2
Node.js + PostgreSQL
const pool = new Pool({ min: 5, max: 20 });
const client = await pool.connect();
try {
const result = await client.query('SELECT * FROM users WHERE id = $1', [id]);
} finally {
client.release(); // Return to pool, don't close!
}
Pool Sizing
Optimal size = (CPU cores × 2) + disk count
Example (4 cores, 1 SSD): 9 connections
PgBouncer
External pool proxy between app and database. Reduces 60 app connections to 20 DB connections.
Tools
| Tool | Language | Highlights | |------|----------|-----------| | HikariCP | Java | Fastest Java pool | | PgBouncer | PostgreSQL | External proxy | | ProxySQL | MySQL | Load balancer + pool | | Prisma | Node.js | Integrated pool |
Best Practices
- Don't oversize the pool | 2. Always release connections in
finally - Health checks | 4. Detect connection leaks | 5. Monitor active/idle/waiting counts
Conclusion
Connection pooling is the lowest-cost database optimization. Proper pool sizing reduces request latency, decreases database load, and prevents connection exhaustion.
Learn database optimization on LabLudus.