I'm just starting to work with Akka and I can't decide if I should use dependency injection (like cake pattern) or actor lookup in order to decouple the actors from each others. What is the preferred method?
Asked
Active
Viewed 2,602 times
1 Answers
20
You should prefer to introduce actors to each other, which means to send an ActorRef in or with a message or to pass it into a constructor. The latter may involve the cake pattern of you so choose, but lookup is much more expensive and therefore you should use real ActorRef whenever possible.

Roland Kuhn
- 15,412
- 2
- 36
- 45
-
I imagined so, but I was planning to use the lookup in the initialisation phase, not each time they were needed. That being said I still tend to agree with you and use the more classical type of dependency injection though I don't have a proper reason in mind. – Cristian Vrabie Jul 22 '13 at 08:01
-
1The comment about lookups being more expensive was not only meant with regards to runtime cost, the implementation cost is also considerably higher as you intuitively figured out. – Roland Kuhn Jul 22 '13 at 11:59
-
WIsh there were more opinions and explanations. – Yuri Geinish Jan 28 '15 at 11:03
-
1But ActorRef changes if the actor is restarted right? So the passed ActorRef would not work anymore. If ever that actor restarted. – Andrew James Ramirez Feb 10 '16 at 03:54
-
3No, ActorRef stays valid across a restart—this is the difference between Akka and Erlang. Only when the actor terminates does the ActorRef become invalid. – Roland Kuhn Feb 10 '16 at 07:51
-
1Is this also valid for a clustered Akka setup? – Alex Khvatov Mar 03 '16 at 16:29