Brief introduction about Vert.x
Overview
In the modern days, the development of people results in the propagation of technology. Many have come and many have gone, one of which has been gaining attention all over the world, namely reactive programming (or asynchronous programming).
What is asynchronous programming?
- Leverage asynchronous programming and non-blocking I/O to handle more concurrent connections and use less threads.
- Instead of waiting for I/O operations to complete, we can shift to non-blocking I/O.
- The idea behind non-blocking I/O is to request a (blocking) operation and move on to doing other tasks until the operation result is ready.
For example a non-blocking read may ask for up to 256 bytes over a network socket, and the execution thread does other things (like dealing with another connection) until data has been put into the buffers, ready for consumption in memory.
What is reactive system?
Reactive system has 4 characteristics:
- Elastic
The ability of the application to work with a variable number of instances.
Allows the app to respond to traffic spikes by starting new instances and load-balancing traffic across instances.
- Resilient
When one instance crashes in a group of elastic instances, resiliency is naturally achieved by redirecting traffic to other instances, and a new instance can be started if necessary.
When an instance cannot fulfill a request due to some conditions, it still tries to answer in degraded mode: (1) returns the old or cached value or (2) returns empty or default data.
It may also be possible to forward a request to some other, non-error instance.
In the worst case, an instance can respond with an error, but in a timely fashion.
- Responsive
Responsiveness is the result of combining elasticity and resiliency.
It is important to note that responsiveness is not possible if one component relies on a non-scalable resource, like a single central data- base. Indeed, starting more instances does not solve the problem if they all issue requests to one resource that is quickly going to be overloaded.
- Message-driven
Using asynchronous message passing rather than blocking paradigms like remote procedure calls is the key enabler of elasticity and resiliency, which lead to responsiveness.
Note: Reactive programming can be implied asynchronous programming but not vice versa.
Some notable names:
- Spring webflux (Java)
- Vert.x (Java)
- Nodejs
- Quarkus
In today’s text, I’m going to talk briefly about Vert.x.
Vert.x
What is Vert.x?
- Vert.x is a toolkit providing APIs for writing asynchronous programming applications.
- Vert.x allows developers to fully customize their code (as it is not a framework, developers can freely use Vert.x code along with their other code).
- Vert.x takes advantage of asynchronous programming (instead of waiting for blocking operations, it moves on to doing other tasks until the result is ready). That’s why vert.x is really fast.
How does Vert.x work?
Verticle
- Vert.x has the smallest computing unit called Verticle.
- Each verticle runs on an event-loop thread (main thread).
- We can deploy multiple verticles.
Event-loop thread
- Event-loop thread pool size can be set (be careful of event-loop threads competing for resources).
- Events (no blocking operations) are dispatched to an event-loop thread (main thread) asynchronously.
Worker thread
- Such blocking operations are dispatched to worker threads.
- After completing blocking operations, callbacks are invoked to return the handle back to event-loop threads.
- Worker threads can also be deployed as a verticle. That time, the verticle will be deployed on a worker thread instead of an event-loop thread (main thread).
Event bus
- Event-loop threads can communicate with each other over an event bus.
- Only 1 event bus per vertx instance.
- Vert.x supports 3 ways of messaging: (1) publish-subscribe messaging: when a message is sent to the destination, all consumers receive it (like broadcasting messages); (2) point to point messaging: messages are distributed in a round-robin fashion among customers and there is no fairness mechanism to distribute fewer messages to an overloaded consumer; (3) request-response messaging: 1 producer — 1 consumer and event bus generates a temporary destination solely for communications between consumers and producers expecting a reply.
Promise
- Promise is used to hold an eventual value.
Future
- Future is used to read that eventual value.