1
votes

I got 2 questions that relate to Vertx threading model. The documentation mentions:

  • A Vert.x instance maintains N event loop threads (where N by default is core*2) by default.
  • For the levels of concurrency required in many modern applications, a blocking approach just doesn’t scale

Vertx also provides thread pool-related function to handle tasks using server resources requiring long periods for event handling (worker threads).

Ok, so we know that threads have overhead in terms of the memory they require (e.g. for their stack) and in context switching.

Vertx threads are not blocked (if correctly used) but if we got more event-loops than cores (and a thread pool for worker threads as well) isn't a context switch inevitable?

Second question - I want to understand how vert assures that a single thread is running for an event loop, considering the fact that thread switching/scheduling is done in OS level. I red in this documentation that:

An event loop context executes handlers on an event loop: handlers are executed directly on the IO threads, as a consequence:

  • a handler will always be executed with the same thread
  • a handler must never block the thread, otherwise it will create starvation for all the IO tasks associated with that event loop.

Can somone please clarify "handlers are executed directly on the IO threads"?

1

1 Answers

3
votes

To your first question, context switches are inevitable. The goal is to minimize them, not to get rid of them. The default numbers of event loop and worker are well... defaults. Note that having 8 event loops doesn't mean they will all be used. If you deploy a single instance of a standard Verticle, only one will be busy.

To your second question, it means that event loop threads handle Netty (socket) IO events (hidden from the developer by Vert.x) as well as Vert.x (connection, request) events. In practice, when a HTTP request buffer is received, the event goes through Netty and Vert.x up to your application code. If you block the thread, the following events cannot be handled.