liveBook · Manning

Exponential backoff

As explained in Akka in Action, Second Edition

A backoff strategy delays successive retries or restarts after failures, typically by progressively increasing the delay.

Exponential backoff spaces out repeated actor restarts when a dependency, such as a database, is unresponsive or overloaded. Each retry waits longer than the previous one, up to a maximum delay. section 5.5.2

Recreating a database writer

A database writer recreates its connection after DbBrokenConnectionException. The first restart waits a few seconds; later restarts wait progressively longer until the maximum. Random delay prevents multiple writers from retrying simultaneously. After 15 seconds without backoff, the next delay returns to the minimum. section 5.5.2

Listing 5.6 DbWriter sketch implementation
object DbWriter {
 
  sealed trait Command
  final case class Line(time: Long, message: String, messageType: String)
      extends Command
 
  def apply(databaseUrl: String): Behavior[Command] =
    supervisorStrategy {                                                   #1
      Behaviors.setup[Command] { context =>
        // creates connection using databaseUrl
        Behaviors
          .receiveMessage[Command] {
            case Line(t, m, mt) => 
            ...                                                            #2
            ... #B                                                         #3
          }
          .receiveSignal {
            case (_, PostStop) => 
            ...                                                            #1
            case (_, PreRestart) => 
            ...                                                            #1
          }
      }
    }
 
  def supervisorStrategy(behavior: Behavior[Command]): Behavior[Command] = #4
    Behaviors
      .supervise {
        Behaviors
          .supervise(behavior)
          .onFailure[UnexpectedColumnsException]
          .SupervisorStrategy.resume
      }
      .onFailure[DbBrokenConnectionException](SupervisorStrategy
        .restartWithBackoff(minBackoff = 3, maxBackoff = 30, randomFactor = 0.1)
        .withResetBackoffAfter(15))
}

Use the database-writer sketch to locate the supervised actor whose setup creates the connection and whose stop or restart handlers prevent resource leaks. section 5.5.2

How Akka in Action, Second Edition covers it

How deep it goes
Explains increasing delays, maximum waiting time, randomness, and reset behavior without specifying the underlying scheduling implementation.
Examples
Uses a supervised database writer that recreates connections after DbBrokenConnectionException.
What it assumes
Assumes actor supervision and restart behavior, while distinguishing connection failures from data-related failures.
What you can do after
Operate actor restarts that reduce synchronized retry bursts and give failing database services time to recover.
How it connects
Connects actor supervision, restart strategies, database connections, and resource cleanup.

Also explained in

Compare how 8 books explain exponential backoff →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.