Why Does the Server Lock Up on Sale Day, Even With the CPU Half Idle?

The promotional email went out at 10:00. By 10:02 the checkout page stops loading, and by 10:05 the customer service phone won't stop ringing. The tech team looks at the server dashboard and sees something odd: the CPU is at 49 percent. Half the machine is idle, yet the site isn't responding to anyone.

The answer to this contradiction lies in where a web request spends its time. We built a small server model, sent it anywhere from 250 to 3,000 requests per second, and measured three different setups side by side. The results show why the "let's get a bigger server" reflex often doesn't help.

135 Milliseconds in the Life of an Order

When a customer clicks "Complete order," the server actually does very little computing. In our model, an order request goes like this:

  • 5 milliseconds of CPU: reading the request, validating it, preparing the response.
  • 10 milliseconds of database: decrementing the stock, saving the order.
  • 80 milliseconds of payment provider: waiting for the bank's "approved" response.
  • 40 milliseconds of SMS service: hearing back that the confirmation text was sent.

Only 5 of the total 135 milliseconds, or 3.7 percent, is actual work. The remaining 96.3 percent is waiting. For almost all of that time, the server is waiting on someone else's response.

The Waiter Is Standing at the Kitchen Window

Picture a restaurant. A waiter goes to a table, takes the order, carries it to the kitchen, and stands at the kitchen window until the food is ready. When the dish comes out, they carry it to the table, and only then can they attend to a new table. If this restaurant has 10 tables but 4 waiters, at most 4 tables can be served at once. Six tables sit idle, and the line at the door grows.

A classic web server written in Java works exactly like this. Each incoming request gets a "waiter"; the technical name is a thread. The thread stays tied to that request for its entire lifetime, even while it waits on the payment provider. In Tomcat, Spring Boot's default web server, that number is 200. So: 200 waiters, at most 200 tables at once.

The math is simple: if each request keeps a waiter busy for 135 milliseconds, 200 waiters can handle at most 1,481 requests per second. When that limit is reached, the 16-core server's CPU is only 46 percent busy. The line at the door grows while half the kitchen sits empty.

The same 4 waiters and 10 tables: at the top, the waiters wait at the kitchen window while the food cooks and the line at the door grows; at the bottom, the waiters drop off the order and move on to other tables, and every table fills up
Illustrative scale: the same flow of customers, the same kitchen. The only difference is whether the waiter waits while the food cooks.

Same Server, Three Setups

We kept the model simple enough for anyone to rerun. The server has 16 CPU cores, the database accepts at most 20 simultaneous connections, and the duration of each step varies from request to request, as it does in real life. If a customer doesn't get a response within 2 seconds, the request counts as failed. We applied each load level for 30 seconds and measured the last 20. The sample data is synthetic.

The three setups are: classic with 200 threads, unlimited virtual threads, and virtual threads with a limit. We'll explain what a virtual thread is shortly. First, let's look at the results at the peak of the sale, when 2,250 requests per second are coming in:

SetupAnswered in timeTimed outImmediate "try again"Slowest 5%CPU
Classic, 200 threads0%100%0%over 2 s49%
Virtual, unlimited26.7%73.3%0%1.96 s70%
Virtual, 300 concurrent limit88.4%0%11.6%0.29 s62%

The first row is the moment we described at the start. With half the CPU idle, not a single customer gets a response in time. On average, 4,520 requests are waiting in line at the door.

The second row is the setup where the waiters no longer wait. Things improve, but not enough; roughly three out of four customers still walk away empty-handed.

The third row is the same virtual setup, but with a doorkeeper. 88 percent of visitors get a response in time, and even the slowest 5 percent of those finish in under 0.3 seconds. The rest get an honest "we're busy right now, please try again in a few seconds" message within 50 milliseconds.

On a normal day, at 1,000 requests per second, there's no difference among the three setups: everyone gets a response, and the slowest 5 percent finish in 0.23 seconds. The difference only shows once you go past the limit. That is exactly what sale day is.

Why Does It Collapse So Suddenly?

In the classic setup, at 1,400 requests per second, the slowest five out of every 100 customers wait 0.24 seconds. When traffic rises 25 percent to 1,750, 96 percent of requests time out. At 1,900, not a single successful response is left. The slowdown doesn't creep in gradually; at a certain threshold, it slams shut like a door.

The reason is the nature of queues. If a system that can keep up with 1,481 requests per second receives 1,750, the line at the door grows by about 270 people every second, and that growth doesn't stop on its own. After a while, every customer whose turn comes up has already waited close to 2 seconds. The server processes their order at full speed, but by the time the response is ready, the customer has long since closed the page. The machine is running and the CPU is busy, but it's producing responses that no one is waiting for anymore.

As incoming requests rise from 250 to 3,000 per second, the number of on-time responses from the three setups and the wait time of the slowest 5 percent
Measurements from our model. The gray line collapses around 1,500 and the blue line just above 2,000; the orange line holds steady at 2,000 responses per second.

The Solution: Don't Make the Waiter Wait

In the restaurant, the fix is obvious: the waiter drops the order off at the kitchen and moves on to another table while the food cooks. When the dish is ready, any free waiter carries it to the table. Without changing the number of waiters, the restaurant can serve far more tables.

In the Java world, this is called a virtual thread. It became a permanent part of the platform with Java 21. When a virtual thread starts waiting on a network response or the database, it releases the real thread underneath it, which immediately moves on to another request's work. As a result, thousands of requests on the same server can be "waiting" at the same time. In Spring Boot, turning this on is a one-line setting.

Java's own documentation draws an important distinction explicitly: virtual threads are not faster. They don't finish a single order in less time. What they do is increase the number of customers who can wait at the same time. In our model, this means the slowest 5 percent stays under 0.27 seconds up to 2,000 requests per second.

But the Bottleneck Moves to the Kitchen

We freed up the waiters, but the kitchen still has only 20 burners. The database accepts at most 20 simultaneous connections, and each order uses a connection for 10 milliseconds. That means at most 2,000 orders per second. Now the line forms not at the door but in front of the kitchen.

In the unlimited virtual setup, at 2,250 requests per second, there are on average 4,336 virtual threads open on the server, and 4,065 of them are waiting for one of the 20 connections to free up. A virtual thread does this waiting very cheaply, but waiting is still waiting. The same queue logic applies here, too: at 2,500 requests per second, nobody gets a response in time.

Gauges showing that as load rises from 1,000 to 2,500 requests per second, the 200 threads fill up in the classic setup and the 20-connection database pool fills up in the virtual setup
In the classic setup the threads fill up; in the virtual setup the database pool does. In both cases, there's spare room on the CPU.

This is where the third setup comes in. We put a doorkeeper at the entrance: at most 300 orders can be inside at once. If there's no room, the customer is held for at most 50 milliseconds, and if room still hasn't opened up, they're politely told "please try again in a few seconds." The number 300 isn't arbitrary: the kitchen can turn out 2,000 orders per second and each order stays inside for about 0.135 seconds; multiplying the two gives 270, plus a small margin.

The result is surprisingly calm. Even at 3,000 requests per second, the server completes about 2,000 orders per second on time, and the slowest 5 percent finish in 0.30 seconds. A third of the visitors get an immediate "try again" message. From the business's point of view, the difference is this: instead of a sale where everyone stares at a page spinning for minutes and nobody can place an order, you get a sale where most people shop without a hitch and the rest know what's going on.

Java's documentation recommends the same path: to limit concurrency, rather than putting virtual threads into an old-style pool, you should use a counter designed for the job, technically called a semaphore.

Four Things That Break It in Real Life

1. Enlarging the Connection Pool Isn't the Answer

If there's a line at the kitchen, the first instinct is to add burners. But the database server also has a limited number of cores and disks. Raising connections from 20 to 200 just moves the line inside the database and slows down every query. The documentation for HikariCP, the most widely used connection pool in Java, recommends as a starting point twice the number of cores plus the number of disks; the default is 10.

There's a sneakier mistake, too: holding the database connection open while waiting on the payment provider. That's like the waiter occupying a burner while waiting for the food. In our model, if a connection stays busy for 130 milliseconds instead of 10, 20 connections can keep up with only 154 orders per second. That's nearly a tenth of even the classic setup. This is the first place to look before a sale.

2. External Services Have Limits, Too

Payment and SMS providers are kitchens, too. In the classic setup, at most about 119 requests went to the payment provider at once, because the number of waiters capped it automatically. In the virtual setup, that number rises to 160, and the number of requests per second going to the SMS service rises by 35 percent. If the provider's contract has a per-second limit, the errors no longer start on your server but at their door: "too many requests" responses and declined payments. Each external service needs its own limit, a reasonable timeout, and, when an error comes back, retries spaced out at intervals rather than immediately.

3. Old Libraries Can Nail the Waiter to the Window Again

Some libraries store expensive objects one per thread. With 200 waiters, that's no problem; with 50,000 virtual waiters, memory fills up in an instant. Java's documentation specifically calls attention to this usage (ThreadLocal).

The second trap has to do with the Java version. In Java 21, if code starts waiting while inside an old-style lock (synchronized), the virtual thread can't release the real thread underneath it and locks it up as well. Java 24 eliminated almost all of these cases; only a few exceptions remain, such as callbacks from native code and class loading. The Spring Boot documentation also specifically recommends Java 24 or later for virtual threads.

4. The Metrics You Watch Change

On a classic server, "how many of the 200 threads are busy?" was a good alarm signal. With virtual threads, that number loses its meaning; according to the Spring Boot documentation, the thread pool settings no longer even take effect. In the new era, these are what you need to monitor: the time spent waiting for a database connection, the number of requests turned away at the door, the response time of each external service, and the slowest 5 percent. Before a sale, a load test in which external services are simulated with realistic latencies shows in advance at which threshold these metrics break.

Does Every Business Need This?

No. In our model, at 1,000 requests per second, all three setups produced exactly the same results. If your busiest day is well below that limit, switching to virtual threads won't make a visible difference. If most of the work happens on the CPU, as in a system that generates reports, processes images, or prepares PDFs, virtual threads won't help at all. If the system is still on Java 8, 11, or 17, the real cost is the version upgrade project, which is a much bigger job than a one-line setting.

What's more, in a significant share of systems that lock up under load, the cause isn't the thread count; it's a slow query, a missing index, or a connection held open while waiting on an external service. That's why the right first step isn't changing a setting but measuring where a request's 135 milliseconds go. Virtual threads pay off most in systems that talk to many external services and whose traffic spikes at moments like sales, ticket releases, or payday.

When designing a backend that can handle sale-day load, the threading model, the connection pool, and external service limits need to be considered together. On our Backend and API development page, we outline this approach.

For developers: the one-line setting and the doorkeeper

With Java 21 or later, virtual threads are turned on in Spring Boot with a single line in application.properties. When this setting is on, pool settings such as server.tomcat.threads.max no longer have any effect.

spring.threads.virtual.enabled=true

The doorkeeper can be written with the JDK's java.util.concurrent.Semaphore. TooBusyException here is the application's own class; it can be turned into an HTTP 503 response with a @ControllerAdvice.

private final Semaphore checkoutSlots = new Semaphore(300);

public Receipt checkout(Order order) throws InterruptedException {
    if (!checkoutSlots.tryAcquire(50, TimeUnit.MILLISECONDS)) {
        throw new TooBusyException();
    }
    try {
        return checkoutService.process(order);
    } finally {
        checkoutSlots.release();
    }
}

Long-lasting pinning cases can be monitored with the jdk.VirtualThreadPinned event in JDK Flight Recorder.

Sources

Note: The server model and all measurements in this post are synthetic. The request durations, core count, connection count, 2-second timeout, and limit of 300 are assumptions chosen for this example; the figures come from a computer simulation of this model and are not measurements of a real system. The waiter animation is at an illustrative scale.