akkaakka-supervisionakka-actor

What is the correct way to retry http calls from Akka Actors


I have an actor that makes Http call to an external service. At times that services responds with Http 404 and there are also http connection errors at times. Both these goes away when retried again.

What is the best way to retry the request by the actor.

What i can think of is

  1. Use supervisor strategy and restart actor

  2. Use a recursive method in the actor that retries the http call , max-retry count times

Which is the correct approach, 1 or 2. I think approach 1 will be an overkill for something as simple as retrying an Http call. Please share your recommendations.


Solution

  • In my opinion both of your approaches are valid.

    The first approach is in my eyes the more reactive way to do this in terms of embracing failure and letting the actor only do what it's supposed to do (instead of letting it handle retries etc.)

    There is a neat thing built in Akka Supervision: BackoffSupervisor

    In my eyes this is a perfect fit for problems where the actor fails due to external factors and it might make sense to wait a period of time to try again (as in your case with http calls).

    From the docs:

    val supervisor = BackoffSupervisor.props(
      Backoff.onFailure(
        childProps,
        childName = "myEcho",
        minBackoff = 3.seconds,
        maxBackoff = 30.seconds,
        randomFactor = 0.2 // adds 20% "noise" to vary the intervals slightly
      ))
    

    You can define a minimum and a maximum backoff and the supervisor will double the wait time before trying to restart the actor until it reaches the maximum. Only then it will stop trying.

    If you prefer your second option, I wouldn't go with a recursive method, but would schedule a message to the actor itself after a period of time to try the http call again:

    system.scheduler.scheduleOnce(1 seconds, self, TryAgain)