We are currently facing a performance issue with spring webFlux. For the sake of ascertaining the benefits of the reactive programming we have implemented a Spring Boot service which fetches data from a MongoDB and returns it via a REST API to the consumers.
The service exists in two variants:
- A non-reactive implementation with Spring Boot, MongoRepository. This service returns the data as List
- A reactive implementation with Spring Boot, ReactiveMongoRepository, spring-boot-starter-webflux. This service returns the data as Flux.
In both implementations the REST controller directly fetches the data from the repository and returns it as List resp. as Flux. No further application logic is executed.
We conducted a small load/performance test with 100 users calling the service and we found out that the non-reactive implementation performed far better than the reactive implementation.
As a matter of fact, not only had the non-reactive implementation a better HTTP throughput but, perhaps more interestingly, it consumed less CPU and less threads than the reactive implementation! This was particularly counter to expectations since we anticipated the reactive version to scale with a small number of thread as mentioned in https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1
Is there something we need to tweak in the settings?
Has someone faced a similar issue?
Let me try to explain the possible reasons for this behavior. A reactive application does not work faster than a reactive application. A reactive application does not allow the system to stand idle if the request queue is not empty. Since you have tested at low load, you have not seen the pros and cons of a reactive application, but you have seen downgrade performance. The performance is lower than that of an inactive application, because the reactive execution has a small overhead.