Sub topic 1
How Spring MVC handles a request today
Spring MVC uses a thread-per-request model. Each incoming HTTP request gets one thread, and that thread does all the work for the request. This chapter walks through that flow. The later chapters build on it.
Prerequisites
This chapter assumes you have:
- Java 21 basics, including
ExecutorServiceandjava.util.concurrent. - A working Spring Boot 3.2 or later MVC application running on Tomcat or Jetty.
- A basic understanding of blocking I/O, such as JDBC and HTTP clients.
- Familiarity with
application.propertiesorapplication.yml.
The life of one request
A Spring Boot MVC application embeds a servlet container, which is Tomcat by default. The container owns a pool of worker threads. When a request arrives, the container follows these steps:
- A small number of acceptor and poller threads read the connection and detect that a request is ready.
- The container hands the request to a worker thread from its pool.
- That worker thread runs the whole servlet chain: filters, then Spring's
DispatcherServlet, then your controller. - Your controller calls services, which call a database or another HTTP service.
- The controller returns, the response is written, and the worker thread goes back to the pool.
The important detail is step 4. While the query runs, the worker thread does nothing. It waits, and it holds the request's state on its stack the whole time. This is what "blocking" means in practice: the thread is occupied but not working.
Seeing it in code
The controller below sleeps for 500 ms to stand in for a slow database call. It returns the name of the thread that handled the request.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class SlowController {
@GetMapping("/slow")
String slow() throws InterruptedException {
Thread.sleep(500);
return "handled by " + Thread.currentThread().getName();
}
}Call /slow and you will see a name like http-nio-8080-exec-3. That is a Tomcat worker. Two requests at the same moment use two different workers. A request that sleeps keeps its worker for the full 500 ms.
Why this model is popular
The thread-per-request model keeps code simple:
- Code reads top to bottom. You call a method and get its result.
- Stack traces show the whole call path.
- A debugger steps through a request naturally.
- Thread-bound features such as
ThreadLocal, transaction context, and the security context work without extra effort.
Most Spring libraries, including JDBC, JPA, and RestClient, assume that blocking is fine.
The pool has a size
The worker pool is finite. In Tomcat the default maximum is 200 threads. You can change it:
server.tomcat.threads.max=200If all 200 workers are blocked on slow calls, the next request waits in a queue. The CPU may be nearly idle during this wait, because the workers are waiting for I/O. Throughput is then limited by the pool size, not by the hardware.
You can estimate the limit with a simple rule. If each request blocks for 500 ms and you have 200 workers, the server completes at most about 400 requests per second, whatever the CPU can do (200 workers divided by 0.5 seconds). The next chapter looks at why simply raising the pool size has costs.
Key points
- One request uses one worker thread for its whole duration.
- Blocking calls leave the worker idle but unavailable to other requests.
- The worker pool size sets the ceiling on concurrent requests.
Exercise
A Spring MVC app on Tomcat uses the default 200 worker threads. Each request spends 250 ms blocked on a database call and almost no time on the CPU. About how many requests per second can it complete at most, and what limits it?
Show answer
About 800 requests per second (200 workers divided by 0.25 seconds). The limit is the worker pool size, not the CPU, because the workers spend most of their time waiting on the database.