
Database Connection Pooling: Why Your App Crashes Under Load
Database Connection Pooling: Why Your App Crashes Under Load 100 concurrent users. 100 database connections. PostgreSQL default max_connections is 100. Connection 101 gets rejected. Your app returns 500. The Problem Opening a database connection takes 20-50ms (TCP handshake, auth, SSL). Without pooling, every request opens a new connection. Under load, you exhaust the DB connection limit. Connection Pool Basics A pool maintains a set of open connections. Requests borrow a connection, use it, and return it. import { Pool } from " pg " ; const pool = new Pool ({ max : 20 , idleTimeoutMillis : 30000 , connectionTimeoutMillis : 5000 }); Sizing Your Pool Formula: pool_size = (core_count * 2) + disk_spindles. For SSD with 4 cores: (4 * 2) + 1 = 9. Start with 10-20 and benchmark. More connections does NOT mean better performance. Beyond the optimal point, context switching overhead degrades throughput. Connection Leaks The #1 pool killer. Borrow a connection and never return it: // BAD - conn
Continue reading on Dev.to
Opens in a new tab


