What is database connection pooling?

Answer

Connection pooling maintains a cache of reusable database connections rather than creating a new connection for every query. Creating a MySQL connection involves: TCP handshake, authentication, session initialization — typically 10-100ms overhead. With pooling, connections are established at startup and reused. Most application frameworks and database libraries implement pooling automatically. Configuration considerations: pool size — too small: requests queue up waiting for connections, increasing latency; too large: overwhelming the MySQL server (MySQL has a max_connections limit, default 151). Optimal pool size is rarely more than 10-20 connections per application instance — MySQL can handle ~200 concurrent queries efficiently. Formula: pool_size ≈ (core_count * 2) + effective_spindle_count. Monitoring: watch for "Too many connections" errors (hit max_connections), slow query time (pool exhaustion), and connection wait time. MySQL-specific considerations: wait_timeout (how long MySQL keeps an idle connection — default 28800s = 8h); your pool must handle connection drops due to timeout with reconnect logic. Connection pools also reduce overhead on MySQL from frequent TCP/auth setup.