Connection Pool
A backend resource pattern that maintains a reusable pool of pre-established database connections to eliminate per-request handshake overhead and cap DB concurrency
Table of Contents
Structure Diagram
Repeated handshakes · DB crashes
- Handshake Cost10–50ms TCP+TLS+Auth every query
- Traffic SpikeExceeds max_connections → outage
- CleanupSocket churn & TIME_WAIT buildup
Instant borrow/return · Bounded load
- Handshake CostSub-millisecond connection checkout
- Traffic SpikeProtects DB via pool cap & queue
- Serverless FitExternal proxy (PgBouncer/RDS Proxy)
What is Connection Pool?
A backend resource pattern that maintains a reusable pool of pre-established database connections to eliminate per-request handshake overhead and cap DB concurrency. It is a core concept in Databases & Caching within Web Dev.
Core Principles
Opening a fresh database connection requires a TCP handshake, TLS negotiation, authentication, and server-side memory allocation (often several megabytes per backend process in PostgreSQL). Doing this on every HTTP request wastes tens of milliseconds and rapidly exhausts database memory under load.
A Connection Pool keeps a bounded set of pre-established connections warm. Requests borrow an idle connection in microseconds, run their query, and return it to the pool instead of closing it.
Practical Takeaways
- Smaller pools often outperform huge ones: Excess active connections cause CPU context-switching and lock thrashing. Start small (often 10–20 per app instance) and always verify that
(app_instances × pool_size) < DB max_connections. - Use an external pooler for serverless: AWS Lambda or Vercel Functions can spin up hundreds of concurrent instances, blowing past DB connection limits. Place PgBouncer (transaction mode), AWS RDS Proxy, or Supavisor in front of the database.
- Configure strict timeouts: Set
acquireTimeoutandidleTimeoutso stalled queries fail fast rather than hanging every worker thread.
Notable Examples
- HikariCP (Java),
pg.Pool(Node.js),sql.DB(Go): Standard in-process connection pools. - PgBouncer & AWS RDS Proxy: Multiplexing thousands of short-lived serverless or microservice clients onto a lean set of database connections.