What is connection pooling in Node.js database access?
Why Interviewers Ask This
Mid-level Node.js roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.
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.
Pro Tip
This topic has Node.js-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
Previous
What is Mongoose and how does it relate to MongoDB?
Next
What is rate limiting and how do you implement it in Node.js?