Quick revision
The short answer
Before the change, Tomcat's pool of 200 threads quietly limited how many requests could run at once; with virtual threads that limit is gone, so far more requests reach the HikariCP connection pool at the same time. They queue for its few connections and time out after connectionTimeout, and whatever gets through puts more concurrent work on the database. The fix is to limit concurrency explicitly, with semaphores, bulkheads or rate limits, keep the pool sized for what the database can handle rather than for the number of threads, and on older JDKs check for pinned threads with -Djdk.tracePinnedThreads (removed in newer JDKs) or the JFR pinning event.
What to remember
In this answer, we saw why enabling virtual threads raised throughput but caused database pool timeouts. Tomcat's 200-thread pool had been an accidental limit on concurrency; without it, requests piled up in front of HikariCP, timed out after connectionTimeout, and raising the pool would only overload the database. Little's law showed that the extra demand must wait or fail somewhere, so we added an explicit limit with a semaphore or bulkhead that fails fast, shortened the connection timeout, sized the pool for the database rather than for the threads, and checked for pinning with jdk.tracePinnedThreads on older JDKs or JFR events.
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.
Before the change, Tomcat's pool of 200 threads quietly limited how many requests could run at once; with virtual threads that limit is gone, so far more requests reach the HikariCP connection pool at the same time. They queue for its few connections and time out after connectionTimeout, and whatever gets through puts more concurrent work on the database. The fix is to limit concurrency explicitly, with semaphores, bulkheads or rate limits, keep the pool sized for what the database can handle rather than for the number of threads, and on older JDKs check for pinned threads with -Djdk.tracePinnedThreads (removed in newer JDKs) or the JFR pinning event.
Why this question is asked
This is the most common surprise after enabling virtual threads, and it teaches an important lesson about capacity: a limit we never configured on purpose was protecting the system. Teams switch on spring.threads.virtual.enabled, see throughput rise in a test, and then watch the database struggle in production. Interviewers ask about it because the answer requires reasoning about queues and limits rather than about threads. In this answer, we will recap what the thread pool was doing for us, reproduce the failure, explain it with a little queueing arithmetic, fix it with explicit limits, size the pool correctly, and check for pinning.
1A Quick Recap: The Limit We Did Not Know We Had
In a classic Spring MVC service, Tomcat handles each request on a thread from a pool whose default maximum is 200. When all 200 are busy, new requests wait in Tomcat's accept queue. So at most 200 requests are inside the application at once, and at most 200 threads can ask HikariCP for a database connection. Nobody designed this as a database protection, but it worked as one: the database never saw more than 200 requests' worth of demand.
HikariCP itself is a pool of database connections, 10 by default (maximumPoolSize). A thread that wants a connection when all are in use waits up to connectionTimeout, 30 seconds by default, and then gets an SQLTransientConnectionException saying the connection is not available.
2Reproducing the Failure
Consider an endpoint that runs one query taking 20 ms, on a pool of 10 connections. That pool can complete at most 10 / 0.02 = 500 queries per second. Under a load of 800 requests per second, the difference between the two setups becomes visible.
| Aspect | Tomcat pool of 200 | Virtual threads |
|---|---|---|
| Requests inside the app | at most 200 | unbounded, grows with backlog |
| Where excess requests wait | Tomcat accept queue | Hikari wait queue, holding memory |
| Hikari waiters | at most 190 | thousands |
| Failure mode | slower responses | pool timeouts after 30 s |
| If pool raised to 100 | database load up | database overloaded |
Notice that throughput rose at first, which is why the change looked good in a short test: requests no longer wait in Tomcat, and the pool works at full speed. But the backlog of virtual threads waiting for a connection keeps growing, each holding its request's memory, until they start failing with pool timeouts. If the team then raises maximumPoolSize to 100 to stop the timeouts, the database receives ten times as many concurrent queries, its CPU and lock contention rise, each query slows down, and the database itself becomes the bottleneck.
3Why This Happens: Little's Law
Little's law states that the number of requests in a system equals the arrival rate times the time each spends inside. The database pool can serve 500 queries per second; if 800 arrive per second, no thread model can make the extra 300 succeed. The only question is where they wait and how they fail. The old thread pool made them wait outside the application and limited how many could pile up. Virtual threads moved the queue inside, in front of the connection pool, where it is unbounded and fails slowly.
So the conclusion is not that virtual threads are bad. They removed an accidental limit, and now we must put back an intentional one, at the right place, with the right size, and with a fast failure when it is full.
4Fix 1: Limit Concurrency Explicitly
The direct fix is a limit on how many requests may use the database at once, placed in front of the repository. A Semaphore is enough; with virtual threads, blocking on acquire is cheap, but we use a timeout so overload fails fast.
- Request arrives on a virtual threadcheap, no Tomcat pool limit
- Permit available within 200 ms?YesBorrow a Hikari connectionNoReturn 503 with Retry-After
- Query runs on one of 10 connectionsabout 20 ms
- Release connection and permitnext waiter proceeds
4 steps in order. Play them, or step through them one at a time.
The permits bound how many requests can wait on the pool, so the Hikari queue stays short and its waits stay in milliseconds. Requests beyond the limit fail within 200 ms with a 503 instead of after 30 seconds with a pool timeout, which is better for the caller and for the service, which no longer accumulates thousands of waiting threads. Resilience4j's Bulkhead implements the same idea with metrics, and a rate limiter at the edge or in the gateway can cap the incoming rate per client. We should also lower Hikari's connectionTimeout to something like 1 to 2 seconds, so that any remaining waits fail quickly.
5Fix 2: Size the Pool for the Database, Not for the Threads
It is tempting to give a service with thousands of virtual threads a pool of hundreds of connections. That is backwards. A database server has a limited number of cores and disks; past a certain number of concurrent queries, adding more only increases contention and slows every query. The pool should be sized for what the database can handle, divided across all instances of all services that share it. A common starting point from the HikariCP guidance is a small number related to the database's cores, and then load testing to find where throughput stops rising. If 6 instances share a database that performs best at 60 concurrent queries, each instance gets a pool of about 10, whatever its thread count.
6Checking for Pinned Threads
One more cause can look similar: pinning. On JDK 21 to 23, a virtual thread blocking inside a synchronized block cannot unmount and holds its carrier. If a library holds a monitor while waiting for a connection or doing I/O, the few carriers fill up and the whole application stalls, which can look like a pool problem. On those JDKs we can run with -Djdk.tracePinnedThreads=full to print stack traces of pinned threads, or record the JFR event jdk.VirtualThreadPinned. The system property was removed in JDK 24, which, through JEP 491, also stopped synchronized from pinning in most cases, so on Java 25 JFR is the tool for the remaining cases, such as native frames. Upgrading the JDK and the JDBC driver is often the simplest fix.
7Follow-up questions
Should we just turn virtual threads off again?
That restores the accidental limit and the old behaviour, which is a reasonable emergency rollback. But the better long-term state is virtual threads plus explicit limits, because then the limit is chosen, sized and visible, and fails fast instead of queueing in Tomcat.
Where else does the same problem appear?
Anywhere a thread pool used to cap demand: HTTP client connection pools, calls to rate-limited third-party APIs, Redis connections, and file handles. Each dependency with a capacity needs its own explicit limit once virtual threads remove the thread cap.
How do we choose the semaphore size?
Start from the pool size and the acceptable wait: with 10 connections and 20 ms queries, a queue of 30 extra waiters adds at most about 60 ms of waiting, so 40 permits is reasonable. Then confirm with a load test that latency stays acceptable and the 503 rate under overload is what we expect.
8Conclusion
In this answer, we saw why enabling virtual threads raised throughput but caused database pool timeouts. Tomcat's 200-thread pool had been an accidental limit on concurrency; without it, requests piled up in front of HikariCP, timed out after connectionTimeout, and raising the pool would only overload the database. Little's law showed that the extra demand must wait or fail somewhere, so we added an explicit limit with a semaphore or bulkhead that fails fast, shortened the connection timeout, sized the pool for the database rather than for the threads, and checked for pinning with jdk.tracePinnedThreads on older JDKs or JFR events.
Open with
Before the change, Tomcat's thread pool, 200 by default, was quietly limiting concurrency: at most 200 requests were inside the app, so at most 200 threads could compete for HikariCP's connections.
Then, if they want more
With virtual threads, every request gets a thread immediately, so concurrency is effectively unbounded. The database pool, say 10 connections at 20 ms per query, can only do 500 queries a second, so when more arrive, virtual threads pile up waiting for a connection and eventually fail after Hikari's connectionTimeout of 30 seconds.
If someone raises the pool size to fix the timeouts, the database gets far more concurrent queries and slows down for everyone. By Little's law, the excess demand has to wait or fail somewhere; the old pool just made it wait outside.
So I would put back an intentional limit: a semaphore or a Resilience4j bulkhead in front of database access, a few times the pool size, with tryAcquire and a short timeout so overload returns a 503 quickly, plus a rate limit at the edge if needed. I would lower connectionTimeout to a second or two.
The pool stays sized for what the database can handle, split across all instances, not for the number of threads. Finally, on JDK 21 to 23 I would check for pinning with jdk.tracePinnedThreads or the JFR VirtualThreadPinned event, since synchronized blocks around I/O could stall carriers; JDK 24 removed most of that and the property.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 24, Java 25
- 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 Framework reference, Spring WebFlux, Project Reactor reference guide
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 "After enabling virtual threads in a Spring MVC service, throughput rises but the...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-testing-and-ops/40fee19ec8/