Appearance
Connection Pooling
A technique where a set of pre-established database connections are reused rather than creating a new connection every time a client makes a request.
The Problem It Solves
Creating a new database connection is expensive — it involves network handshake, authentication, memory allocation, and session setup. If every request creates and destroys a connection, it leads to high latency and resource wastage under heavy load.
How It Works
Clients Connection Pool Database
| | |
|---Request----------->| |
| |---Use Connection---->|
| |<--Result--------------|
|<--Response-----------| |
| (connection returned to pool, not closed)
|---Request----------->| |
| (reuses same connection)Key Parameters
| Parameter | Description |
|---|---|
| Minimum Pool Size | Connections kept open even when idle |
| Maximum Pool Size | Max connections allowed in the pool |
| Connection Timeout | How long a client waits for a free connection |
| Idle Timeout | How long an unused connection stays in the pool |
| Max Lifetime | Maximum age of a connection before it's recycled |
Pool Size Rule of Thumb (HikariCP)
Pool Size = (Core Count × 2) + Effective Disk SpindlesImportant: Bigger pool ≠ Better performance. Too many connections can overwhelm the database.
Connection Pool States
- Idle → Available, waiting to be borrowed
- Active → Currently in use by a client
- Closed → Removed from pool (timed out or errored)
Benefits vs Drawbacks
| Benefits | Drawbacks |
|---|---|
| Reduced connection overhead | Consumes memory even when idle |
| Faster response times | Misconfigured pool size can cause issues |
| Better resource management | Stale connections can cause errors |
| Handles traffic spikes better | Adds complexity to the system |
Popular Connection Pool Libraries
| Language | Library |
|---|---|
| Java | HikariCP, c3p0, DBCP |
| Python | SQLAlchemy Pool, psycopg2 pool |
| Node.js | pg-pool, mysql2 pool |
| .NET | Built-in ADO.NET pooling |