Tuning a database connection pool
connection-pool sizing intuition.
max size, min idle, connection and max-lifetime timeouts; size from cores and latency, not guesswork.
WHAT THIS TESTS It checks whether you treat the pool as a concurrency limiter governed by the database's capacity, not as a free buffer you maximize.
A GOOD ANSWER COVERS The core parameters. Max pool size caps concurrent connections. Min idle keeps a warm baseline so bursts do not pay connection-setup latency. Connection timeout bounds how long a thread waits to borrow a connection before failing fast instead of piling up. Idle timeout reaps unused connections, and max lifetime recycles connections to avoid stale or leaked server-side state. Approach sizing empirically and analytically: the database has a finite number of useful concurrent connections, often near its CPU and disk parallelism, so a small pool plus queuing frequently beats a giant pool. Use Little's Law, pool size roughly equals throughput times average service time, and load-test to find the knee.
RISKS OF MIS-SIZING Too small: threads block waiting to acquire connections, latency spikes, and timeouts fire under load. Too large: more connections than the database can usefully serve cause context switching, lock and latch contention, memory growth per connection, and degraded throughput; you can also exhaust the server's max_connections and starve other services.
LIKELY FOLLOW-UPS Why a smaller pool sometimes raises throughput, how max lifetime interacts with load balancers, what acquisition timeout failures indicate, and how to monitor pool saturation.
ONE CONCRETE EXAMPLE A service set max pool to 200 against an 8-core Postgres and saw rising latency. Dropping to about 20 connections with a short acquisition timeout and warm min idle cut p99 latency because the database stopped thrashing and requests queued briefly in the app instead.
Read the original → dev.to
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.