oeaoaueaa
oeaoaueaa

Reputation: 91

Akka.net actor selections or references

What is more efficient in Akka.net, talking to actor selections or interacting with an IActorRef being passed in a message?

Upvotes: 0

Views: 562

Answers (2)

Robin Cox
Robin Cox

Reputation: 41

I think perhaps you're coming at this in the wrong way. I don't know for sure which is 'more efficient' in terms of performance. (Although @Horusiath gives some great guidance on the differences between the two)

The thing is that using ActorSelection is a bit of an anti-pattern in itself and its generally recommended that you use IActorRefs. See point #3 on this blog: Petabridge: The Top 7 Mistakes Newbies Make with Akka.NET

In short; when using Actor Refs, the location of the actor is transparent. The actor you're attempting to interact with could be anywhere in your cluster and it wouldn't matter when using an IActorRef.

That being said, they also have a nice little blog on when ActorSelection might be useful... Petabridge: When Should I Use Actor Selection?

I'd recommend giving both of those links a read through if you're weighing up which to use in your code. Hope this helps!

Upvotes: 2

Bartosz Sypytkowski
Bartosz Sypytkowski

Reputation: 7542

The best answer would be to benchmark it for your case, as this may depend on multiple conditions (like local/remote communication). When we're talking about communication within the same process, IActorRef should always be faster. In remote communication the difference may be smaller, but stil in favor of IActorRef.

That being said, it's important to get the difference between two:

  • When underlying actors stops (don't confuse stop with restart) its IActorRef is no longer valid. If it will be created some time later, your old IActorRef doesn't necessary have to point to it. This is one of the reasons, why you may Context.Watch(actorRef) to be notified when your actor dies.
  • Actor selection doesn't point directly to an actor's mailbox, therefore usually it's slower. The actual recipient's mailbox is resolved when you're trying to send a message through it. It doesn't suffer invalidation issues, but cannot be watched either. It also may point to more than one actor (using wildcards), so your message may be delivered to multiple actors somewhere in the actor hierarchy.

Upvotes: 2

Related Questions