I’m back to share the results of a comparison I was curious about: comparing the scalability of a simple microservice developed with Spring Boot. The idea behind this exercise is to test how Virtual Threads (one of the most famous Java 21 new features) impact on service availability.
The service will be responsible for providing a simple REST API to store, read, modify, and delete locations in a PostgreSQL database. The code is available in the GitHub repository spring-boot-location-api.
In order to develop this microservice I’ve used the traditional technological stack of Spring Boot, with the Spring Web and Spring Data JDBC starters. To the most curious, in the repository branch webflux-r2dbc I’ve also added an equivalent reactive implementation using Spring Webflux and Spring Data R2DBC.
How to execute the load tests
To measure the availability with and without virtual threads, I will use the command-line program siege, which provides a straightforward interface for executing load tests. Let’s take a look at an example:
This command will launch 1,200 concurrent HTTP requests (using the -c option) against the specified endpoint for 60 seconds (using the -t option). With this command, the availability of the endpoint that responds to GET requests can be measured with the different microservice configurations.
Virtual threads and blocking services
Let’s check the benefits of using Virtual Threads in a traditional blocking application. Traditional blocking applications typically use a thread to handle each incoming request. By default, the JVM uses an operating system thread to handle these HTTP requests. By enabling Virtual Threads, the Java machine will scale better in scenarios where multiple threads are blocked by I/O operations, as Virtual Threads are specifically optimized for such workloads and do not require the same resources as an OS thread.
In Spring Boot applications, we can activate Virtual Threads simply by adding the property:
spring.threads.virtual.enabled=true
Comparing the HTTP GET method
Let’s see what the results of our location API are with and without Virtual Threads by sending a request that will execute a query against the database.
Test with Virtual Threads disabled
Test with Virtual Threads enabled
The availability provided by the solution with virtual threads is superior. Additionally, it is capable of handling 13% more requests within the same time interval without any changes to our code, resulting in significantly improved scalability.
Comparing the HTTP POST method
Now let’s examine the performance of the location persistence endpoint. This test has consistently been conducted with an empty table to ensure equal conditions between executions with and without virtual threads.
Test with Virtual Threads disnabled
Test with Virtual Threads enabled
In this case the service availability provided by the virtual threads solution is also better, being able to respond to 16% more requests.
Summing up
Definitely, virtual threads offer performance advantages for our blocking Java services. If your projects are already using Java 21 or migrating those that receive heavier loads to this version do not require a significant effort, it could be a good idea to consider starting to use virtual threads in your Java services as soon as possible.