Over a million developers have joined DZone.

Supervision and Monitoring in Akka

DZone's Guide to

Supervision and Monitoring in Akka

It's important to know your Akka actor hierarchy so you know how to react to failures. This guide covers the actor system and how you can supervise parents and children.

· Java Zone ·
Free Resource

Delivering modern software? Atomist automates your software delivery experience.

Supervision describes a dependency relationship between actors: the parent and child relationship. A parent is unique because it created the child actor, so the parent is responsible for reacting when failures happen in the child.

And the parent decides which choice needs to be selected. When a parent receives the failure signal from its child, depending on the nature of the failure, the parent decides from following options:

  • Resume: Parent starts the child actor, keeping its internal state.

  • Restart: Parent starts the child actor by clearing its internal state.

  • Stop: Stop the child permanently.

  • Escalate: Escalate the failure by failing itself and propagating the failure to its parent.


Akka Actor Lifecycle

It is always important to view all parts of the supervision hierarchy, which explains the escalate option. Each supervisor should cover all possible failure cases.

Actor System

Actor System

/user: The User Guardian Actor

An actor created using system.actorOf() are children of the user guardian actor. Whenever a user guardian terminates, all user-created actors will be terminated, too. Top-level user-created actors are determined by the user guardian actor as to how they will be supervised. Root Guardian is the supervisor of the user guardian.

/root: The Root Guardian

The root guardian actor is the father of the actor system. It supervises the user guardian actor and system guardian actor.

Supervision Strategies

There are two types of supervision strategies that we follow to supervise any actor:

  1. One-For-One

  2. One-For-All

case object ResumeException extends Exception
case object StopException extends Exception

case object RestartException extends Exception

override val supervisorStrategy =

    OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 second){

    case ResumeException => Resume

    case RestartException => Restart

    case StopException => Stop

    case _: Exception => Escalate 


Example of One-For-One Strategy

What is Monitoring?

Lifecycle Monitoring in Akka is usually referred to as DeathWatch

Monitoring is thus used to tie one actor to another so that it may react to the other actor’s termination, in contrast to supervision which reacts to failure.


Monitoring is particularly useful if a supervisor cannot simply restart its children and has to terminate them, such as in the case of errors during actor initialization. In that case, it should monitor those children and re-create them or schedule itself to retry this at a later time.

Code Repository: Supervision and Monitoring

Start automating your delivery right there on your own laptop, today! Get the open source Atomist Software Delivery Machine.

akka ,monitoring ,scala ,java ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}