Thursday, July 16, 2015

Akka Respond When Ready Pattern (pause processing of messages)?

Perhaps someone has already solved this, or perhaps it is so simple what I've done is common sense. I am too new to Akka to know for sure. So here is the scenario:

  • An Actor we'll call Server needs to bootstrap itself. This takes some time.
  • Meanwhile, an Actor we can call Client begins to send Request messages to the server
  • ...but the server isn't ready yet.
The punchline, put long running initialization in preStart(). since messages will happily collect in the mailbox.
So, let's look to the more general case of what do do if an actor becomes unavailable for some reasonable period of time, not just at initialization. Here are the options I considered:
  • Server can discard messages any time it isn't available to respond (such as the bootstrap scenario)
    • Requires clients to wait a certain amount of time for a timeout, then resend.Requires client to keep track of/queue messages
  • Server can buffer messages when it is unable to respond, then handle them when ready
 I like the last solution the best. I browsed the akka docs on the thought that maybe they had something that does this. Low and behold: ReliableProxy is already available from Akka. At first I thought this was what I needed.
But I immediately realized that the message between E to B in the picture above suffers exactly the same problem as the message from A to B directly. So this is of no use. Therefore I implemented serverside buffering of messages during times it is unable to respond. When it is able to respnd it dequeues the messages and handles them. This is better than any option in which the client needs to take server "sleepiness" into consideration. I came across this stack overflow topic which affirms my design choice: