Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

Under load a service's latency jumps to 30 seconds while CPU is at 20 percent. A thread dump shows all 200 Tomcat threads waiting in HikariPool.getConnection. What is happening, and what would you change?

The request threads are not working, they are waiting: all 200 Tomcat threads queue for a Hikari pool that holds only 10 connections by default, so latency grows while the CPU stays idle, and 30 seconds is exactly Hikari's default connection timeout. The fix is to find why connections are held so long (slow queries, long transactions, remote calls inside transactions), set a short connection timeout so requests fail fast, and size the pool from database capacity. Raising Tomcat threads would make it worse.

Why this question is asked

This symptom, high latency with low CPU, confuses many teams, because the usual reflex is to add more threads or more pods. Interviewers ask it because it tests whether we can read a thread dump and reason about queues rather than guess. In this answer, we will recap the two pools involved, read what the thread dump is telling us, explain why the latency is exactly 30 seconds, find what holds the connections, and then change the right settings in the right order.

1A Quick Recap of the Two Pools

Tomcat runs each request on a worker thread from its pool, which has 200 threads by default. When our code needs the database, it borrows a connection from HikariCP, the connection pool Spring Boot uses by default. A connection pool keeps a few open database connections and lends them out, because opening a new connection for each query would be slow.

Hikari's default maximum pool size is 10. So by default, at most 10 requests can talk to the database at the same time, while up to 200 requests can be running. The ratio of 200 to 10 is fine when each request holds a connection for a few milliseconds. It becomes a problem when connections are held for a long time.

2Reading the Thread Dump

A thread dump is a snapshot of what every thread is doing. We can take one with jcmd and look at the Tomcat worker threads.

Shell
$ jcmd 4711 Thread.print | grep -A 6 'http-nio-8080-exec' | head
"http-nio-8080-exec-17" #63 daemon prio=5 ... TIMED_WAITING (parking)
at jdk.internal.misc.Unsafe.park(Native Method)
at java.util.concurrent.SynchronousQueue...poll
at com.zaxxer.hikari.util.ConcurrentBag.borrow(ConcurrentBag.java)
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java) // waiting for a connection
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection
// the same stack for about 190 threads; about 10 threads are inside the JDBC driver

Notice the state, TIMED_WAITING, and where it waits: inside HikariPool.getConnection. These threads are parked, using no CPU, waiting for one of the 10 connections to come back. That explains the 20 percent CPU. The roughly 10 threads not in this stack are the ones holding connections, and they are the most interesting ones, because they show what the connections are being used for.

3Why the Latency Is 30 Seconds

Requests queue behind the pool. If each request holds a connection for 500 milliseconds, 10 connections can serve 20 requests per second. If 60 requests per second arrive, the queue grows by 40 every second, and every new request waits longer than the last.

Hikari does not let a thread wait forever. Its connectionTimeout, 30 seconds by default, is how long getConnection() waits before throwing an SQLTransientConnectionException. So the latency settles at around 30 seconds: requests either get a connection just before the limit or fail at the limit. The 30 seconds in the question is a strong clue that we are looking at Hikari's default timeout.

Step through200 threads, 10 connections
Tomcat threadHikari poolPostgreSQL
getConnection()
all 10 lent out: wait in the queue
parked, using no CPU, up to 30 s
a connection comes back, or timeout after 30 s
run query
result
return connection

7 messages in order. Play them, or step through them one at a time.

Waiting threads use no CPU, which is why CPU stays at 20 percent while latency climbs.

4Finding Why Connections Are Held So Long

The real question is why 10 connections are not enough, and the answer is usually one of three things. The first is slow queries: a missing index can turn a 5 millisecond query into 800 milliseconds. The second is long transactions, where a @Transactional method holds its connection from start to end, including all the Java work in between. The third, and the most common in this pattern, is a remote call inside a transaction.

Java
@Transactional
public Order placeOrder(OrderRequest req) {
Order order = orders.save(Order.from(req)); // connection taken here
PriceQuote q = pricingClient.quote(req); // HTTP call: 1.5 s, connection still held
order.setPrice(q.total());
return order; // connection returned at commit
}
// Better: call the remote service first, then run a short transaction
public Order placeOrder(OrderRequest req) {
PriceQuote q = pricingClient.quote(req); // no connection held
return orderWriter.save(req, q); // @Transactional on another bean, about 10 ms
}

In the first version, each order holds a database connection for the whole 1.5 second pricing call, so 10 connections serve under 7 orders per second. In the second, the connection is held for about 10 milliseconds. The pool did not change; how long we keep each connection did.

One more Spring Boot default can hide here: spring.jpa.open-in-view is true by default, which keeps the JPA session, and with it a connection once one is used, open until the view is rendered. Setting it to false makes connection use match the transaction boundaries. Hikari's leakDetectionThreshold, for example 2 seconds, logs a stack trace whenever a connection is held longer, which points straight at the code.

5What to Change, and Why Not More Threads

First, fail fast. A user waiting 30 seconds is worse than a quick error, and fast failure lets retries, fallbacks and load balancers do their job.

Properties
spring.datasource.hikari.connection-timeout=2000 # wait at most 2 s for a connection (milliseconds)
spring.datasource.hikari.maximum-pool-size=20 # from database capacity, not from thread count
spring.datasource.hikari.leak-detection-threshold=2000
spring.jpa.open-in-view=false
Step throughFixing a service stuck on the connection pool
  1. Confirm threads wait in HikariPool.getConnectionthread dump, hikaricp.connections.pending
  2. Set connection-timeout to fail fastfor example 2 s
  3. Find long holdersleak detection, slow query log, open-in-view
  4. Move remote calls out of transactions
  5. Still waiting after the fixes?
    YesRaise the pool within database capacityNoDone
  6. Leave Tomcat threads alone

6 steps in order. Play them, or step through them one at a time.

Second, size the pool from what the database can handle. If PostgreSQL allows 300 connections and 12 pods share it, each pod can have at most 25, minus headroom for migrations and admin tools. A database often does more total work with fewer, busier connections, so we raise the pool only after measuring.

Raising Tomcat's threads would make it worse. With 800 threads, 790 would queue for the same 10 connections, each queued request would hold memory and an open client connection, and the database would see no more work than before. More threads only grow the waiting line in front of the real bottleneck.

6Follow-up questions

How can we see this without a thread dump?

Hikari publishes Micrometer metrics: hikaricp.connections.active stuck at the maximum and hikaricp.connections.pending above zero show the queue directly, and hikaricp.connections.acquire shows how long threads wait. Together with tomcat.threads.busy at 200, they tell the same story as the dump.

Should the pool simply be 200 to match the threads?

Usually not. The database has a fixed number of cores and a connection limit shared by every pod, and more parallel queries than it can run just move the queue into PostgreSQL, where it is harder to see and costs memory per connection. The pool should be sized from database capacity, divided across instances.

Would virtual threads fix this?

No. Virtual threads remove the limit of 200 Tomcat threads, but the pool still has 10 connections. Thousands of virtual threads would wait for them instead of 190, so the latency and the timeouts stay until we shorten how long connections are held.

7Conclusion

In this answer, we read a thread dump where all 200 Tomcat threads waited in HikariPool.getConnection. The pool of 10 connections was the bottleneck, requests queued behind it with no CPU use, and the 30 seconds came from Hikari's default connection timeout. The fix is to find why connections are held long, such as slow queries, long transactions or remote calls inside transactions, to fail fast with a short timeout, and to size the pool from database capacity, never by adding Tomcat threads.

What to say in the interviewAbout 80 seconds aloud

Open with

The thread dump tells me the threads are not working, they are waiting. All 200 Tomcat threads are parked in HikariPool.getConnection, and Hikari's default pool has only 10 connections.

Then, if they want more

So 10 requests at a time can use the database and the other 190 queue behind them. Parked threads use no CPU, which is why CPU is at 20 percent while latency climbs.

The 30 seconds is a clue too: it is Hikari's default connection timeout, so requests either get a connection just before 30 seconds or fail at 30 seconds. The real question is why 10 connections are not enough, so I would look at the threads that hold connections and turn on leak detection.

Usually it is a slow query, a long transaction, or a remote HTTP call made inside a @Transactional method, which holds a connection for the whole call. I would also check that open-in-view is off.

Then I would set connection-timeout to something like two seconds so requests fail fast, move remote calls out of transactions, and size the pool from what the database can handle across all pods, not from the thread count. I would not raise Tomcat threads, because that only makes the queue in front of the pool longer.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Under load a service's latency jumps to 30 seconds while CPU is at 20 percent. A thread...": https://scrivio-interview-library.netlify.app/spring-boot/embedded-servers/f88028e800/