Build REST API with Vertx toolkit - Part I
VertX is a toolkit to develop a large numbers of the applications with the reactive programming model. Vertx is polygot, so it supports a numbers of languages to develop code, like Java, Groovy, JS, Kotlin etc. VertX is based on another project called netty.io which actually the base platform to use the capability of efficient reactive programming with non-blocking threads.
Vertx Capability(s)
- Polyglot is the feature making Vert.x stand out from other server frameworks. In the past, server frameworks did not support multiple languages. Supporting several languages does more than expand the range of users. More important thing is that services using different languages in a distributed environment can intercommunicate with ease.
- Event driven programming with the distributed environment programming capability.
- Efficient Built-in Threading model helps to achieve higher throughputs.
This article mainly focusing on the Java8 with VertX. As the first part, will walk some of the basic concepts of VertX.
Core VertX components
There are 3 main parts of VertX solutions.
- Event loop: This is the non-blocking thread to pick the request from event queue and send it to correct address registered into Event bus. Since this is non-blocking thread , so it is very efficient in terms of performance.
- Verticle : This is the handler part for every request. It is responsible to process the request. Verticles are mainly 2 types - Http Verticle and Worker Verticle. Http verticles are mainly handling http requests directly from event bus , it needs to be non-blocking always. Worker verticles are responsible for performing the heavy duty job and connecting to external services.
- Event Bus: Event bus is the main tool to communicate the verticles with each others.Like, for example, if Http Verticle needs to call another Worker verticle to perform some background operation, it needs to communicate through event bus. This is similar concept on how in MS Azure a Web Role communicates with Worker Role to perform background work.
Vert.x Thread Pool
Vert.x has few types of thread pools:
- Acceptor: A thread to accept a socket. One thread is created for one port.
- Event Loops: It equals the number of cores. When an event occurs, it executes a corresponding handler. When execution is performed, it repeats reading for another event.
- Background: Used when Event Loop executes a handler and an additional thread is required. Users can specify the number of threads in vertx.backgroundPoolSize,can be set into config file. The default is 20. Please be careful for using too many threads as it causes an increase in context switching costs.
Worker Verticle vs ExecuteBlockingHandler
To perform any background operation, there are 2 choices , either to use worker verticle or use blocking handler.
Execute blcoking handler mainly used when the background job can be performed within the same verticle . This is mainly inline coding , not advisible to perform a lot of jobs into this block.
Worker verticle is the best choice in case the application needs to perform a lot of job around the background operation, like request/response processing including its business logic. Since this is verticle, so this will be deployed separately and will be communicating with other verticles using Event bus.
Useful resources:
vertx.io, netty.io