exponentialDelay

fun exponentialDelay(initialDelay: Duration = 500L.milliseconds, multiplier: Double = 2.0, maxDelay: Duration = 1.minutes, randomizationFactor: Double = 0.0)(source)

Configures the retry delay strategy to use the exponential backoff algorithm. The delay between retries is calculated using the formula: The algorithm is based on the formula:

  • (initialDelay * multiplier^(attempt - 1)) + jitter, where attempt is the current delay attempt which starts at 1.

Example:

exponential(500.milliseconds, 2.0, 1.minutes)
// Delay between transitions will be as follows:
// [500ms, 1s, 2s, 4s, 8s, 16s, 32s, 1m, 1m, 1m, ...]
exponential(500.milliseconds, 2.0, 1.minutes, 0.1)
// Delay between transitions will be something like:
// [450ms, 1.1s, 1.4s, 2.2s, 2.3s, 3.1s, 3.4s, 4s, 4s, 4s, ...]

Note:

  • Because the jitter calculation is based on the newly calculated delay, the new delay could be less than the previous value.

Parameters

initialDelay

the initial delay before the first retry.

multiplier

the multiplier to increase the delay between retries.

maxDelay

the maximum delay between retries. Used as a safety net to prevent infinite delays.

randomizationFactor

the randomization factor to add randomness to the calculated delay (e.g., 0.1 for +/-10%).

See also