ab_732
ab_732

Reputation: 3897

Akka: Ask vs Tell

I finished to read the Effective Akka book from Jamie Allen. I am fairly new to Akka, I cannot really get why at some point he says:

However, there are a couple of things about it that are not ideal. First of all, it is using futures to ask other actors for responses, which creates a new PromiseActorRef for every message sent behind the scenes. This is a waste of resources. It would be better to have our actor send messages out in a “fire and forget” fashion and collect results asynchronously into one actor.

Using Ask instead of Tell seems generally to be more expensive. How much more expensive is using a Pull instead of a Push? And also, why is a Tell generally preferable compared to an Ask?

Upvotes: 2

Views: 1237

Answers (2)

jamie
jamie

Reputation: 2161

I'm the author of Effective Akka. My point about using Ask was that you are using a Future. If you aggregate multiple Asks into a single response, you created multiple timeouts for each Future that is behind the Ask. In small doses, this isn't a terribly big deal, but in the large, it adds up to being an additional drag on performance.

Note that this book was written in 2013, and while most of it is still relevant, Akka Streams and the backpressure approach of Reactive Streams have negated some of the discussion around choosing between push and pull. In any situation where either the producer(s) or consumer(s) could be overwhelmed, Akka Streams are definitely a better approach.

Thanks for reading the book, I hope you found it helpful!

Upvotes: 6

BatteryAcid
BatteryAcid

Reputation: 8881

Using a tell is preferred because:

No blocking waiting for a message. This gives the best concurrency and scalability characteristics.

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. http://doc.akka.io/docs/akka/2.4.1/java/untyped-actors.html#Send_messages

If there is a case where you use ask in a place where tell could have been used, then you've wasted resources. If you don't care about a response then why would you waste overhead (or resources) making your system return one.

Upvotes: 2

Related Questions