10

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?

multitask landscape
  • 8,273
  • 3
  • 33
  • 31
Cristian Vrabie
  • 3,972
  • 5
  • 30
  • 49

1 Answers1

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
  • 1
    The 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
  • 1
    But 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
  • 3
    No, 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
  • 1
    Is this also valid for a clustered Akka setup? – Alex Khvatov Mar 03 '16 at 16:29