Vert.x for Reactive Applications: What it is, Features and Benefits
Tech Insights
Vert.x for Reactive Applications: What it is, Features and Benefits
Vert.x for Reactive Applications: What it is, Features and Benefits
Tech Insights

Vert.x for Reactive Applications: What it is, Features and Benefits

Reactive applications are scalable since workload grows and also resilient when failures arise. If an application has both scalability and resiliency, it means that the application is responsive. 

Vert.x is a toolkit used for building reactive applications on the JVM using an asynchronous and non-blocking execution model. As it is based on Netty, which is an event-driven and asynchronous network application framework, Vert.x helps build reactive applications using the event loop concept, which is an implementation of a Multi-Reactor pattern. 

An event bus always listens for new events. Whenever a new event arrives, it dispatches it to some handler that knows how to handle it.

Vert.x Architecture

The image above indicates a traditional implementation of the Reactor pattern executed by a single thread and reducing the concurrency issues as the pattern that guarantees no handlers is executed concurrently. However, it also implies that the event loop must never be blocked by any of the execution handlers since otherwise, it can decrease the whole system performance significantly. Vert.x brings a good approach here by using a Multi-Reactor pattern, which means using multiple event loops running on separate threads while still guaranteeing that no execution handlers will be executed concurrently. Multi-Reactor patterns use multiple event loops, which requires more CPU cores to increase performance.  

Another case is that even when we use multiple event loops, some events can still take a long time to be processed. In this case Vert.x provides using worker threads for costly executions.  

Vert.x Terminology  

Vert.x has its defined terms. Let’s go over the most well-known terms below:  

  • Verticle 

Vertical is a part of Vert.x architecture. It’s a piece of code that can be deployed to Vert.x and is executed in a single thread. It can be considered as a controller handling servlet requests, and all communications are executed with events asynchronously.  

Vertical types:  

Standard verticles: All the code is executed within the same event loop.

Standard verticles-types in vertx-1
Standard verticles-types in vertx-2

Worker verticles: Not executed within an event loop, executed using a thread from the Vert.x worker thread pool. We should not block the event loop for long-running tasks, so it can use a thread from the Vert.x worker thread pool and not block the event loop at all. They can be executed by different threads at different times.

Srever-JVM Vert.x

How to deploy a worker verticle?

You need to create an object from DeploymentOptions class and set its worker property as true. Finally, the deployment options must be used while deploying the verticle. Please check the simple example below: 

How to deploy a worker verticle-code
  • Vert.x Instance  

A verticle is executed within a single Vert.x instance, which is executed on the JVM instance. If there are a lot of verticles, they will be simultaneously executed in the same Vert.x instance. Moreover, it is feasible to make multiple Vert.x instances clustered using Event Bus. 

It is like a container for verticles and also an entry point to API. 

Vert.x Instance-article
  • Concurrency  

The Vertical instance always promises that it is executed within the same thread. No deadlock or race condition will occur.  

  • Event Loops  

Vert.x instance manages the number of thread pools to the number of CPU cores very closely.  

  • Event Bus  

It is a lightweight distributed messaging system used for point-to-point communication, publish/subscribe messaging model, request/response model.

event-bus-vertx-article
  • Shared Data  

Passing and sharing data between threads is a very important concept in asynchronous programming. Vert.x provides both synchronous and asynchronous shared data functionality for this. The shared data feature enables users to use shared data between different verticles for the same Vert.x instance or a cluster of Vert.x instances.  

Vert.x uses Shared Map for global access of data.  

Vert.x Features  

Let’s look over a list of features characteristic of Vert.x that make it the framework of choice for many developers. What are the advantages of using Vert.x?  

  • Handling concurrency and high scalability  

Vert.x is a lightweight event-driven and non-blocking application framework that allows developers to handle a lot of concurrency using a small number of kernel threads, so it increases the scalability of the application significantly.  

  • Polyglot 

Vert.x is similar to Node.js but it is both polyglot and reactive on the JVM. It supports multiple languages like Java, JavaScript, Apache Groovy, Ruby, Scala, Kotlin. 

Polygot-vertx-article
  • Automatic failover(Resilient)  

In a cluster environment, if running a Vert.x instance fails, it will be automatically restarted on another node of the cluster. 

  • Fast  

The way Vert.x addresses that problem is that any I/O work is put in a queue. Since putting a new task in a queue is not a particularly heavy operation, Vert.x is able to process hundreds of thousands of those per second.  

  • Ecosystem  

Web APIs, databases, messaging, event streams, cloud, registries, security…  you name it. Vert.x has you covered with a comprehensive end-to-end reactive clients stack for modern applications.  

ecosystem-vertx-article
  • Clustering  

A cluster manager is responsible for the discovery of nodes and sharing data within different Vert.x instances.  

To use a cluster manager programmatically in Vert.x: 

cluster manager programmatically in Vert.x-1
cluster manager programmatically in Vert.x-2
  • Documentation  

The official Vert.x documentation is very extensive, nicely grouped by topic and with many manuals and examples in multiple programming languages. These examples are usually kept quite simple, which makes sense to not overwhelm the reader and to focus on one certain tool, as well as look over Vert.x use cases one at a time. 

  • Lightweight  

It is very easy to integrate Vert.x into your project and its core library. It can also be used in a Spring Boot project, so you can take advantage of a dependency injection feature of the Spring framework inside a Vert.x project. 

Benchmarks for Vert.x and Spring Boot Framework 

The following are some examples of Vert.x vs Spring Boot uses, that illustrate how the two frameworks perform under various conditions with different concurrency levels. 

Vert.x example: 

 Vert.x example

Spring Boot example: 

Spring Boot example
Vert.x Test1 Spring Boot Test1 
Concurrency Level: 10 
Time taken for tests: 58.640 seconds 
Complete requests: 1000000 
Requests per second: 17053.09 [#/sec] 
Time per request: 0.586 [ms] 
Concurrency Level: 10 
Time taken for tests: 205.175 seconds 
Complete requests: 1000000 
Requests per second: 4873.89 [#/sec] 
Time per request: 2.052 [ms] 

As you can see in the first test, Vert.x is significantly faster compared to Spring Boot. Let’s have a look at the CPU and memory usage during the test: 

  • CPU and memory usage for Vert.x: 
  • CPU and memory usage for Spring Boot: 

As expected from a lightweight framework, Vert.x consumes much less CPU and memory over time. But this is just a very simple test case. More interesting is the second test: 

Vert.x Test2 Spring Boot Test2 
Concurrency Level: 10 
Time taken for tests: 103.471 seconds 
Complete requests: 10000 
Requests per second: 96.65 [#/sec] 
Time per request: 103.471 [ms] 
Concurrency Level: 10 
Time taken for tests: 106.633 seconds 
Complete requests: 10000 
Requests per second: 93.78 [#/sec] 
Time per request: 106.633 [ms] 
Concurrency Level: 100 
Time taken for tests: 10.488 seconds 
Complete requests: 10000 
Requests per second: 953.46 [#/sec] 
Time per request: 104.881 [ms] 
Concurrency Level: 100 
Time taken for tests: 11.779 seconds 
Complete requests: 10000 
Requests per second: 848.99 [#/sec] 
Time per request: 117.787 [ms] 

Because of the 100 ms waiting time in it, the minimum time per request is 100 ms in this test, so only the additional time is relevant here. With 10 concurrent requests, you can already see a slight advantage for Vert.x, but with 100 concurrent requests, the difference is much more obvious. Again as expected, Vert.x is significantly faster. Due to the use of fewer threads, Vert.x with its non-blocking approach performs a lot faster than Spring Boot, which needs to create at least 100 parallel threads in this example. 

What is the Future of the Vert.x Project?  

Vert.x has very good documentation that makes it easy to understand every detail with the help of simple examples. At the moment, it makes it a good option to use in terms of easiness of deployment and super performance as an async server. There is a great community with contributors who continue to fix the issues and release new features in Vert.x. It is also supported by the Eclipse Foundation community team. The strong community that has emerged around Vert.x makes it very easy to discover all its advantages, whether it’s through studying documentation or any Vert.x tutorial or example you may need to further explore the possibilities. 

Conclusion  

To summarize, Vert.x is a great toolkit when you want a non-blocking strategy in an event-driven architecture. It works with verticles, which provides a thread-safe environment. Vert.x makes it easy to write concurrent applications that scale effortlessly from a single low-end machine to a cluster with several high-end servers. Add the fact that you can use the most popular languages for the JVM, and you have a web developer’s dream come true! 

Orkhan has been working for Symphony Solutions since November 2021. Prior to joining Symphony Solutions, he was a part of E-Gov Azerbaijan as their Senior Java Developer. Orkhan has over eight years of solid experience in analysis, design, development, testing, deployment of financial institutions, integration projects, as well as management systems application development.

Orkhan completed his formal education having obtained a Bachelor’s degree in Mathematics Engineering from Istanbul Technical University. He also holds several professional certificates in Oracle.

Orkhan Mirzayev
Orkhan Mirzayev
Senior Java Developer
Share