Quick revision
The short answer
I would not raise the thread pool. The bottleneck is the 20 database connections, so 400 threads would only mean 380 threads waiting in getConnection instead of 180, with more memory and more timeouts. Instead, limit database concurrency to about the connection pool size, decide the pool size from what the database can handle, shorten how long each request holds a connection, and set a short connection timeout so requests fail fast instead of queueing.
What to remember
In this answer, we saw that 200 or 400 threads make no difference when only 20 connections can do database work: throughput is capped at connections divided by hold time, and extra threads only add waiting, memory and timeouts. The fixes are to match database concurrency to the connection pool, to size the pool from the database's capacity across all instances, to shorten hold time by keeping remote calls out of transactions and disabling open-in-view, and to set a short connection timeout so overload fails fast.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would not raise the thread pool. The bottleneck is the 20 database connections, so 400 threads would only mean 380 threads waiting in getConnection instead of 180, with more memory and more timeouts. Instead, limit database concurrency to about the connection pool size, decide the pool size from what the database can handle, shorten how long each request holds a connection, and set a short connection timeout so requests fail fast instead of queueing.
Why this question is asked
This scenario appears in almost every team that runs a Java service against a relational database, and the suggested fix is the most natural one: things are slow, threads are busy, so add threads. Interviewers ask it to see whether we can find the real bottleneck before turning a knob. In this answer, we will recap how HikariCP hands out connections, show why 400 threads cannot help, work out what limits throughput, and then go through the four changes that actually help: matching concurrency to connections, sizing the pool from the database, holding connections for less time, and failing fast.
1A Quick Recap of How HikariCP Hands Out Connections
HikariCP is the connection pool Spring Boot uses by default. It keeps a fixed number of open database connections, here 20 (spring.datasource.hikari.maximum-pool-size). When code needs to talk to the database, it calls getConnection(). If a connection is free, it gets one at once. If all 20 are in use, the calling thread waits until another thread returns one, or until connectionTimeout passes, which is 30 seconds by default, and then it gets an SQLTransientConnectionException.
So in this service, at most 20 threads can be doing database work at any moment. The other 180 threads that need the database are standing in a queue inside Hikari.
2Why 400 Threads Cannot Help
Throughput is limited by the busiest resource. If each request holds a connection for 50 ms, 20 connections can serve 20 / 0.05 = 400 requests per second, and not one more, whatever the number of threads. Little's law tells us the rest: if 600 requests per second arrive, the extra 200 per second pile up in the wait queue and the waiting time keeps growing.
| Thread pool | Working (have a connection) | Waiting in getConnection | Max throughput |
|---|---|---|---|
| 200 (today) | 20 | up to 180 | 400 per second |
| 400 (proposed) | 20 | up to 380 | 400 per second |
| about 25 for DB work | 20 | about 5 | 400 per second, short waits |
Doubling the threads keeps the 20 working threads exactly where they were and doubles the waiting ones. Each waiting thread still has its own stack, usually reserving up to 1 MB, and holds its request's objects in memory. Waits get longer because the queue in front of the same 20 connections is longer, so more requests hit timeouts. And when the database does slow down, a larger thread pool lets even more requests pile up before anything rejects them. In short, latency gets worse, not better.
3Fix 1: Match Database Concurrency to the Connection Pool
The work that touches the database should run with a concurrency close to the connection pool, perhaps a little above so that a connection is never idle while a thread does the small non-database part of its task. If the 200 threads also do other work, such as calling other services, we limit only the database part, for example with a separate executor or a semaphore of 25 permits around database access. Requests beyond that limit wait briefly at the front door or are rejected with a clear 503, which is better than waiting 30 seconds inside Hikari.
4Fix 2: Size the Connection Pool From the Database, Not From Threads
Is 20 the right number? That question has to be answered from the database's side. A database server can only run so many queries in parallel; beyond roughly a small multiple of its cores, extra connections add contention for locks, memory and disk instead of throughput. A starting point suggested by the HikariCP project is around twice the database's core count, and then we measure.
We must also count the whole fleet. If ten instances each have 20 connections, the database sees 200 connections, and PostgreSQL's default max_connections is 100. So raising one instance's pool to 40 could break the whole fleet during a deploy. The right size is total database capacity divided by the number of instances, with room for deploys, migrations and admin tools.
5Fix 3: Hold Each Connection for Less Time
The most effective change is usually to make each request hold its connection for less time, because halving the hold time doubles the throughput of the same 20 connections. The classic mistake is a remote call inside a transaction: the connection stays checked out while we wait for another service.
Notice the effect: the same request went from holding a connection for about 320 ms to about 20 ms, so the pool can serve sixteen times as many requests. The fixed version needs care: if the charge succeeds and the second step fails, the order must be recoverable, for example with a pending state that a job reconciles.
Two more sources of long hold times are worth checking. Spring Boot's spring.jpa.open-in-view is true by default, which can keep a connection for the whole web request once it has been used, including while the view is rendered; setting it to false returns the connection when the transaction ends. And slow queries are hold time too, so the slowest queries in the database's statistics are often the cheapest fix of all.
6Fix 4: Fail Fast With a Short Connection Timeout
A 30-second wait for a connection is far longer than any user will wait. We set spring.datasource.hikari.connection-timeout to something like 2000 ms, so that when the pool is exhausted, requests fail quickly with an exception we map to a 503 instead of tying up threads for half a minute. Hikari's Micrometer metrics, such as hikaricp.connections.pending (threads waiting) and the time spent acquiring a connection, show us how close we run to the limit, and an alert on pending connections warns us before users notice.
7Follow-up questions
How can you prove the connection pool is the bottleneck?
A thread dump shows most request threads parked inside HikariPool.getConnection, and Hikari's metrics show active connections pinned at 20 with many pending threads. Meanwhile, the database itself may be mostly idle, which also suggests connections are held while nothing is happening.
Should we just raise the connection pool to 200 instead?
Only if the database can actually run that many queries in parallel, across all instances, which is rarely true. Too many connections cause contention inside the database and can make every query slower. Size it from the database's capacity, then shorten hold times.
How do virtual threads change this picture?
With virtual threads, there is no thread pool to raise at all, and thousands of requests could wait for the same 20 connections. So the connection pool, plus an explicit limit with a short timeout, becomes the main protection, and reducing hold time matters even more.
8Conclusion
In this answer, we saw that 200 or 400 threads make no difference when only 20 connections can do database work: throughput is capped at connections divided by hold time, and extra threads only add waiting, memory and timeouts. The fixes are to match database concurrency to the connection pool, to size the pool from the database's capacity across all instances, to shorten hold time by keeping remote calls out of transactions and disabling open-in-view, and to set a short connection timeout so overload fails fast.
Open with
I would push back on 400 threads. The symptom says the bottleneck is the 20 connections: most threads are parked in getConnection, which means at most 20 threads can do database work, and throughput is capped at 20 divided by the hold time.
Then, if they want more
If a request holds a connection for 50 milliseconds, that is 400 requests per second whether we have 200 threads or 400. More threads just means 380 waiting instead of 180, more memory, longer queues and more timeouts. So first I would make database concurrency roughly match the connection pool, maybe 25, with excess requests rejected quickly.
Second, I would ask whether 20 is right, but from the database's side: what it can run in parallel, and across all instances, because ten instances with 20 each is already 200 connections. Third, and usually the biggest win, I would reduce how long each request holds a connection.
I would look for remote calls inside transactions, like a payment call inside a transactional method, which hold an idle connection for hundreds of milliseconds, turn off open-in-view, and fix the slowest queries. Halving hold time doubles throughput with the same pool.
Fourth, I would drop Hikari's connection timeout from 30 seconds to about 2, so an exhausted pool gives a fast 503, and alert on pending connections.
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
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "A service has a 200-thread pool calling the database and a Hikari connection pool of 20....": https://scrivio-interview-library.netlify.app/java/executors/51b0da8dd5/