The 30 second answer
I check the pool metrics first: pending threads and connection acquire time. If threads wait for connections, I look for long transactions holding them, like slow queries or remote HTTP calls inside a transaction. A bigger pool is rarely the fix, because small pools usually perform better. I set a connection timeout so callers fail fast and enable leak detection to log connections held too long.
Check Hikari metrics: pending threads and connection acquire time. If threads wait for connections, look for long transactions holding them: slow queries, or remote HTTP calls made inside a transaction. A bigger pool is rarely the fix; HikariCP's sizing guide says a small pool, starting near (core_count * 2) + effective_spindle_count, usually beats a big one. Set connectionTimeout so callers fail fast, and leakDetectionThreshold to log connections held too long. Follow-up: Why can a bigger pool make it worse? (More concurrent queries compete for the same CPU and disk on the DB.)
Alternate interview scenarios
"Your database connection pool is exhausted. What would you check first?"
Here the pool is already failing, not just slowing down now and then. Callers wait for the full connectionTimeout (30 seconds by default in HikariCP) and then get an exception saying no connection is available. First I compare active, idle and pending connections. If active stays at the maximum but the database shows little real work, the likely cause is a leak or a connection held while nothing runs. Examples are a connection that is never closed, a transaction open around a remote call, or Open Session in View keeping the connection through view rendering. leakDetectionThreshold logs the stack trace of the code that holds the connection. If active is at the maximum and the database is busy, I check pg_stat_activity for long-running queries, sessions that are idle in a transaction, and lock waits. Lowering connectionTimeout makes callers fail fast, but it does not fix the cause.
"Database CPU is low, but requests wait for connections. What is holding them?"
Connections are busy without doing database work. The usual suspects are long transactions that wait on something else: a remote HTTP call or message send inside @Transactional, open-in-view holding the connection through view rendering, or lazy loading during serialization. Streaming a large result set through a cursor also keeps the connection for the whole stream, so a slow consumer holds it. In HikariCP terms, connectionTimeout (30 seconds by default) is how long a caller waits before failing, so timeouts mean starvation, not a slow database. Fixes: keep transactions short and move remote calls outside them; mark read paths readOnly (Spring Data's CRUD read methods already are); batch writes with hibernate.jdbc.batch_size instead of one round trip per row; and use leakDetectionThreshold to log the code that holds a connection too long.
"How would you tune a saturated connection pool?"
Here saturation is already confirmed, so I spend less time proving the pool is the bottleneck and more on what to change. I start from metrics: active, idle and pending connections, acquire time, and query latency on the database, not from a guess at maximumPoolSize. If pending is high and the database still has headroom, a slightly larger pool or a shorter connectionTimeout (HikariCP's default is 30 seconds) can stop threads piling up; HikariCP's sizing guide still warns that a pool far above (core_count * 2) + effective_spindle_count usually makes the database slower. If active is already at the maximum and the database is busy, I do not grow the pool; I shorten transactions, add statement or query timeouts, and move HTTP calls out of @Transactional work. leakDetectionThreshold tells me whether the saturation is a leak rather than load.
Sources (6)
- HikariCP wiki: About pool sizinggithub.com
- HikariCP configuration (maximumPoolSize, connectionTimeout, leakDetectionThreshold)github.com
- PostgreSQL 17 docs: pg_stat_activitypostgresql.org
- Spring Boot: Open EntityManager in Viewdocs.spring.io
- Spring Data JPA reference: Transactionality (CRUD methods, readOnly)docs.spring.io
- Hibernate 6.6 User Guide: JDBC batching (hibernate.jdbc.batch_size)docs.jboss.org