Reactive Scala Driver for MongoDB

Asynchronous & Non-Blocking

Failover Strategy

A FailoverStrategy defines if and how many times should ReactiveMongo retry a database operation (query, insertion, command, etc.) that failed for the following reasons:

The other causes (business errors, normal database errors, fatal errors, etc.) are not handled.

FailoverStartegy is a case class defined as follows:

/**
 * A failover strategy for sending requests.
 *
 * @param initialDelay the initial delay between the first failed attempt and the next one.
 * @param retries the number of retries to do before giving up.
 * @param delayFactor a function that takes the current iteration and returns a factor to be applied to the initialDelay.
 */
case class FailoverStrategy(
  initialDelay: FiniteDuration = 500 milliseconds,
  retries: Int = 5,
  delayFactor: Int => Double = n => 1)

The default FailoverStrategy retries 5 times, with 500 ms between each attempt. Let’s say that we want to define a FailoverStrategy that waits more time before a new attempt:

val strategy =
  FailoverStrategy(
    initialDelay = 500 milliseconds,
    retries = 5,
    delayFactor =
      attemptNumber => 1 + attemptNumber * 0.5
  )

This strategy retries at most 5 times, waiting for initialDelay * ( 1 + attemptNumber * 0.5 ) between each attempt (attemptNumber starting from 1). Here is the way the attempts will be run:

You can specify a strategy by giving it as a parameter to connection.db or db.collection:

val defaultStrategy = FailoverStrategy()

val customStrategy =
  FailoverStrategy(
    initialDelay = 500 milliseconds,
    retries = 5,
    delayFactor =
      attemptNumber => 1 + attemptNumber * 0.5
  )

// database-wide strategy
val db = connection.db("dbname", customStrategy)

// collection-wide strategy
val db = connection.db("dbname", defaultStrategy)
val collection = db.collection("collname", customStrategy)