Should we move our Spring Boot services from WebFlux or thread pools to virtual threads? What risks do you check?
Short answer
For I/O-bound request/response services on Java 21+, virtual threads give high concurrency with simple blocking code. Check pinning (largely fixed for synchronized in Java 24+), ThreadLocal usage, connection pool and downstream limits, library compatibility, and measure under load. Keep reactive where streaming and backpressure are central.
Detailed answer
- Fit: services mostly waiting on databases/HTTP benefit; CPU-bound services do not.
- Enable:
spring.threads.virtual.enabled=true(Boot 3.2+); blocking JDBC andRestClientwork naturally. - Risks:
- Pinning in
synchronizedblocks on Java 21–23 (JFRjdk.VirtualThreadPinned); upgrade to 24+/25 or replace withReentrantLock. - Unlimited concurrency overwhelms databases: bound with HikariCP and semaphores.
- Heavy ThreadLocal caches per thread multiply memory.
- Libraries with native blocking or their own thread assumptions.
- Pinning in
- Measure: throughput, p99 latency, memory, DB connection wait time before/after.
- WebFlux codebases: migrate only if complexity is costing you; reactive is still good for streaming.
What the interviewer expects
Nuance about benefits and limits, operational checks, and measurement.
Common mistakes to avoid
Claiming virtual threads make everything faster; pooling virtual threads; ignoring downstream limits.