0

I know this question has already been asked, but I don't think it ever got a precise answer.

Can Vert.x run multiple instances of the same Verticle on a single Vert.x meaning that a single Verticle can run on multiple event loops ? If it is the case, is each of the event loops running the same handler instance or a separate one, in another words are multiple instances of the same Verticle thread-safe and not sharing any state or can there be concurrency issues ?

According to Vert.x documentation -

Even though a Vertx instance maintains multiple event loops, any particular handler will never be executed concurrently, and in most cases (with the exception of worker verticles) will always be called using the exact same event loop.

It's hard to tell what they mean exactly.

I'm trying to figure out how Actor model and Vert.x compare as far as concurrency and mapping to threads. So far it seems like Vert.x works like Actors where Verticle is a bundle of Actors assigned to a single thread and potentially the only difference is that in Vert.x one bit of code can run concurrently somewhere else (on the same Vert.x) although likely as a separate instance with its own state, while with Actors its strictly prohibited, unless you copy an Actor as a separate class and then it's the same.


Update: It seems like there is a full isolation of state between the instances running on separate event loops as each runs its own instance loaded via a separate classloader in a way that even static variables are not shared.

Here's an interesting thread about it.

And here's what ChatGPT has to say - That's what ChatGPT told me

Tomek Samcik
  • 526
  • 4
  • 7

1 Answers1

2

Yes, vertx can run multiple instances of the same verticle.

When a vertx instance is created it will create some number of event loop threads. By default it is 2 times the number of cpus on the machine.

All verticles deployed will be multiplexed onto those threads. You can have 100 verticles on 4 event loop threads.

But once a verticle has been assigned a thread it will always be executed from that event loop thread.

If you have a non atomic/thread safe variable global somewhere and you have verticles on more than one event loop - it is not safe to access that variable concurrently from multiple verticles. But inside a verticle everything is thread safe unless you launch your own different threads.

However, that is not the way it should be approached anyways. The app should me modeled as a verticle and then multiple copies should be deployed to utilize all the cores on your machine.

Everything inside the verticle can be thought of as thread safe since it is single threaded.

And if multiple verticles need to access global data - then use shared data, eventbus, locking, or any other concurrent friendly data structure like the concurrent hash map.

The foundational concept that needs to be understood is that I want my code to act and be safe as single threaded but I want to be able to use all the cores on my machine. How do you do that? You do that by deploying multiple instances of your verticle. Each instance is single threaded but since multiple instances are there they will utilize all the cpu cores.

It is similar to node.js - node js is single threaded so to utilize all cores on the machine devs deploy multiple copies of the processs. Jvm has true threading so instead of multiple jvms/processes we deploy multiple copies on different threads.

As far as actor model goes - a verticle can be thought of as an actor. You don't have to model ot that way since vertx is unopionanted. And messages are passed via the eventbus among them. Verticles are flat and have no hierarchy or supervision unlike akka or erlang's actor model which may or may not be needed for your use case.

Asad Awadia
  • 1,417
  • 2
  • 9
  • 15
  • Thanks for the answer. I assume that by global you mean static. According to what you say - no shared state between instances of the same verticle across event loops, no concurrency issues then besides static variables. – Tomek Samcik Mar 19 '22 at 18:53
  • You can have concurrency issues if you launch new threads in a verticle as well. – Asad Awadia Mar 20 '22 at 14:29
  • Yes, if you explicitly launch some threads alongside, but the idea I guess is to make use of the provided execution context and run everything as async code that lives on the event loop. – Tomek Samcik Mar 20 '22 at 17:26
  • Yes, then you should be good – Asad Awadia Mar 20 '22 at 18:23
  • Sorry I have to udo accepting, turns out that it's not how we thought it was. – Tomek Samcik Feb 09 '23 at 11:34
  • My answer and explanation are correct - you don't have the understanding of vertx yet to comprehend – Asad Awadia Feb 09 '23 at 18:45
  • You wrote - "If you have a non atomic/thread safe variable global somewhere and you have verticles on more than one event loop - it is not safe to access that variable concurrently from multiple verticles.". That's not correct, have a look at the linked thread. – Tomek Samcik Feb 11 '23 at 15:07
  • It is correct. I have a course on udemy on vertx and provide professional consulting for vertx - i don't need to look at some thread from 10 years ago – Asad Awadia Feb 12 '23 at 14:59