Sub topic 14
A checklist for a safe rollout
This chapter assumes you know Java 21 concurrency basics, run a Spring Boot 3.2+ MVC application on Tomcat or Jetty, and have read the earlier chapters on pinning, ThreadLocal, and concurrency limits. Here you combine them into one ordered rollout plan.
Why a checklist
Enabling virtual threads takes one line. The risks come from what changes underneath. Thread pools no longer cap concurrency, so a database or downstream service can receive far more simultaneous calls than before. Code that relied on pool size as a safety limit now has no limit.
Roll out in stages, and check each stage before you move on.
Step 1: Audit the code
Confirm the platform first. You need Java 21 or newer and Spring Boot 3.2 or newer. Then look for these problems:
synchronizedblocks or methods that perform blocking I/O. On Java 21 these pin the carrier thread.- Native frames on the blocking path, which also pin.
ThreadLocalvalues that are large or expensive to create. Many short-lived virtual threads mean many copies.- Code that uses a thread pool size as a hidden rate limit, such as a small pool for calls to a partner API.
- Libraries that are old enough to use
synchronizedaround network calls. Check their release notes for virtual thread support.
Replace synchronized sections that block with ReentrantLock:
import java.util.concurrent.locks.ReentrantLock;
class Counter {
private final ReentrantLock lock = new ReentrantLock();
private long value;
long increment() {
lock.lock();
try {
return ++value;
} finally {
lock.unlock();
}
}
}Step 2: Add explicit limits
Before virtual threads, the pool size limited concurrent work. Now you must limit it yourself. Wrap each scarce resource, such as a partner API, with a Semaphore.
import java.util.concurrent.Semaphore;
class LimitedInventoryClient {
private final Semaphore permits = new Semaphore(20);
private final InventoryClient delegate;
LimitedInventoryClient(InventoryClient delegate) {
this.delegate = delegate;
}
String stockFor(String sku) throws InterruptedException {
permits.acquire();
try {
return delegate.stockFor(sku);
} finally {
permits.release();
}
}
}Also check the database connection pool. A pool of 10 connections still allows only 10 concurrent queries. Thousands of virtual threads will wait for a connection, and your connection timeout will decide how many requests fail. Set that timeout deliberately.
Step 3: Enable in staging
Set the property in the staging configuration only:
spring.threads.virtual.enabled=trueThen confirm it took effect. This endpoint reports the thread type handling the request:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ThreadInfoController {
@GetMapping("/thread")
String thread() {
Thread current = Thread.currentThread();
return "virtual=" + current.isVirtual() + ", thread=" + current;
}
}If it returns virtual=false, check the Java and Boot versions. Remove this endpoint, or restrict it, before production.
Step 4: Observe under load
Run a load test that matches production traffic, then compare against your baseline. Look at these:
| Signal | What a problem looks like |
|---|---|
| Latency percentiles (p95, p99) | Rising tail latency while CPU is low |
| Connection pool wait time | Many threads waiting for a connection |
| Downstream error rate | Timeouts or rejections from partner services |
| Pinning events | Repeated pinned stack traces |
Use these two commands to find pinning and capture thread state:
java -Djdk.tracePinnedThreads=full -jar app.jar
jcmd <pid> Thread.dump_to_file -format=json threads.jsonThe first prints a stack trace when a virtual thread blocks while pinned. The second writes a JSON thread dump that includes virtual threads, which the plain jstack output omits.
Step 5: Enable in production
Make the change reversible. Keep the property behind your normal configuration mechanism, so you can set it back to false and restart. Enable it for a small share of instances first, and compare them with instances that still use platform threads. Widen the rollout only when latency, errors, and pool wait times match or improve.
Exercise
Exercise
After enabling virtual threads in staging, p99 latency rises while CPU stays low, and the database connection pool shows many waiting threads. What is the likely cause, and what do you change?
Show answer
Virtual threads removed the request thread pool as a concurrency cap, so more requests now compete for a fixed number of database connections and wait for one. Limit concurrency to match capacity, for example with a Semaphore around the database-heavy work, and set the connection timeout deliberately. Resize the connection pool only if the database can handle more connections.