From https://vertx.io/docs/4.2.0/vertx-core/java/#_specifying_number_of_verticle_instances:
When deploying a verticle using a verticle name, you can specify the number of verticle instances that you want to deploy. This is useful for scaling easily across multiple cores. For example you might have a web-server verticle to deploy and multiple cores on your machine, so you want to deploy multiple instances to utilise all the cores.
I confirmed this through experiments: For multi instance deployment of a verticle, the deployment ID of all instances is the same. All instances are indeed thread safe.
DEMO code:
public class Server extends AbstractVerticle {
private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
private int counter = 0;
@Override
public void start(Promise<Void> startPromise) throws Exception {
vertx.createHttpServer().requestHandler(request -> {
LOGGER.info("Request #{} from {}, verticleId: {}, hashcode: {}", ++counter, request.remoteAddress().host(), vertx.getOrCreateContext().deploymentID(), this.hashCode());
request.response().end("Hello!");
}).listen(8090).onComplete(server -> {
if (server.succeeded()){
startPromise.complete();
}else {
startPromise.fail(server.cause());
}
});
}
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(Server.class, new DeploymentOptions().setInstances(4));
}
}
public class Client {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
WebClient webClient = WebClient.create(vertx);
for (int i = 0; i < 100; i++) {
webClient.getAbs("http://localhost:8090").send().onSuccess(response -> System.out.println(response.bodyAsString())).onFailure(Throwable::printStackTrace);
}
}
}
However, this does not mean that you can deploy multiple instances of any verticle. Because multi instance deployment means that multiple verticle objects are generated. If your verticle is stateful, the states of multiple instance objects are independent of each other. In other words, if you need to maintain a consistent and shared state(like counter in DEMO) in verticle during multi instance deployment, you can't multi instance deployment. Alternatively, you can use the shared data provided by vertx to solve this problem.