Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

After enabling virtual threads, a service handles more concurrent requests, but the database starts rejecting connections and a third-party client is overloaded. What happened and what do you do?

With virtual threads, the server no longer limits concurrency through a fixed pool of 200 threads, so many more requests reach the database and the third-party API at the same time, and they hit limits that the thread pool used to protect without anyone noticing. The fix is to limit concurrency explicitly with semaphores or bulkheads around downstream calls, keep connection pools sized to database capacity so requests wait for a connection, check for pinning and heavy ThreadLocal use, and load test before and after.

Why this question is asked

This scenario is the most common surprise after enabling virtual threads. The service itself looks better than ever, more concurrent requests and no thread exhaustion, but its neighbours start failing. Interviewers ask it to see whether we understand that a thread pool was doing two jobs, providing threads and limiting load, and that removing it removes both. In this answer, we will recap the hidden job the pool was doing, reproduce how the overload happens, then add explicit limits for the database and the third-party client, check for pinning and ThreadLocal costs, and plan the load tests.

1A Quick Recap: The Pool Was Also a Limit

Before the change, Tomcat had 200 worker threads. That meant at most 200 requests could run at once, so at most 200 could call the third-party API at once, and at most 200 could try to get a database connection. Nobody designed it as a safety limit, but it was one. Requests beyond 200 waited in Tomcat, where waiting is cheap and harmless to neighbours.

With spring.threads.virtual.enabled=true, each request gets its own virtual thread and there is no pool. If 3,000 requests arrive, 3,000 run, and all 3,000 reach the next layer at once. The queue did not disappear; it moved from Tomcat to the downstream systems, which are less able to handle it.

2How the Overload Happens

Two things went wrong in the question. The database rejecting connections suggests that something opens more connections than the database allows: perhaps several instances each with a large pool, or code that opens connections outside the pool. Even with a correctly sized pool, thousands of threads waiting on it means long waits and timeouts. The third-party client being overloaded is simpler: it now receives 3,000 parallel calls where it used to receive at most 200, and it may have a contractual rate limit or simply not cope.

Concurrency reaching each downstream
Downstream200 platform threadsVirtual threads
Requests running at onceup to 200as many as arrive, e.g. 3,000
Calls to the partner APIup to 200up to 3,000
Threads asking for a DB connectionup to 200up to 3,000
Where excess requests waitIn Tomcat, cheapAt the downstream, harmful

3Fix 1: Limit Downstream Calls Explicitly

The replacement for the hidden limit is an explicit one, placed exactly where it matters: around each downstream call. A semaphore is the simplest form. It holds a number of permits; a thread must take one before the call and give it back after, so no more than that number can be inside at once. A semaphore is also cheap for virtual threads to wait on, because waiting on it unmounts the virtual thread.

Java
@Component
class PricingClient {
private final RestClient client;
private final Semaphore permits = new Semaphore(50); // the partner allows about 50 parallel calls
PricingClient(RestClient.Builder builder) { this.client = builder.baseUrl("https://pricing.partner").build(); }
Quote quote(String sku) {
try {
if (!permits.tryAcquire(500, TimeUnit.MILLISECONDS)) {
throw new PricingBusyException(sku); // fail fast instead of piling up
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PricingBusyException(sku);
}
try {
return client.get().uri("/quotes/{sku}", sku).retrieve().body(Quote.class);
} finally {
permits.release();
}
}
}
// 3,000 requests: 50 calls in flight at a time, the rest wait up to 500 ms, then get a fast error

Notice the timeout on tryAcquire. Without it, thousands of virtual threads would wait indefinitely, and the user would wait with them. With it, excess requests fail fast and can be answered with a fallback or a 503. Libraries such as Resilience4j offer the same idea as a bulkhead, and Spring Framework 7 adds a @ConcurrencyLimit annotation for methods (check your version). The number 50 comes from the partner's documented limit or a load test, not from guessing.

4Fix 2: Keep the Connection Pool Sized to the Database

For the database, the connection pool is already the right kind of limit, as long as it is sized to what the database can take. If PostgreSQL allows 200 connections and we run 8 instances, each pool gets at most about 20, leaving headroom for migrations and admin tools. It is tempting to raise the pool to match the new concurrency, but that is exactly what makes the database reject connections. Requests should wait for a connection in the pool, with a short connection timeout so they fail fast rather than queue for 30 seconds.

Properties
spring.datasource.hikari.maximum-pool-size=20 # per instance: 8 instances x 20 = 160, under 200
spring.datasource.hikari.connection-timeout=2000 # wait at most 2 s for a connection
# Every database access must go through this pool: no DriverManager.getConnection in code
Step throughBounded access with virtual threads
Virtual threadsSemaphore (50)Partner APIHikari pool (20)
tryAcquire, up to 500 ms
permit granted, 50 at most
quote(sku)
quote
release permit
getConnection, up to 2 s
connection, 20 at most

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

Each downstream gets its own explicit limit, so excess waits in our service and fails fast.

5Fix 3: Check Pinning and ThreadLocal Use

Two virtual-thread specific problems make overload worse. On JDKs before 24, a virtual thread that blocks while holding a monitor, inside a synchronized block or method, is pinned: it cannot unmount, so it holds its carrier. With only a few carriers, a handful of pinned threads can stall the whole service. Older JDBC drivers and some client libraries used synchronized around I/O. The JFR event jdk.VirtualThreadPinned shows where it happens, and moving to JDK 24 or later, or to library versions that use ReentrantLock, removes most cases.

ThreadLocal is the second. With a thread pool, a ThreadLocal cache was created 200 times and reused. With a virtual thread per request, it is created for every request, so a 1 MB buffer kept in a ThreadLocal costs 1 MB per concurrent request. With 3,000 concurrent requests that is 3 GB, which shows up as heap pressure and long garbage collection pauses.

6Load Testing Before and After

Finally, we test the change the way it fails. We run the same load test with virtual threads off and on, at normal and at peak traffic, and we watch the downstreams as closely as the service: partner call concurrency and errors, Hikari active and pending connections, database connection count, heap use and pinning events. The goal is that the service's latency improves while the downstream numbers stay inside their limits. If any downstream metric jumps, the explicit limit for it is missing or set too high.

7Follow-up questions

Should we just turn virtual threads off again?

That is a fine short-term rollback, which is why the change should sit behind a property. But the real problem is that our protection was accidental. Adding explicit limits makes the service safe whatever the threading model, and then virtual threads can be enabled again with confidence.

Why not limit concurrency at the Tomcat level instead?

A single global limit treats every request the same, so a slow partner could still use all of it. Limits per downstream protect each dependency at its own capacity, and requests that do not touch the partner are never blocked by it.

How can we see pinning in production?

Recording a JFR session with the jdk.VirtualThreadPinned event enabled shows the stack traces where virtual threads blocked while pinned. On older JDKs, the system property jdk.tracePinnedThreads printed similar stacks, though it was removed in later versions; check your JDK.

8Conclusion

In this answer, we saw that the fixed thread pool was also an accidental limit on how much load reached downstreams, and virtual threads removed it, so the database and the partner API took the full concurrency. We limited each downstream explicitly with semaphores or bulkheads and fast failure, kept the connection pool sized to database capacity with a short connection timeout, checked for pinning on older JDKs and heavy ThreadLocal use, and load tested before and after with the downstream metrics in view.

What to say in the interviewAbout 90 seconds aloud

Open with

What happened is that the Tomcat thread pool was doing a second job nobody had written down. With 200 platform threads, at most 200 requests could call the partner or ask for a database connection at once.

Then, if they want more

With virtual threads there is no pool, so if 3,000 requests arrive, 3,000 run and they all hit the downstreams together. The queue moved from Tomcat, where waiting is cheap, to the database and the partner, which cannot absorb it. So the fix is to replace the accidental limit with explicit ones.

Around the partner client I would put a semaphore or a bulkhead with a limit from their documented capacity, say 50 parallel calls, with a short acquire timeout so excess requests fail fast with a fallback or a 503.

For the database, I keep the Hikari pool sized from database capacity divided across instances, not raised to match the new concurrency, and set a short connection timeout so requests wait in the pool briefly and then fail.

I would also check for pinning on JDKs before 24, using the JFR pinned event, and for large ThreadLocal caches, since they are now created per request. Then I would load test with virtual threads off and on, watching the downstream metrics as well as our own latency.

About this answer

Last updated
12 November 2026
Versions it names
Java 24, Spring Framework 7
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 "After enabling virtual threads, a service handles more concurrent requests, but the...": https://scrivio-interview-library.netlify.app/spring-boot/embedded-servers/7c2c76f636/