Course map

Topic 077 / Core

Connection pooling: pool sizing, pool saturation, connection limits

Chapter 5.6 · Operational Storage and the Data Lifecycle

What and why

Opening a database connection costs a TCP handshake, authentication, and session setup — expensive to pay on every request — and the database enforces a hard maximum number of open connections. A connection pool keeps a fixed set of warm connections open and hands them out to requests, instead of opening and closing one per request. Builds on: → 58 (database fundamentals).

Diagram

app instance 1 ─┐                               database
app instance 2 ─┼─► pool (size K) ──checkout──► max_connections = M
app instance N ─┘      ▲   │
                    checkin (return when done)

How it works

  1. The pool opens up to K connections once and reuses them.
  2. A request checks out a connection, uses it, and checks it back in when done.
  3. Too small a pool and requests queue for a connection even though the database has spare capacity — added latency with nothing visibly wrong downstream.
  4. Too large a pool, multiplied across many app instances, can saturate the database itself (context-switching among many active backends) or exceed its hard connection limit.
  5. Size pools from expected concurrency and per-query time (→ 101, Little's Law), not from guesswork, checked against the database's actual connection limit across all instances combined.

Practical example

(numbers illustrative). By Little's Law, concurrency ≈ arrival rate × service time: at 200 requests/sec and 20ms average query time, expected in-flight connections ≈ 200 × 0.02 = 4. A pool of 10 per instance across 20 instances is 200 connections total — which can still exceed a database's connection limit even though each individual pool "looks" modestly sized.

When to use it

  • Use when: always, for any service talking to a relational database.
  • Avoid when: never skip pooling — but avoid one uncapped pool per request-thread with no shared limit, which is how the database's connection limit gets exhausted under load.
  • Failure scenarios: many app instances, each with its own uncapped pool, collectively exceeding the database's maximum connections, so brand-new connections are refused platform-wide (→ 103, pool saturation as a hidden bottleneck); a slow query holding a pooled connection and starving the pool for unrelated fast queries.
  • Simpler alternative: for one low-traffic instance, a small fixed pool (size 5–10, say) with no further tuning is often enough.

Practice

(numeric, 15–20 min, lab Postgres). Configure an application-side pool of size 5 against the lab database. Run a workload with more than 5 concurrent clients at once and measure the queueing latency as requests wait for a free connection. Then, using the measured throughput and per-query time, compute the pool size Little's Law would recommend, and confirm raising the pool to that size (within the database's connection limit) removes the queueing. Done when the measured pool size matches the Little's Law estimate and the queueing disappears.

Shared lab setup

Key takeaway

Size pools from measured concurrency and query time, checked against the database's real connection limit across every instance — not from a guessed round number. Done when you can: compute a pool size from Little's Law and explain pool saturation as a hidden bottleneck. Used next in → 103 (bottleneck analysis); Little's Law → 101.

References

None — this topic makes no external claims.

Source directory

Search the guide