vert.xvertx-verticlevertx-eventbusvertx4

Multiple or single instance for clustered vertx


I am developing microservices using vertx4 and each service is composed of multiple verticles and all are deployed as clustered(hazelcast) verticle using below code.

Vertx.clusteredVertx(options.setHAEnabled(isHa), (vertx) -> {
            ((Vertx)vertx.result()).deployVerticle(verticleName, (new DeploymentOptions()));
});

I am calling above code for each verticle in a service and multiple vertx object or instance is created.

  1. Is it correct way deploy verticles in cluster?
  2. Should I get one vertx object from clusteredVertx and use it deploy multiple verticles?
  3. What would be the advantage and disadvantage of having multiple vertx instance?

I have gone through the vertx documentation but I am not able to understand clustering particularly. I even checked some of vertx source codes like Launcher class but nothing helped me to get answers.


Solution

  • I think you are mixing two concepts, one is the Vertx instance, the other is clustering.

    Each Vertx instance has and manages its own event bus and its own event loops that are best used to run many verticles. One instance of Vertx is fully capable of managing a lot of verticles and will allow them to communicate to each other over one event bus. This is the concurrency model.

    Clustering needed when you need to extend and connect the event bus between multiple applications (= multiple Vertx instances) normally deployed to different JVMs and even machines.

    When clustered, each Vertx instance has own event bus but instances that discover each other (how depends on clustering technology) will bridge their event buses this means that their verticles will find each other. However this bridge has its own cost and is not something you want to use for inside application communications.

    So if you are talking about one application, then the usual method is to obtain a single Vertx instance and use that for deploying all your verticles. The verticle itself does not know if its mounted on a clustered Vertx or not. Except it can explicitly mount its handlers only for local usage (vertx.eventBus().localConsumer(...)) which means even in a clustered setup those handlers will not respond to messages from other instances.

    Having multiple Vertx instances inside one application is not impossible but if you are not trying to bend Vertx to its extends, then this is not for you.