Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

Your team wants to replace async servlet code and DeferredResult handling with virtual threads. What do you evaluate?

Virtual threads let ordinary blocking code handle many concurrent requests, so async servlet code and DeferredResult handlers that existed only to free container threads may become unnecessary; Spring Boot 3.2 and later turns them on with spring.threads.virtual.enabled=true on Java 21 or later. Before removing the async code we should check for pinning in synchronized blocks on older JDKs, find the limits that become the new bottleneck such as connection pools, and load test before and after, keeping async for long-held connections like streaming where it still helps.

Why this question is asked

Many Spring MVC services contain Callable, DeferredResult or hand-written AsyncContext code that was added for one reason: a request waiting on a slow call should not hold one of Tomcat's 200 platform threads. That code is harder to read, harder to debug and easy to get wrong with timeouts and thread-local context. Virtual threads promise the same capacity with plain blocking code, so teams naturally want to delete the async layer. Interviewers ask this scenario to see whether we can evaluate a change like this carefully instead of flipping a switch. In this answer, we will recap what virtual threads change, see which async code becomes unnecessary, look at pinning and the new bottlenecks, and then plan the load test and decide what to keep.

1A Quick Recap of Virtual Threads

A platform thread is a thin wrapper around an operating system thread. Each one reserves a stack of around 1 MB by default and is expensive to create, so servers keep a fixed pool. A virtual thread, final since Java 21, is a thread managed by the JVM. When it blocks on I/O, the JVM unmounts it from the small set of carrier platform threads and runs another virtual thread instead. We can have hundreds of thousands of them.

In Spring Boot 3.2 and later, setting spring.threads.virtual.enabled=true makes embedded Tomcat or Jetty run each request on a new virtual thread, and also switches Spring's task executors and scheduled tasks to virtual threads. The request code stays exactly the same.

Java
# application.properties (Java 21+, Spring Boot 3.2+)
spring.threads.virtual.enabled=true
// Inside a controller after the switch:
log.info("{}", Thread.currentThread());
// VirtualThread[#81]/runnable@ForkJoinPool-1-worker-3

2Which Async Code Becomes Unnecessary

The test is simple: why does this async code exist? If a controller returns a Callable only so that a blocking repository or HTTP call runs on another thread, then with virtual threads the blocking call no longer wastes a scarce thread, and the Callable adds nothing but complexity. The same applies to DeferredResult used only to wrap a CompletableFuture from a thread pool.

Java
// Before: async only to free the Tomcat thread
@GetMapping("/quote")
Callable<Quote> quote() {
return () -> pricingClient.fetch(); // blocking HTTP call, 800 ms
}
// After: plain blocking code on a virtual thread
@GetMapping("/quote")
Quote quote() {
return pricingClient.fetch(); // virtual thread unmounts while waiting
}

But not every async use is about threads. A DeferredResult that waits for a Kafka event in a long poll, or an SseEmitter that streams updates for minutes, models a request whose answer arrives later from somewhere else. Those still make sense, although with virtual threads we could also block a virtual thread on a queue. We decide case by case.

3Pinning and Older JDKs

A virtual thread is pinned when it cannot unmount from its carrier while blocking. On Java 21 to 23, blocking inside a synchronized block or method pinned the carrier, and so did some native calls. With only as many carriers as CPU cores, a few pinned threads waiting on a slow database could stall the whole server. JEP 491 in Java 24 removed pinning for synchronized, so on Java 25 LTS this is mostly gone, though native frames and some class initialisation cases can still pin.

So the evaluation depends on the JDK. On Java 21 we run with -Djdk.tracePinnedThreads=full or watch the jdk.VirtualThreadPinned event in Java Flight Recorder, and we check libraries we depend on, such as older JDBC drivers and HTTP clients that synchronize around I/O. Most popular libraries replaced such blocks with ReentrantLock. On Java 25 we still look at the JFR events, but we expect far fewer.

4The New Bottleneck Moves Downstream

With 200 platform threads, Tomcat itself limited concurrency to 200 requests. That limit also protected everything behind the service. With virtual threads, 5,000 requests can be in progress at once, and they all try to use the same resources.

The most common victim is the database connection pool. HikariCP's default is 10 connections. 5,000 virtual threads waiting for 10 connections will queue on the pool, and after connectionTimeout (30 seconds by default) they fail. Downstream services can also be overwhelmed by a sudden increase in parallel calls. So we add explicit limits where they belong: a sized connection pool, a semaphore or bulkhead around a fragile downstream, and timeouts on every outbound call. Memory changes too, because each in-flight request holds its objects longer; we watch heap usage under load.

Step throughWhere requests wait after the switch
  1. Requests arriveNo thread pool limit at Tomcat
  2. Virtual thread per requestCheap; thousands at once
  3. Need a DB connection?Pool of 10 to 50
    YesWait in pool queueNoCall downstream
  4. Pool queue growsTimeouts after 30 s by default
  5. Add limits and timeoutsBulkheads, pool sizing

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

Virtual threads remove the front gate; the next narrow point becomes the limit.

5Load Testing Before and After

The decision should come from measurement. We pick a realistic workload, for example the production mix at two and four times peak, and run it against the current async build and against the virtual thread build with the async code removed. We compare throughput, p99 latency, error rate, heap use and CPU. We also watch pool wait time in Hikari metrics and pinning events in JFR.

A typical result for an I/O-bound service is similar or better throughput with simpler code, and a new failure point at the connection pool that we fix with sizing and limits. If a service is CPU-bound, virtual threads give little benefit, because the CPU, not the thread count, is the limit.

6What I Would Decide

I would enable virtual threads first without removing any async code, run the load test, and fix pinning and pool problems. Then I would remove async wrappers whose only job was to free threads, one endpoint at a time, with the test repeated. I would keep DeferredResult, SseEmitter or a reactive stack for long-held streaming connections where the existing code already works well. And I would check thread-local based code such as MDC logging and security context, which behaves better with plain blocking code than it did with async handoffs.

7Follow-up questions

Does Tomcat's server.tomcat.threads.max still matter with virtual threads?

Not as a concurrency limit. With virtual threads enabled, Tomcat uses a virtual thread per request instead of its bounded platform pool, so that setting no longer caps concurrent requests. Connection limits such as max-connections still apply, and limits should now be set explicitly on the resources behind the service.

Should we pool virtual threads?

No. Virtual threads are cheap to create and are meant to be one per task. Pooling them brings back the old limits for no gain. If we need to limit concurrency, we use a semaphore around the resource being protected.

Is ThreadLocal safe with virtual threads?

It works, but each virtual thread has its own copy, so caching expensive objects in a ThreadLocal per thread no longer reuses them; with thousands of threads, it can also use a lot of memory. Context such as MDC still works as long as it is set and cleared per request.

8Conclusion

In this answer, we saw that virtual threads make blocking code cheap, so async servlet code that existed only to free threads can often be removed after Spring Boot's spring.threads.virtual.enabled switch. We must check pinning, which was a real problem with synchronized before Java 24, and find the new bottleneck, usually the connection pool or a downstream service. Load tests before and after decide the change, and async remains useful for long-held streaming connections.

What to say in the interviewAbout 80 seconds aloud

Open with

First I would ask why each piece of async code exists. Most Callable and DeferredResult handlers in typical services exist only so a slow blocking call does not hold one of Tomcat's 200 threads, and virtual threads solve exactly that: on Java 21 or later with Spring Boot 3.2 or later, spring.threads.virtual.enabled=true runs each request on a virtual thread, and when it blocks on I/O the JVM unmounts it.

Then, if they want more

So that async code can go. Before deleting anything I would check three things. Pinning: on Java 21 to 23, blocking inside synchronized pins the carrier thread, so I would check the libraries and watch JFR pinning events; Java 24 and 25 fixed the synchronized case.

The new bottleneck: Tomcat no longer limits concurrency, so thousands of requests can hit a Hikari pool of ten connections or a fragile downstream, so I would size pools and add bulkheads and timeouts. And thread-local context like MDC, which is usually simpler with blocking code.

Then I would load test the current build and the virtual thread build at two and four times peak, comparing p99 latency, errors and memory. I would remove async code endpoint by endpoint, and keep DeferredResult or SSE for long-held streaming connections that already work.

About this answer

Last updated
12 November 2026
Versions it names
Java 21, Spring Boot 3.2, 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
Jakarta EE specifications
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 "Your team wants to replace async servlet code and DeferredResult handling with virtual...": https://scrivio-interview-library.netlify.app/java/servlets/562822646f/