spring-bootperformancespring-webfluxreactive-programmingvirtual-threads

Virtual Threads and Spring WebFlux


Recently Java 21 was released in which they added virtual threads, I was looking at some performance tests of Spring Boot on virtual threads and Spring WebFlux, the results showed that standard Spring Boot outperforms reactive Spring WebFlux, I have a question is there any point to use virtual threads in Spring WebFlux to improve performance? Will there be a performance gain? Will it not be a blocking operation? Also, how to use virtual threads in Spring WebFlux? Is my code below a good practice?

private final ExecutorService virtualPool;

public Mono<String> myMethod() {
    return Mono.fromFuture(CompletableFuture.supplyAsync(() -> doBlockOperation(), virtualPool));
}

I want to use virtual threads in Spring WebFlux to get a performance boost


Solution

  • Your code will not work in the current 3.5.11 version of project-reactor, therefore, also in spring-webflux because for blocking operations you are forced to use boundedElastic scheduler. If you use another pool you'll get IllegalStateException I would expect a dedicated Scheduler in the next releases of the project reactor.

    You can track the integration between Project Reactor and Project Loom (Virtual Thrads) under https://github.com/reactor/reactor-core/issues/3084

    EDIT:

    Also, how to use virtual threads in Spring WebFlux?

    Since Java 21+ and the new reactor-core 3.6.x:

    Add a JVM argument:

    -Dreactor.schedulers.defaultBoundedElasticOnVirtualThreads=true
    

    verify the logs like:

    .publishOn(Schedulers.boundedElastic())
    .map(doSth())
    .log()
    

    you should see sth like:

    2024-06-12 10:46:16.570 [loomBoundedElastic-1] [] INFO  reactor.Mono.Map.28 - onSubscribe(FluxMap.MapSubscriber)
    

    instead of

    2024-06-12 10:46:16.570 [boundedElastic-1] [] INFO  reactor.Mono.Map.28 - onSubscribe(FluxMap.MapSubscriber)
    

    References: https://spring.io/blog/2023/10/31/what-new-is-coming-in-reactor-core-3-6-0