Vertx event bus - what is an event loop?

1,716 views
Skip to first unread message

Jayamine A.

unread,
May 1, 2016, 9:27:52 PM5/1/16
to vert.x
I was reading a article about vert.x event bus. 
 
  • Internally, Vert.x instances maintain a set of threads (typically one for each CPU core) that executes in an event loop: check to see if there's work to do, do it, and go to sleep.
what is this event loop? 
if anyone can explain it i'm really appreciate about it.

clement escoffier

unread,
May 2, 2016, 1:30:16 AM5/2/16
to ve...@googlegroups.com
Hi,

Event loops are a (pretty old) pattern to dispatch "events". See https://en.wikipedia.org/wiki/Event_loop.  

Basically, it's a thread with an associated queue of "events" and an associated set of "handlers". The thread pick the right "handler" for each event from the queue and call it. Event loops have several benefits. Among these we can cite: ordering (the event are processed in order), and synchronization (as everything is called in the same thread). However, because of this, called handlers must never block as it would avoid the other events from the queue to be dispatched.

As said previously, event loops are not new. Many well known technologies use event loops such as AWT / Swing, Android (activities), node.js, and most operating systems...

What makes vert.x pretty unique is the number of event loops and their organization. Most of the time you have a single thread. With vert.x, it's configurable but generally it's the number of CPU cores to balance the load to all the cores. Then, vert.x enforces the benefits mentioned previously:

* on one event loop, events are dispatched in order
* handlers are always called by the same event loop, reducing the need of synchronization

Clement


--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/3b404948-8128-4c32-8974-45c1cd793e56%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

clement escoffier

unread,
May 2, 2016, 7:39:51 AM5/2/16
to ve...@googlegroups.com
Just found this academic article about the reactor pattern: http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf
This article details how the event loop works and how the events are dispatching.

Clement

Jayamine A.

unread,
May 2, 2016, 10:57:01 AM5/2/16
to vert.x
Hi 

I got the idea!
Thanks a lot. 
specially the article that you sent me was helpful and i learned a lot from it(not only event loop).
thanks again :-)

jayamine 

Julien Viet

unread,
May 2, 2016, 11:17:48 AM5/2/16
to ve...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.

Jayamine A.

unread,
May 9, 2016, 10:04:54 AM5/9/16
to vert.x
Thanks . It helped me a lot :-)

Tosheer Kalra

unread,
Oct 21, 2017, 1:12:39 AM10/21/17
to vert.x
Hi Clement,

I understood the concept of event loop, but one thing regarding vertx event loop which still i am not able to figure out is how "handlers are always called by the same event loop, reducing the need of synchronization". As for me the process goes like this.

1. We have a verticle which is running on 4 core CPU and we create 4 event loops thread. Now Verticle start method is processed and a callback registered for a HTTP get call at "/customer" endpoint with event loop.
2. Http request come for "/customer".
3. Event loop find what is the event handler registered for this event and trigger that event handler and request is processed.

For any subsequent request will it execute the request in the same event loop out of 4 we have? Even how it decide which which event loop to use first time?

Let me know if i am missing anything.

Thanks
Tosheer

Jez P

unread,
Oct 21, 2017, 2:51:22 AM10/21/17
to vert.x
Take a look at this 


The event loop doesn't find the handler. The verticle instance knows which event loop it runs against via its Context (which is set at verticle deployment), and executes the handler on that context. Strictly speaking it puts an entry on a queue destined to be run by that event loop thread. So yes your one verticle instance will always run its handlers on the same event loop, which includes, therefore, the handlers for subsequent requests. 

Tosheer Kalra

unread,
Oct 22, 2017, 2:25:52 PM10/22/17
to vert.x
Thanks Jez,

Things are much more clear now.
Reply all
Reply to author
Forward
0 new messages