Sub topic 2
Why platform threads become the bottleneck
This chapter assumes you know Java 21 basics, including ExecutorService, and that you have a Spring Boot 3.2+ MVC application on Tomcat or Jetty. It also assumes you know what blocking I/O is (JDBC, HTTP clients) and can edit application.properties. The previous chapter showed that Spring MVC gives each request its own thread. This chapter shows why that model stops scaling.
What a platform thread is
A platform thread is the ordinary Java Thread. Each one is a thin wrapper around one operating system (OS) thread. The OS owns its stack, schedules it onto CPU cores, and preempts it when its time slice ends.
That one-to-one mapping makes platform threads simple, and it also makes them expensive. The JVM cannot make a platform thread cheaper than the OS thread beneath it.
The costs of a platform thread
Four costs matter for a web application.
| Cost | What happens | Typical size |
|---|---|---|
| Stack memory | The JVM asks the OS to reserve a fixed stack for every thread. | About 1 MB of address space by default on 64-bit Linux (`-Xss` or `-XX:ThreadStackSize`) |
| Creation time | The OS must allocate the thread and its structures before it can run. | Tens of microseconds, much more than creating an object |
| Context switching | The kernel saves one thread's registers and loads another's. Caches go cold. | Microseconds per switch, rising with the number of runnable threads |
| OS limits | The kernel caps threads per user and per system. | Set by `ulimit -u` and `/proc/sys/kernel/threads-max` |
The stack is reserved, not fully committed. The OS hands out physical memory only as the thread touches its stack pages. So 10,000 idle threads do not necessarily use 10 GB of RAM. They still reserve about 10 GB of address space, and each thread commits at least a few tens of kilobytes. Deep call stacks, which are common in Spring with its proxies, filters and interceptors, commit much more.
The numbers vary by OS and JVM flags. The point is the shape: each thread has a fixed overhead, so the number of threads you can afford is small compared with the number of connections a modern server can hold open.
What a blocked thread does
Consider a typical handler:
- Parse the request.
- Call the database and wait.
- Call another HTTP service and wait.
- Build the response.
The CPU work in steps 1 and 4 may take a millisecond. Steps 2 and 3 may take 100 ms or more. During the waits the thread does nothing. It holds its stack and its place in the pool, but it cannot serve anyone else.
So in a blocking application, the thread is occupied for the whole duration of the request, not just the time it spends on the CPU. A thread that is 99% idle still counts as one of your scarce threads.
The default limits in Spring Boot
Spring Boot's embedded Tomcat uses a bounded pool of request threads. These are the defaults that matter:
# Maximum request-processing threads (default 200)
server.tomcat.threads.max=200
# Connections the OS may queue when all threads are busy (default 100)
server.tomcat.accept-count=100
# Connections Tomcat keeps open at once (default 8192)
server.tomcat.max-connections=8192With these defaults, Tomcat can keep thousands of connections open, but only 200 requests make progress at a time. Request 201 waits in a queue. If the queue fills, new connections are refused or time out.
The database pool adds a second, smaller limit. HikariCP defaults to 10 connections:
spring.datasource.hikari.maximum-pool-size=10That limit is deliberate and often correct, because databases perform badly with too many concurrent queries. Chapter "Protecting databases and downstream services" returns to this. For now, note that you can have 200 request threads and 10 connections, and 190 threads may wait for a connection.
Throughput is bounded by pool size and latency
Little's law relates three quantities: the average number of requests in the system equals throughput multiplied by average time in the system. Rearranged for a blocking server:
max throughput = number of threads / average latencyWith 200 threads:
| Average latency | Maximum throughput |
|---|---|
| 50 ms | 4,000 requests/s |
| 200 ms | 1,000 requests/s |
| 2 s | 100 requests/s |
The last row is the dangerous one. Suppose a downstream service slows from 200 ms to 2 s. Your CPU is nearly idle, because threads are only waiting. Yet throughput drops by a factor of ten, queues grow, and users see timeouts. Looking at CPU graphs, you would see nothing wrong.
Seeing the effect in code
This program runs 2,000 tasks, each a 100 ms blocking wait, on a pool of 200 platform threads. It stands in for 2,000 requests that each spend 100 ms in a database call.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class PoolDemo {
public static void main(String[] args) throws Exception {
int tasks = 2_000;
long start = System.nanoTime();
try (ExecutorService pool = Executors.newFixedThreadPool(200)) {
for (int i = 0; i < tasks; i++) {
pool.submit(() -> {
Thread.sleep(100);
return null;
});
}
} // close() waits for all submitted tasks to finish
long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
System.out.println(tasks + " tasks took about " + millis + " ms");
}
}Run it with java PoolDemo.java. Expect roughly 1,000 ms. The tasks run in ten "waves" of 200, and each wave takes 100 ms. No CPU is busy, since the work is sleeping. The pool size alone sets the time. Change 200 to 50 and the time grows to about 4,000 ms.
The usual fix, and why it stops working
The obvious response is to raise server.tomcat.threads.max. This helps up to a point, then hurts:
- Memory. Each added thread reserves stack space and commits some of it. Heap pressure from per-request objects held while blocked adds to this.
- Scheduling overhead. With thousands of runnable threads, the kernel spends more time switching and less time running your code. Latency becomes less predictable.
- Garbage collection. The collector must scan every thread's stack for roots. More threads mean longer pauses in some collectors' root-scanning phases.
- Hard limits. Eventually thread creation fails with
java.lang.OutOfMemoryError: unable to create native thread. The cause is often an OS limit, not heap. - Downstream overload. More threads send more simultaneous calls to a database or service that was already struggling. You move the bottleneck and may make it worse.
Teams commonly respond by rewriting code in an asynchronous or reactive style, which frees threads during waits. That works, but it changes how you write, debug and test code. A later chapter compares that approach with virtual threads.
Common mistakes when diagnosing this
- Reading low CPU as spare capacity. In a blocking server, low CPU with high latency usually means threads are all waiting. Check pool utilization, not just CPU.
- Tuning the pool without measuring. Look at active thread count (for example
tomcat.threads.busyin Micrometer) and queue behavior before changing limits. - Assuming a bigger pool helps CPU-bound work. If threads compute rather than wait, more threads than cores only add switching overhead. Only waiting-heavy work benefits from more concurrency.
- Forgetting the second pool. A request thread that needs a database connection is limited by the connection pool as well as the thread pool.
What this sets up
The core problem is a mismatch. Waiting is cheap for the hardware, since a network wait uses almost no CPU. But in the thread-per-request model, each wait costs an expensive OS thread. You want the simple blocking style of programming and the ability to hold tens of thousands of concurrent waits. Platform threads give you the first but not the second.
Virtual threads, introduced next, keep the blocking style and remove the per-thread cost, so a wait no longer ties up a scarce OS thread.
Exercise
A Spring Boot MVC service uses the default 200 Tomcat threads. Each request makes one downstream call that normally takes 100 ms and uses almost no CPU. (a) What is the approximate maximum throughput? (b) The downstream service degrades to 1 second per call. What happens to throughput and CPU usage? (c) Why might raising server.tomcat.threads.max to 2000 be a poor fix?
Show answer
(a) Throughput is threads divided by latency: 200 / 0.1 s = about 2,000 requests per second. (b) Throughput falls to 200 / 1 s = about 200 requests per second, a tenfold drop, while CPU usage stays low because the threads are only waiting. Requests queue up and users see timeouts. (c) Ten times more threads reserve about ten times more stack memory, raise context-switching and garbage-collection root-scanning costs, may hit OS thread limits, and send up to ten times more simultaneous calls to a downstream service that is already slow, so the problem can get worse.