liveBook · Manning

Connection pooling

As explained in Fast by Default

Connection pooling is the management and reuse of a collection of established connections instead of creating a new connection for each operation.

Connection pooling reuses a fixed set of established database connections instead of opening one for every query or request. Establishing a connection requires a TCP handshake and authentication, adding roughly 20–50 ms each time; at high request rates, that cost compounds. A shared pooler can serve requests from multiple application instances through common open connections. section 5.4.3

Pooling is primarily a scalability control, not a universal response-time fix. It limits connection contention as concurrency rises, while query optimization addresses slow individual queries and resilience patterns address dependency failures. For PostgreSQL, connection counts matter especially because each connection creates a dedicated operating-system process; MySQL and SQL Server use lighter-weight threads. section 5.1 section 5.4.3

Size a pool by balancing queueing against database capacity:

  • Count total possible connections across all application servers, not only the size configured on one server. section 5.4.3
  • Treat too few connections as a latency risk: requests queue when no connection is available, affecting p99 latency. section 5.4.3
  • Treat too many connections as a database-capacity risk: PostgreSQL degrades when many backend processes consume memory and contend for CPU. section 5.4.3
  • Target 50–70% normal utilization for headroom; investigate sustained utilization above 80% by evaluating database capacity or reducing query latency. section 5.4.3

Estimating shared capacity

Ten application servers with pools of 20 expose 200 potential database connections. With 10 ms average queries, each connection can theoretically serve 100 queries per second, or 20,000 queries per second across the pool. Leave headroom rather than designing around that theoretical maximum, because workload variance raises utilization and queueing. section 5.4.3

Startup behavior is part of pool design. Eager initialization establishes connections before service readiness and can fail fast when the database is unreachable. Lazy initialization starts the pool while acquiring connections in the background, so connections open on first use. Pre-warming opens a minimum number immediately and creates remaining connections lazily, trading faster startup against immediate traffic readiness. section 5.3.1

Monitor pool metrics during development and under load: active connections, idle connections, and requests waiting for a connection. Use those signals to size for expected concurrency and alert when the pool approaches exhaustion. Auditing peak connection counts and utilization links configuration to observed database stability and application latency. section 7.2.2 section 5.4.4

How Fast by Default covers it

How deep it goes
Explains pooler behavior, sizing arithmetic, utilization thresholds, startup modes, and runtime metrics without detailing pooler implementation internals.
Examples
Uses ten servers with twenty connections each and a checkout workload with eight of ten connections active.
What it assumes
Assumes familiarity with database requests and concurrent application traffic while defining connection reuse and its capacity trade-offs.
What you can do after
Operate/configure it by calculating total connections, monitoring utilization and waiters, selecting initialization behavior, and alerting near exhaustion.
How it connects
Connects connection pooling to scalability, p99 latency, database query latency, PostgreSQL, startup probes, and horizontal scaling.

Also explained in

Compare how 11 books explain connection pooling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast by Default. The section links point to the parts of the book it draws on.