Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Akka's event bus guarantee message order?

I would like to keep the order the events entered the bus. For example if event1 then event2 where entered to the bus then a subscribed actor would get them in that order.

The question is if such order is guarantee, both on clustered and on single node actor system.

like image 711
Noam Shaish Avatar asked Oct 07 '14 10:10

Noam Shaish


1 Answers

If you are using the event stream on the actor system (system.eventStream) and if you can guarantee a single thread is publishing, then yes, the order will be preserved. The subchannel classification flavor of the event bus (the kind tied to system.eventStream) is really simple. There is a Map of basically class type to a list of subscribing actors. When an event is published, it gets the matching list of subscribers from the Map (if any) and then sends the message to each of them. So if only one thread was calling publish, then it would need to finish publishing event1 (and thus delivering it to the mailboxes of all subscribers) before moving on to event2. Actors process their mailboxes in the order that the messages were received (unless you are using a custom PriorityMailbox impl), so first in first out.

Now of course if you had multiple threads, I can't guarantee this in order processing as thread1 may start the publish first, but thread 2 might finish it first leading to what you would consider out of order.

like image 182
cmbaxter Avatar answered Oct 21 '22 06:10

cmbaxter