2
votes

I have a question regarding the Ask pattern in Akka.

I am calling an actor using the ask pattern from my application. My application is a normal class (not an actor itself):

(actor1Ref ? msg) mapTo[ResponseMsg]

However the actor which is called above calls a second actor like:

secondActor ! msg(sender)

and it's the second actor which returns the actual response like:

originalSender ! responseMsg

So basically there is an indirection involved and when Actor1 calls Actor2 it uses !.

I tested the code and it works. But I am not sure if the above is the right way of writing this code.

So should I always use ? between the actor1 and actor2 calls and try to pipeTo the results? Or is it OK for me to use ! between intermediate calls and when I want to return to the original caller, just send a message to the reference I pass around?

1
According to the documentation, it is okay: ActorRefs can be freely shared among actors by message passing. Please, check the documentation for more details doc.akka.io/api/akka/2.5/akka/actor/ActorRef.html.Mykhailo Hodovaniuk

1 Answers

2
votes

Your current approach is perfectly fine. In fact, it is preferable to use tell (!) for actor-to-actor communication. From the documentation:

There are performance implications of using ask since something needs to keep track of when it times out, there needs to be something that bridges a Promise into an ActorRef and it also needs to be reachable through remoting. So always prefer tell for performance, and only ask if you must.

[Tell] is the preferred way of sending messages. No blocking waiting for a message. This gives the best concurrency and scalability characteristics.