Skip to main content
PreparationZone Logo
PrepZone

Connection Pooling

HikariCP sizing, pool exhaustion, and why opening a connection per request kills throughput.

Pass checkpoint below

Read these first

Why this matters

  • Opening a Postgres connection takes 20–50ms; at 1000 RPS that overhead alone exceeds query time.
  • Pool exhaustion — every connection busy, requests queue — is a classic production outage pattern.
  • HikariCP is Spring Boot's default pool; misconfigured maximumPoolSize starves or overwhelms Postgres.
  • Interview system-design questions pair pool sizing with thread pool sizing and database max_connections.
Clustered (PK)
Table rows (sorted by PK)
Row 1id=1
Row 2id=2
Row 3id=3
Non-clustered (email)
email index→ row pointer
Table heapunordered rows
A clustered index sorts table rows by key. Non-clustered indexes point to row locations.

Why connections are expensive

Each Postgres connection spawns a backend process consuming ~5–10 MB RAM. The handshake involves TCP, TLS, authentication, and session setup:

Java
// Anti-pattern: new connection per request
Connection conn = DriverManager.getConnection(url, user, pass);
// ... query ...
conn.close();

VaultCommerce's early prototype did this under load — Postgres hit max_connections (100) within minutes of a marketing email blast.

How pooling works

The pool maintains warm connections. Application threads borrow, execute SQL, and return:

Pool lifecycle

  1. Startup — Pool opens minimumIdle connections.
  2. Borrow — Thread requests connection; pool assigns idle one or waits (up to timeout).
  3. Use — Thread runs queries on the borrowed connection.
  4. Return — Connection goes back to idle queue, not closed.
  5. Eviction — Stale or broken connections are replaced.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • HikariCP configuration
  • Sizing formula
  • Pool exhaustion symptoms

Test yourself

Answer these before moving on — recall is what makes it stick.