Database Performance: Connection Pooling & Read/Write Splitting
Master HikariCP connection pool sizing, connection leak detection, read/write replica routing, and query optimization in high-throughput microservices.
The database is almost always the ultimate throughput bottleneck in a microservice ecosystem. While compute instances can be multiplied horizontally in seconds, relational databases face strict concurrency constraints dictated by disk I/O, lock contention, and connection pool capacity.
01The HikariCP Connection Pool: Sizing Formula
Spring Boot 3 uses HikariCP as its default high-performance JDBC connection pool. A common rookie mistake is setting maximum-pool-size=100 under the assumption that more database connections equal higher throughput.
In reality, having too many open connections causes database CPU thrashing due to OS context switching and disk spindle contention.
The PostgreSQL / HikariCP Sizing Formula
connections = ((core_count * 2) + effective_spindle_count)For a 4-core database server with SSD storage:
- Core count = 4
- Effective spindle count = 1
- Optimal pool size = (4 * 2) + 1 = 9 to 10 connections.
A pool of 10 connections can easily serve 5,000 requests per second if query execution times are kept under 2 milliseconds.
Production HikariCP Configuration
# application.properties
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.minimum-idle=15
spring.datasource.hikari.connection-timeout=3000
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.leak-detection-threshold=2000minimum-idle = maximum-pool-size: Fixes pool size to prevent latency spikes caused by dynamically establishing TLS handshakes to the database during traffic bursts.connection-timeout = 3000: Fail fast after 3 seconds if all connections are busy. Do not let HTTP threads block indefinitely.leak-detection-threshold = 2000: Logs a stack trace if a thread holds a connection without closing it for longer than 2 seconds.
Loading the rest of the lesson…