Sub topic 1
How Spring MVC Handles a Request Today
Prerequisites
You should know Java 21 basics, ExecutorService, blocking I/O, and Spring Boot configuration. This chapter assumes a Spring Boot 3.2 or later MVC application using Tomcat or Jetty.
The Thread-Per-Request Model
Spring MVC commonly handles each incoming HTTP request on one platform thread. A platform thread is an operating-system thread managed through the JVM. The thread enters the controller, calls services, waits for a database or HTTP response, and then writes the response.
During blocking I/O, the thread is mostly waiting. It cannot handle another request while it waits.
sequenceDiagram
participant C as Client
participant S as Spring MVC
participant T as Platform thread
participant D as Database
C->>S: HTTP request
S->>T: Assign request
T->>D: Blocking query
D-->>T: Result
T-->>C: HTTP responseWhy Platform Threads Become the Bottleneck
Platform threads consume substantial memory and scheduling resources. Applications therefore use a bounded thread pool. The pool protects the process, but it also limits the number of requests that can wait concurrently.
For example, a pool with 200 threads can actively wait on roughly 200 blocking operations. Additional requests queue, even when the CPU is not busy.
| Model | Waiting request | Main limit |
|---|---|---|
| Platform thread per request | Holds an OS-backed thread | Thread count and memory |
| Virtual thread per request | Can release its carrier while waiting | Downstream capacity |
A larger platform-thread pool may increase concurrency briefly, but it also increases memory use and scheduling overhead. It does not remove limits such as database connections.
> callout note > Virtual threads improve how the application waits. They do not make a database or remote service faster.
What a Virtual Thread Is
A virtual thread is a lightweight Java thread managed by the JVM rather than directly represented by one operating-system thread. Java can create many virtual threads, so blocking application code can remain straightforward while supporting more concurrent waiting.
Carrier Threads: How Virtual Threads Run
A carrier thread is a platform thread that temporarily runs a virtual thread. When the virtual thread reaches supported blocking I/O, the JVM can unmount it from the carrier. The carrier then runs another virtual thread. When the I/O completes, the JVM schedules the waiting virtual thread again.
This is cooperative scheduling inside the JVM, not a larger platform-thread pool.
Turning on Virtual Threads in Spring Boot
With Java 21 or newer and Spring Boot 3.2 or later, enable virtual threads with one property:
spring.threads.virtual.enabled=trueSpring Boot then uses virtual threads for its auto-configured application task executor and the embedded web server when the server supports them. Existing blocking MVC controllers can usually remain unchanged.
Verify the result from a controller:
@GetMapping("/thread")
String thread() {
Thread current = Thread.currentThread();
return "virtual=" + current.isVirtual();
}A response containing virtual=true confirms that this request runs on a virtual thread.
Exercise
Why does increasing a platform-thread pool not fully solve blocking I/O concurrency?
Show answer
Each waiting request still occupies a platform thread, so memory, scheduling, and downstream resource limits remain.