Sub topic 2
Why platform threads become the bottleneck
This chapter assumes you know how Spring MVC assigns one thread to each request. It also assumes you are comfortable with blocking calls such as JDBC and RestClient. Here you will see why that model hits a ceiling, and you will learn to estimate where the ceiling is.
What a platform thread is
A platform thread is the ordinary Java Thread. The JVM maps each one to one operating system (OS) thread. The OS schedules it, and the JVM cannot make it cheaper than the OS allows.
Each platform thread has a cost:
- Stack memory. The JVM reserves a stack for every thread. On 64-bit Linux the default is typically 1 MB of address space, and the OS commits pages as the stack grows.
- Creation time. Starting an OS thread is a system call. Applications therefore reuse threads through pools instead of creating one per task.
- Context switching. When thousands of threads compete for a few CPU cores, the OS spends time saving and restoring thread state.
A server can hold thousands of platform threads, but not hundreds of thousands. The practical limit depends on memory, OS settings, and how much the threads actually do.
Why blocking makes it worse
In Spring MVC on Tomcat, a request keeps one worker thread from start to finish. When your code calls a database or another HTTP service, the thread blocks. It does no work and the CPU is free, but the thread stays reserved until the response arrives.
Most request time in a typical service is spent waiting on I/O (input/output, such as network calls), not computing. The thread count then becomes the limiting resource, even while CPU use is low.
Tomcat's default is 200 worker threads:
server.tomcat.threads.max=200Request 201 waits in a queue until a worker is free.
Estimating the ceiling
Little's law gives a quick estimate:
max throughput = threads / average time a thread is held per requestWith 200 threads and a request that holds its thread for 100 ms:
- 200 / 0.1 s = 2,000 requests per second.
If a downstream service slows down and each request now holds its thread for 500 ms:
- 200 / 0.5 s = 400 requests per second.
Nothing in your application changed, yet capacity dropped by 80 percent. The extra requests queue, latency climbs, and timeouts follow.
The usual workarounds and their limits
Teams commonly respond in one of these ways:
- Raise the thread count. This helps until memory pressure and context switching grow. It also moves the overload downstream, for example onto a small database connection pool.
- Add instances. This works but costs money. Each instance still sits mostly idle while it waits.
- Rewrite as asynchronous or reactive code. This frees threads during waits, but it changes the programming model, the libraries, and how you debug.
All three treat the symptom. The underlying problem is that one blocked request ties up one expensive OS thread. Later chapters show how virtual threads address that cost without changing your blocking code style.
Checking whether you are thread-bound
Signs that worker threads are the bottleneck:
- Latency rises while CPU use stays low.
- Tomcat's busy-thread count stays at its maximum.
- Requests queue even though the database and downstream services respond normally under light load.
If instead the CPU is saturated, adding threads of any kind will not help.
Exercise
A Spring MVC service uses Tomcat with 200 worker threads. Each request holds its thread for an average of 250 ms, mostly waiting on a database. What is the approximate maximum throughput, and what would you expect the CPU to be doing at that limit?
Show answer
About 200 / 0.25 s = 800 requests per second. The CPU would be mostly idle, because the threads spend most of their time blocked on I/O. The thread count is the bottleneck, not processing power.