🟢 Node.js Intermediate

What is connection pooling in Node.js database access?

Answer

Connection pooling is the practice of maintaining a cache of database connections that can be reused for multiple requests, rather than creating and destroying a new connection for every operation. Creating a new database connection involves TCP handshaking, authentication, and protocol negotiation — typically 20-100ms of overhead. With pooling, connections are established once at startup and reused, keeping latency low. Most database drivers for Node.js implement pooling automatically: pg (PostgreSQL) uses new Pool({ max: 20 }); Mongoose manages a connection pool internally (poolSize option); MySQL2 uses mysql.createPool(). Key pool settings: max (maximum connections — set based on DB server capacity and expected concurrency), min (minimum idle connections), acquireTimeout (wait time for a connection before error), and idleTimeoutMillis (close connections idle longer than this). Misconfiguring pool size can cause either wasted connections or request queuing under load.