One More Byte

Advice on starting with Vert.x

• java

Vert.x is a Java based platform to run serverside programs. It is very similar to Node.js, and very different from Java Enterprise application servers.

Although Vertx has some good documentation to get you started, there are a few pitfalls the documentation does not make you aware of. In this article, I’ll highlight a few I ran into myself in the past few months of working with Vertx in a real project.

Verticles, Modules, Containers and Clusters

Vertx is pretty flexible in splitting up code in several ways, following a Russian Doll pattern. Although all the details are documented, no overview is available. This will likely confuse you as you move beyond deploying your first Verticle. This is a good way of looking at the different dolls, starting from small to big:

Vertx also encourages the following (but also allows other models):

In practice, if you can constrain yourself to this one Verticle to one Module to one Container, this makes for a very simple model. Just run everything in different Containers and configure at least a Cluster for the local machine to connect everything together.

Threading and pooling

Threading in Vertx is pretty straightforward, but again, it is good to get a bit more overview before diving in the docs. One of the staples of Vertx is to encourage “non blocking” coding styles. In typical Java programs, for instance, the thread is blocked while waiting for external dependencies like the file system, database or external services. By not blocking, less threads are needed to process the same user load. Since threads are relatively expensive, it means in practice that you’d be able to service more users at once with the same hardware.

For tuning, you need to think about Verticle instance count and the thread pool affected. Although this will be wildly different for each application and you will need to tune yourself, the following basic rules do apply:

Error handling

Something Vertx does not provide in its documentation is some consideration on how to do error handling. Vertx, by default, does not render a response to the receiver when an error occurs. Any uncaught Exceptions will be propagated to the Vertx log, but the client of the event that is waiting for the response will never receive any response. At best, this results in an unnecessary timeout at the client. Vertx, however, has default API that does not explicitly timeout. When you never receive an answer, the handler waiting for a response will never be called or cleaned up, resulting in a memory leak.

By now, you should have become rightfully scared about dealing with Exceptions correctly in Vertx. Do not trust the example code on this! A few practical considerations:

There is some more useful things to say about error handling in non-blocking code in general, but I’ll save those for another post. For now, happy coding with Vertx, and let me know what other useful tips you may have!