positive: "Use when user configures database connection pools, asks about pool sizing, PgBouncer, HikariCP, SQLAlchemy pool, connection limits, pool exhaustion, or connection lifecycle management."
Opening a database connection involves TCP handshake, TLS negotiation, authentication, and backend process creation (PostgreSQL forks a new process per connection). This costs 50–150ms per connection. Pooling amortizes this cost by reusing a fixed set of pre-established connections.
Connection lifecycle without pooling:
With pooling:
Key benefits: lower latency, bounded resource usage, protection against connection storms.
From HikariCP wiki (Brett Wooldridge):
connections = (CPU cores × 2) + effective_spindle_count
(4 × 2) + 1 = 9 connections.This formula applies to the total connections hitting the database, not per-application-instance.
For workload-driven sizing:
L = λ × W
Pool Size = requests_per_second × avg_query_duration_seconds
Example: 500 req/s × 0.020s avg query = 10 connections. Add 10–20% headroom for spikes.
| Parameter | Purpose | Typical Default | Guidance |
|---|---|---|---|
minIdle / min_size | Warm connections kept ready | 0–5 | Match steady-state load |
maxSize / max_pool_size | Hard cap on connections | 10 | Size per formula above |
idleTimeout | Close idle connections after | 10 min | Keep below DB wait_timeout |
maxLifetime | Recycle connections before | 30 min | Set below DB/firewall timeout, stagger across pool |
connectionTimeout | Max wait for a connection | 30s | Fail fast: 5–10s in production |
validationQuery / test_on_borrow | Check connection health | SELECT 1 | Use driver-level validation when available |
leakDetection | Log stack trace for unreturned connections | disabled | Enable in dev/staging |
Spring Boot application.yml: