62

Akka 2.x requires many commands to reference an ActorSystem. So, to create an instance of an actor MyActor you might say:

val system = ActorSystem()
val myActor = system.actorOf(Props[MyActor])

Because of the frequent need for an ActorSystem: many code examples omit the creation from the code and assume that the reader knows where a system variable has come from.

If your code produces actors in different places, you could duplicate this code, possibly creating additional ActorSystem instances, or you could try to share the same ActorSystem instance by referring to some global or by passing the ActorSystem around.

The Akka documentation provides a general overview of systems of actors under the heading 'Actor Systems', and there is documentation of the ActorSystem class. But neither of these help a great deal in explaining why a user of Akka can't just rely on Akka to manage this under-the-hood.

Question(s)

  • What are the implications of sharing the same ActorSystem object or creating a new one each time?

  • What are the best practices here? Passing around an ActorSystem all the time seems surprisingly heavy-handed.

  • Some examples give the ActorSystem a name: ActorSystem("MySystem") others just call ActorSystem(). What difference does this make, and what if you use the same name twice?

  • Does akka-testkit require that you share a common ActorSystem with the one you pass to the TestKit constructor?

4
  • 1
    It sounds like you're programming outside of the ActorSystem too much. Can you share concrete pieces of code where you need to pass around the ActorSystem? FYI, within an actor you can obtain its system by: context.system May 1, 2012 at 14:00
  • 3
    Also, drexin is right, an ActorSystem is very heavyweight so only create one per logical application. May 1, 2012 at 14:01
  • 3
    Also, you should really not create many top level actors: "system.actorOf" as that creates a very pointless error kernel and doesn't scale as the creation of top-level actors needs to block to create the instance. May 1, 2012 at 14:08
  • 1
    Explaining why Akka cannot do it “under the hood” is that anything done there would have to be global, and we discovered that that simply does not scale (imagine several frameworks using Akka for different purposes, as is already the case). So we undertook the huge effort of removing all global state. Your “inconvenience” is small compared to that ;-) (you may just create your own singleton in your application, nothing should stop you) May 2, 2012 at 7:10

2 Answers 2

50

Creating an ActorSystem is very expensive, so you want to avoid creating a new one each time you need it. Also your actors should run in the same ActorSystem, unless there is a good reason for them not to. The name of the ActorSystem is also part the the path to the actors that run in it. E.g. if you create an actor in a system named MySystem it will have a path like akka://MySystem/user/$a. If you are in an actor context, you always have a reference to the ActorSystem. In an Actor you can call context.system. I don't know what akka-testkit expects, but you could take a look at the akka tests.

So to sum it up, you should always use the same system, unless there is a good reason not to do so.

5
  • 1
    "you should always use the same system" <- makes you wonder why the API allows you to construct multiple instances Mar 24, 2014 at 14:38
  • There are some use cases, where having multiple systems can make sense.
    – drexin
    Mar 24, 2014 at 15:10
  • Such as what? What about multi-tenant applications?
    – Isvara
    Apr 28, 2014 at 4:39
  • 4
    For example a framework could use an internal actor system that should not be accessible from the user code. Play does that for request handling.
    – drexin
    Apr 28, 2014 at 6:12
  • Does anybody have some data to compare the performances for different cases?
    – Haimei
    Mar 19, 2016 at 4:09
4

Here are some materials which might be helpful to understand "Why does document always suggest to use one ActorSystem for one logical application" :

  1. The heaviest part of an ActorSystem is the dispatcher. Each ActorSystem has at least one. The dispatcher is the engine that makes the actors running. In order to make running, it needs threads (usually got from a thread pool). The default dispatcher uses a fork-join thread pool with at least 8 threads.

  2. There are shared facilities, like the guardian actors, the event stream, the scheduler, etc. Some of them are in user space, some are internal. All of them need to be created and started.

  3. One ActorSystem with one thread pool configures to the numbers of cores should give the best results in most cases.

  4. Here document mentions logical application, I prefer to consider blocking or non-blocking application. According to dispatcher's configuration, one ActorSystem is for one configuration. If the application is for the same logics, one ActorSystem should be enough.

Here is a discussion , if you have time, you can read it. They discuss a lot, ActorSystem, local or remote, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.