Open ORESoftware opened 5 years ago
regarding this question: https://stackoverflow.com/questions/23931208/how-vert-x-is-single-threaded
the biggest question in my head for a simple REST API is - for each incoming request - should I do work with a worker verticle or can everything be done in the main verticle? Where do I need to do locking, if anywhere?
For a simple REST server, my guess is you don't want do db calls with a worker verticle and then send the data via the eventBus, because that's too expensive. I assume using io.vertx.ext.web.Router to handle requests in the main verticle is best. But I don't know if the main verticle code is always running in the same thread - my guess is that it is.
?
You should only work with a worker verticle if you're dealing with "blocking calls" (i.e. non NIO calls). Actually, this is really well covered in the docs. Please read the section about the event-loop and verticles.
For instance, if you're using the reactive pg driver, the official redis driver, the official mongodb driver, etc. You don't need any worker verticle at all, just a standard one.
If you're dealign with blocking calls sporadically, then go with executeBlocking
. If you're having something like a CronJob which takes a long time to compute stuff, then go with a Worker Verticle.
These topics are covered in the documentation.
@aesteve my biggest question is if we use 1 verticle, if all my code is running in the same thread (that way I can avoid the need to explicitly lock).
I would be really nice to be able to clone a simple project that does RESTful API and a simple project that does websockets, devising the project structure o/w leaves lots of questions, better to have some structure determined already?