跪拜 Guibai
← All articles
Database

HikariCP Connection Pool Tuning That Won't Crash Your Database

By 倔强的石头_ ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Connection pool misconfiguration is the most common self-inflicted outage in database-backed services. Over-provisioning connections across multiple instances silently exhausts the database's session limit, while under-provisioning causes thread starvation that looks like a slow database but is actually a queueing problem in the app layer.

Summary

Default HikariCP settings get a Spring Boot app running but rarely survive real traffic. The pool size must be derived from the database's max_connections, the number of app instances, and the ratio of SQL time to total request time — not from a guess. A simple formula (target concurrency × DB time / total request time) produces a starting number that stress testing can validate.

Connection timeouts should force fast failure under congestion rather than letting threads hang; idle and max-lifetime values need to stay below any infrastructure-level disconnect thresholds. Enabling leak-detection-threshold in dev and test catches unclosed connections before they reach production, surfacing the exact call stack.

The pool doesn't operate in isolation. A Tomcat thread pool of 100 with only 10 Hikari connections starves 90 threads; 100 connections against a database capped at 100 leaves no headroom for other consumers. The article provides conservative starter configs for Windows dev (max 5 connections, leak detection at 10s), test (max 20), and a checklist for production tuning that begins with database session counts and ends with P95 latency.

Takeaways
Query the database's max_connections and current session count before setting any pool size; a single app should claim only a fraction of the total.
Estimate per-instance pool size as (target concurrent requests × DB time per request / total request time) rather than picking an arbitrary number.
Set connection-timeout to 3–5 seconds so congested requests fail fast instead of occupying threads for 30 seconds.
Keep max-lifetime shorter than any network or database idle-disconnect threshold; 30 minutes is a common starting point.
Enable leak-detection-threshold at 10–30 seconds in dev and test to surface unclosed connections with stack traces.
Add ApplicationName to the JDBC URL so database-side session queries clearly identify which app owns each connection.
Match the Web container's thread pool to the connection pool: a 100-thread Tomcat with a 10-connection Hikari pool guarantees 90 threads will queue.
Recalculate the total connection budget every time you scale out instances — 10 instances × 30 connections = 300 sessions against the database.
Conclusions

The estimation formula (concurrency × DB time / total time) is a useful heuristic, but it assumes uniform request profiles; real workloads with mixed fast and slow queries will need headroom beyond the calculated number.

Fast-fail connection timeouts are a deliberate architectural choice that trades user-facing errors for system stability — it requires corresponding alerting and retry logic upstream, otherwise it just shifts the failure to the caller.

The advice to set max-lifetime below infrastructure disconnect thresholds is sound but often overlooked: cloud load balancers, firewalls, and managed database proxies all have their own idle-timeout defaults that can silently kill connections the pool still considers valid.

Concepts & terms
HikariCP
A high-performance JDBC connection pool and the default in Spring Boot. It manages a set of reusable database connections to avoid the overhead of establishing a new connection per request.
connection leak
A bug where application code borrows a database connection from the pool but never returns it, gradually exhausting available connections until the pool is empty and all subsequent requests fail.
leak-detection-threshold
A HikariCP setting that logs a warning with a stack trace when a connection has been borrowed for longer than the configured duration, helping developers locate the code that failed to close the connection.
KingbaseES
A Chinese relational database management system based on PostgreSQL, commonly used in enterprise and government deployments in China. It supports standard SQL and JDBC connectivity.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗