Search code examples
akkadaemonlong-running-processes

Akka Daemon Services


Most of the beginner's Akka examples seem to advocate calling the actor system's stop() and shutdown() methods like so:

object Main extends App {
    // create the ActorSystem
    val system = ActorSystem("HelloSystem")

    // put your actors to work here ...

    // shut down the ActorSystem when the work is finished
    system.stop
    system.shutdown
}

However what if your Akka app is meant to be a running service, that should (conceivably) live forever? Meaning it starts, the actor system is created, and actors simply idle until work (perhaps coming in from connected clients, etc.) needs to be done?

Is it OK to just initialize/start the actor system and leave it be (that is, omit invoking stop and shutdown altogether? Why/why not?


Solution

  • Yes, it is ok. This is a problem similar to AkkaHTTP implementation. In AkkaHTTP, you start actors which open a socket and wait for requests.

    One possible issue comes to my mind: if you need some short-living actors (inside your long-running service) to process a single request, you should stop them after they are no longer needed (to free resources), especially if the actors are stateful.

    I wrote a blog post about that issue: https://mikulskibartosz.name/always-stop-unused-akka-actors-a2ceeb1ed41