Video in progress

How do you plan a migration of a large monolith from platform threads to virtual threads?

First I measure where there is a benefit: the share of IO waiting, thread pool queues. Then I look for obstacles (pinning, heavy reliance on ThreadLocal, hidden limits imposed by pools) and roll it out step by step, behind a feature flag, verified by load testing.

📖 Answer

Turning on virtual threads is a single property, but the consequences spread across the whole system. This is how I plan it.

1. Is there a benefit? From JFR and the APM: how much of the request time goes to IO waiting, whether the Tomcat pool fills up, whether we hit the server.tomcat.threads.max limit. If the bottleneck is CPU or the DB, virtual threads give you nothing.

2. An inventory of obstacles:

  • IO inside synchronized, in our own code and in libraries. On JDK 21, I check staging with JFR's jdk.VirtualThreadPinned. Moving straight to JDK 24+ removes a large part of this problem;
  • versions of the JDBC driver, Hikari, HTTP client and Kafka client: use versions that are adapted for virtual threads;
  • ThreadLocal: every virtual thread gets its own copy. If a library caches an expensive object in a ThreadLocal (a buffer, a SimpleDateFormat, a cryptographic context), a new one is now created on every request, and allocation shoots up;
  • a hidden rate limit created by thread pools. Before, with 200 threads, no more than 200 requests could go to the DB at once. Now there's no limit, and you have to set one explicitly with a Semaphore or the connection pool wait time.

3. Stages:

  1. Upgrade the JDK and libraries, and release without virtual threads.
  2. Turn it on in one internal, IO-heavy service, on a canary pod.
  3. Load test: p99, CPU, heap, GC, DB connection wait time, before and after.
  4. Observability: a JSON thread dump (jcmd <pid> Thread.dump_to_file -format=json), because the classic jstack doesn't show virtual threads.

4. Rollback through the spring.threads.virtual.enabled flag, with no code change.

An important organizational point: don't tell the teams "you no longer have to think about pool size". Threads got cheaper, but DB connections, partner API quotas and memory did not.