Skip to main content
PreparationZone Logo
PrepZone

Connection Pooling in Production

HikariCP settings, pool sizing math and monitoring active connections.

Pass checkpoint below

Read these first

Why this matters

  • Connection pool exhaustion is a top production failure mode — every request waiting for a connection times out simultaneously.
  • Pool sizing directly affects throughput and database load — too few connections starve requests, too many overwhelm the database.
  • HikariCP is the fastest pool available for Java; Boot configures it automatically when a DataSource starter is present.

Connection pool role

L1 · Fresher

HikariCP reuses JDBC connections — creating a connection per request would exhaust database resources.

Key settings

L2 · Intermediate

maximum-pool-size, connection-timeout, and idle-timeout live under spring.datasource.hikari.*.

Pool sizing math

L3 · Advanced

Rough guide: pool size ≈ threads that hit the DB concurrently — not equal to total Tomcat threads.

Leak detection

L4 · Expert

Enable leak detection thresholds in staging; log long-held connections tied to missing @Transactional boundaries.

Cache-aside with ElastiCache

1. GET2. miss3. SET+TTLCOMPUTE
EKS API podstreamhub-api
DATABASE
RDS Postgressource of truth
DATABASE
ElastiCachesub-ms reads
App checks Redis first; on miss loads RDS and populates cache with TTL.

Default configuration

Boot auto-configures HikariCP with sensible defaults:

Java
spring:
  datasource:
    url: jdbc:postgresql://127.0.0.1:5432/bookstore
    username: bookstore
    password: ${DB_PASSWORD}
    hikari:
      maximum-pool-size: 10
      minimum-idle: 5
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000
      pool-name: BookStorePool

No extra dependency needed — spring-boot-starter-data-jpa includes HikariCP.

Key HikariCP settings

  • maximum-pool-size — Maximum concurrent connections. Default 10.
  • minimum-idle — Connections kept warm when idle. Default equals max pool size.
  • connection-timeout — Milliseconds to wait for a connection before throwing exception. Default 30s.
  • idle-timeout — How long an idle connection stays in the pool. Default 10 minutes.
  • max-lifetime — Maximum age of a connection before retirement. Default 30 minutes.

Pool sizing formula

A widely used guideline:

Java
connections = (core_count * 2) + effective_spindle_count

For a BookStore API on a 4-core server with SSD:

Java
connections = (4 * 2) + 1 = 9

Set maximum-pool-size: 10. Adjust based on monitoring, not theory.

Critical rule: Total connections across all app instances must not exceed the database's max_connections. With 3 BookStore pods at 10 connections each, you need at least 30 available on PostgreSQL.

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 · 5 sections

  • Monitoring pool health
  • Connection leak detection
  • Tuning for BookStore workloads
  • Read replicas
  • Validating connections

Test yourself

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