Skip to content

Virtual Threads for the Web App

Should the storefront (web) serve requests on virtual threads (spring.threads.virtual.enabled=true) instead of Jetty's platform-thread pool? This page records the load test that answered it (November 2026) and what changed as a result.

Short answer: yes, but only together with a fair admission limit. On the same 2 CPUs, under overload, virtual threads with the limit served 16-41% more shop traffic than today's pool and cut its p99 by 36-54% (5% less crawler traffic got through, but shoppers were answered faster). Without the limit, virtual threads queue requests past Jetty's 200-task cap in no particular order, and in every overloaded run 141 to 1,291 requests waited 30 seconds or more while later ones were served. RequestAdmissionFilter puts a first-in, first-out queue with a bounded wait back in front, and VirtualThreadsJettyConfiguration lifts Jetty's cap so that queue is the only one. Both are off unless BADGER_VIRTUAL_THREADS=true. See Recommendation.

What the setting changes

Today nothing sets spring.threads.virtual.enabled or server.jetty.threads.*, and nothing in web-mvc or commerce-core customises Jetty's thread pool (JettyErrorConfiguration only installs an error handler). So Jetty runs a QueuedThreadPool: at most 200 platform threads, and further requests wait in its first-in, first-out job queue.

With the setting on, Spring Boot 4.1 replaces that pool with Jetty 12.1's VirtualThreadPool:

  • every task (selectors and acceptors as well as requests) runs on its own virtual thread, over a carrier pool as large as the CPU count (2 on a production pod);
  • it still allows only 200 tasks at once (server.jetty.threads.max, default 200), but the rest wait on a non-fair Semaphore. A waiting request can lose the race to newer ones again and again: there is no queue order any more (this branch replaces that pool when the admission filter is on, see Recommendation);
  • @Scheduled jobs and RabbitMQ listener containers also move to virtual threads. AsyncConfig's executor (the event multicaster's, which runs start-up listeners) stays on platform threads: it sets setVirtualThreads(false) explicitly, because of the start-up deadlock described in its comment.

How it was measured

  • Build: web from development (14d1a1481b) in the production image (web/Dockerfile: AOT cache, G1, compact object headers, MaxRAMPercentage=60), production profile.
  • Limits like a production pod: 2 CPUs and 1536 MiB for the web container. Mongo 8 and Typesense 29 each had 2 CPUs; Valkey and RabbitMQ unlimited; all on their own Docker network, apart from any dev stack.
  • Data: the Halden demo (210 products, indexed in Typesense, faceted category tree). Listing traffic guard on with its defaults (40 listings, 16 filtered, 100 ms wait).
  • Slow downstream: Mongo and Typesense were reached through toxiproxy, which added latency on demand: +25 ms (±6) per Mongo reply, or +300 ms (±75) per Typesense reply.
  • Load: k6 in a container, closed model (each virtual user sends its next request as soon as the last one answers, no think time), 30 s client timeout, 45 s per measurement after a warm-up.
  • Shop mix: home 15%, category 25%, product 35%, search 15%, add to basket 10%.
  • Crawl mix: 80% faceted /c/** URLs (1-4 random facets, sometimes a sort or a page, a different CF-Connecting-IP each time, as a distributed crawler would send), 20% shoppers (product page or add to basket), whose latency is reported separately.
  • Noise: daytime runs shared the machine with CI jobs using 1 to 5.6 of Docker's 10 CPUs, and running one mode after the other gave numbers that moved by up to 4x between rounds. The results below are paired runs made in the evening, with CI idle (0% CPU in every block of the main table): all variants up at once, the load moved from one to the next in 45 s blocks, the order rotated each repetition, three repetitions, medians reported. The daytime runs showed the same ranking.

Throughput below counts successful (2xx/3xx) responses only. Shed requests (503 from the listing guard or the admission filter, 429 from the per-client throttle) are counted separately, and so are requests the client gave up on after 30 s.

Results

Paired comparison (main result)

Medians of three 45 s blocks per variant; throughput is successful responses per second. Timeouts are requests the client gave up on after 30 s (total over the three blocks). Shed is 503 and 429 answers, almost all from the listing guard (the admission filter shed nothing in these runs).

Load Variant req/s p50 ms p95 ms p99 ms Shed Timeouts
Shop mix, 400 users Platform, 200 threads (today) 110 2,771 5,805 7,338 1,273 0
Virtual 146 1,810 3,693 19,837 1,824 176
Platform, 50 threads 114 4,418 5,108 6,453 0 0
Virtual + admission 155 2,637 3,100 3,387 0 0
Shop mix, 800 users Platform, 200 threads (today) 130 5,100 7,974 10,802 1,505 0
Virtual 159 1,570 26,921 28,895 1,958 1,291
Platform, 50 threads 141 7,288 8,411 10,768 0 0
Virtual + admission 162 5,382 6,587 6,908 0 42
Shop mix, 400 users, Mongo +25 ms Platform, 200 threads (today) 130 2,607 4,666 5,814 1,000 0
Virtual 148 1,438 3,095 28,934 1,507 369
Platform, 50 threads 130 4,085 5,148 5,636 0 0
Virtual + admission 151 2,674 3,375 3,716 0 0
Crawl, 400 users, Typesense +300 ms Platform, 200 threads (today) 165 466 1,508 2,236 23,334 0
Virtual 195 312 927 1,405 26,872 141
Platform, 50 threads 129 1,332 1,703 1,885 11,819 0
Virtual + admission 156 714 1,032 1,137 17,055 0

In the crawl, the shoppers (product pages and add to basket, 20% of the traffic) saw:

Variant Shopper p50 ms Shopper p95 ms
Platform, 200 threads (today) 1,068 2,259
Virtual 695 1,222
Platform, 50 threads 1,251 1,472
Virtual + admission 640 799

Admission here means max-concurrent 64, queue-wait-ms 10,000 and Jetty's cap raised to 10,000.

What the table says:

  • Virtual threads do more work per CPU. Every virtual-thread variant beat today's pool on throughput, by 14-41%, except the admission variant in the crawl (156 against 165, see below). Resource use shows where the platform pool loses it.
  • On their own they're unfair. Plain virtual threads had the best medians and the worst tails: 141 to 1,291 requests in every overloaded case waited more than 30 s, while others were answered in a second. That's Jetty's non-fair semaphore: a thread dump at 400 users had 651 virtual threads parked on it.
  • The admission filter only works with Jetty's cap lifted. A first attempt kept Jetty's cap at 200, so 200 requests reached the filter's fair queue and the rest still waited on Jetty's semaphore: 292 timeouts at 400 users, worse than plain virtual threads. With the cap lifted, the filter's queue is the only one, and the p99 was the best of all four variants in every case.
  • Shrinking the platform pool to 50 removes the platform pool's waste only in part: no timeouts, tight tails, but throughput only 0-8% above today's, and 22% below it with slow Typesense, where 50 threads aren't enough to cover the waiting.
  • The crawl case. With admission, fewer crawler requests got through (17,055 shed by the listing guard against 23,334 today, and 9 req/s fewer served in all), because 64 requests in flight leave the guard's 16 filtered-listing places busier. Shoppers did better than in any other variant. That is the trade the listing guard exists to make.
  • The 42 timeouts at 800 users with admission (about 0.3 a second, against 1,291 for plain virtual threads) didn't come from the filter: it refused nothing, and a request waits there for 10 s at most. They're unexplained; the connection-level work (accepting and reading the request) happens before the filter and is the likeliest place.

The switch as shipped. Rebuilt with the configuration below, one pod with nothing set and one with only BADGER_VIRTUAL_THREADS=true, two paired blocks each:

Load Variant req/s p50 ms p95 ms p99 ms Shed Timeouts
Shop mix, 400 users Nothing set (today) 118 2,611 5,186 6,451 1,296 0
BADGER_VIRTUAL_THREADS=true 139 2,916 3,736 4,183 0 0
Shop mix, 800 users Nothing set (today) 124 5,584 8,697 11,721 1,347 0
BADGER_VIRTUAL_THREADS=true 139 6,402 7,181 7,885 0 0

At 100 users (below the pool limit) the variants were within the noise of each other (102-124 req/s, CI busy at the time).

Resource use (sequential rounds)

From two daytime rounds of each mode at 50 to 800 users, before the paired runs. Their throughput was too noisy to compare, but these differences held in every run.

Platform threads (today) Virtual threads
Live JVM threads 111 at 50 users, 255 from 200 users 44-46 at any load
Heap used (max during a run) 440-830 MiB 530-810 MiB
Container memory (max) 1.1-1.5 GiB of 1.5 1.2-1.5 GiB of 1.5
Redis command time, max per run (Lettuce) 1.3-5.3 s 0.16-0.63 s
GC pause time per 45 s run 2.6-15.2 s 1.7-3.3 s
Mongo connections checked out (pool of 100) up to 98 with slow Mongo up to 100 with slow Mongo
Mongo pool checkout failures 0 0

With 200 runnable platform threads on 2 CPUs, the threads that don't serve requests (Lettuce's event loop, the Mongo driver's) wait their turn too, so every Redis cache read gets slower, and the collector has 200 deep stacks to scan. On virtual threads only the two carriers compete with them.

A thread dump taken from the virtual-thread run at 400 users (jcmd 1 Thread.dump_to_file -format=json) showed 890 virtual threads: 651 waiting on the VirtualThreadPool semaphore, 188 waiting for a Redis reply (CircuitBreakingRedisCacheWriter get and put), and about 40 doing anything else. More requests were waiting than there were clients, because a request whose client had given up stays queued until it gets a place.

Pinning

JFR's jdk.VirtualThreadPinned event, with the threshold at 0 ms, over a minute at 400 users:

Run Pinned events Total time pinned p95 Max What pinned
Shop mix 121 333 ms 9 ms 33 ms class loading (BuiltinClassLoader.loadClassOrNull, ZipFile.getEntry)
Shop mix, Mongo +25 ms 144 287 ms 4 ms 45 ms the same

No synchronized block pinned anything (since JDK 24 a virtual thread blocked in one releases its carrier). The pinning left is the first load of a class, which is rare once the app is warm, but it is what turned the start-up deadlock into a hang.

Start-up

Twenty starts with virtual threads on, each with the worker running and a 100-user crawl hitting the pod while it started: 20 of 20 became healthy (19-113 s, median 43 s), and none hung. That rules out a hang rate much above 15% (at 95% confidence), not a rare one. In 4 of the 20 starts the first product page took more than 30 s once the pod was healthy: that was the crawl getting ahead of it in Jetty's unordered queue, not a hang.

Recommendation

Switch, with the admission filter, and turn it on per environment. The branch that carries this page does so behind one switch, off by default:

# web, application.yml
spring.threads.virtual.enabled: ${BADGER_VIRTUAL_THREADS:false}
badger.request-admission:
  enabled: ${BADGER_REQUEST_ADMISSION_ENABLED:${spring.threads.virtual.enabled}}  # follows it
  max-concurrent: 64          # BADGER_REQUEST_ADMISSION_MAX_CONCURRENT
  queue-wait-ms: 10000        # BADGER_REQUEST_ADMISSION_QUEUE_WAIT_MS
  jetty-max-tasks: 10000      # BADGER_REQUEST_ADMISSION_JETTY_MAX_TASKS

BADGER_VIRTUAL_THREADS=true on a deployment turns on virtual threads, the admission filter and the raised Jetty cap together. Don't turn virtual threads on with the filter off: that is the variant with the 30 s timeouts.

Guard rails that come with it:

  • RequestAdmissionFilter (web-mvc, render/filters, order HIGHEST_PRECEDENCE + 4, ahead of the listing guard and tenant resolution): 64 requests in flight, the rest wait in arrival order for up to 10 s, then a 503 with Retry-After: 5 and the listing guard's traffic.busy text. Metrics: badger_request_admission_in_flight, badger_request_admission_waiting, badger_request_admission_rejected_total.
  • VirtualThreadsJettyConfiguration (web-mvc, server): only on virtual threads with the filter on, replaces Boot's VirtualThreadPool (cap 200, non-fair) with one capped at 10,000, so the filter is the only queue. On platform threads it doesn't exist, so the raised cap can never become a 10,000-thread platform pool.
  • 64 is below the Mongo pool (100 per pod), so a request never waits for both a place and a connection; no run had a Mongo checkout failure. It is above the listing guard's 40, so listings keep their own limits inside it.
  • Health checks already use the management port, which has its own Jetty and isn't behind the filter.

Before turning it on in production:

  1. Turn it on for the dev web deployment and compare a day of http_server_requests p95/p99, badger_request_admission_*, CPU and memory with the day before. Watch for 503s from the filter with normal traffic: that would mean 64 is too low.
  2. The start-up hang was not reproduced (0 of 20), which rules out a frequent one but not a rare one. Keep the startup probe; a pod that hangs is restarted.
  3. Only web was tested. rest and worker run other code (OAuth, AMQP consumers, batch jobs) and need their own measurement before the same switch.

Reproducing it

The scripts that ran this live outside the repository. To repeat it, build the production image, start the dependencies on an internal network with toxiproxy in front of Mongo and Typesense, seed with -Dspring.profiles.active=dev,seed-demo -Dseed.demo.sites=halden, run the worker once to index Typesense, and start each variant with JAVA_OPTS:

Variant JAVA_OPTS
Platform, 200 threads (today) (none)
Platform, 50 threads -Dserver.jetty.threads.max=50
Virtual, no admission -Dspring.threads.virtual.enabled=true -Dbadger.request-admission.enabled=false
Virtual + admission (as shipped) -DBADGER_VIRTUAL_THREADS=true (or the environment variable)

To look for pinning on a running pod, start it with -XX:StartFlightRecording=name=vt,settings=profile,jdk.VirtualThreadPinned#threshold=0ms and dump the recording with jcmd 1 JFR.dump name=vt filename=/tmp/vt.jfr from a JDK container that shares the pod's process namespace (the runtime image has no jcmd). -Djdk.tracePinnedThreads no longer exists.