Error handling and recovery in Akka Streams

Error handling and recovery in Akka Streams

When developing applications you should assume that there will be unexpected issues. For this, Akka provides a set of supervision strategies to deal with errors within your actors. Akka streams is no different, in fact its error handling strategies used those actor supervision strategies as inspiration.

There are three ways to handle exceptions in your application code:

  • Stop - The stream is completed with failure
  • Resume - The problematic element is dropped and the stream continues
  • Restart - The problematic element is dropped and the stream continues after restarting the stage. Restarting the stage means that any accumulated state is cleared.

Default supervision strategy for a stream can be defined via the settings of the materializer. And this can be set for the whole stream or just for a particular stage.

 implicit val system = ActorSystem()
 implicit val ec = system.dispatcher

 val decider: Supervision.Decider = {
   case _: ArithmeticException =>
     println("Dropping element because of an ArithmeticException (i.e. Divide by zero)")
     Supervision.Resume
   case _ => Supervision.Stop
 }

 implicit val materializer = ActorMaterializer(
   ActorMaterializerSettings(system).withSupervisionStrategy(decider)
 )

 val source = Source(0 to 5).map(100 / _)
 val result = source.runWith(Sink.foreach(println))
                    .onComplete(_ => system.terminate())

Akka Streams also provides a RestartSourceRestartSinkRestartFlow for implementing the so called exponential backoff supervision strategy - starting a stage again when it fails each time with a growing time delay between restarts.

See more in the Java or Scala documentation.


    • Related Articles

    • Can Lagom errors be handled using Play error handling?

      No, the Play error handler is not invoked from Lagom, because the Lagom exception serializer intercepts it first. Any exception thrown by application code will be wrapped in a transport exception, serialized to json and a response is built from it. ...
    • How to implement batching logic in Akka Streams

      A common request we see with streaming data is the need to take the stream of elements and group them together (i.e. committing data to a database, a message queue or disk). Batching is usually a more efficient and performant solution than writing a ...
    • Error Handling Best Practices

      The best overview of the error handling options in the standard library we are aware of is tersesystems.com/2012/12/27/error-handling-in-scala, by Will Sargent, a former Lightbender. Digression: One topic he doesn’t cover is why checked exceptions ...
    • How to do Rate Limiting in Akka Streams

      In certain scenarios it is important to limit the number of concurrent requests to other services. For example, to avoid overwhelming the services and avoid performance degradation, or to maintain service level agreements. This is particularly ...
    • Can I customize error handling in Lagom?

      Yes, the best way to customize error handling in Lagom is to use a custom ExceptionSerializer.  This Knoldus blog post explains it well: https://blog.knoldus.com/2018/02/05/exception-serializer-in-lagom/ That example, however, is more focused on how ...