- NAME
- VERSION
- SYNOPSIS
- DESCRIPTION
- METHODS
- HOMEPAGE
- SOURCE
- BUGS
- SEE ALSO
- AUTHOR
- COPYRIGHT AND LICENSE

# NAME

Algorithm::Backoff::Exponential - Backoff exponentially

# VERSION

This document describes version 0.009 of Algorithm::Backoff::Exponential (from Perl distribution Algorithm-Backoff), released on 2019-06-20.

# SYNOPSIS

```
use Algorithm::Backoff::Exponential;
# 1. instantiate
my $ab = Algorithm::Backoff::Exponential->new(
#consider_actual_delay => 1, # optional, default 0
#max_actual_duration => 0, # optional, default 0 (retry endlessly)
#max_attempts => 0, # optional, default 0 (retry endlessly)
#jitter_factor => 0.25, # optional, default 0
initial_delay => 5, # required
#max_delay => 100, # optional
#exponent_base => 2, # optional, default 2 (binary exponentiation)
#delay_on_success => 0, # optional, default 0
);
# 2. log success/failure and get a new number of seconds to delay, timestamp is
# optional but must be monotonically increasing.
# for example, using the parameters initial_delay=5, max_delay=100:
my $secs;
$secs = $ab->failure(); # => 5 (= initial_delay)
$secs = $ab->failure(); # => 10 (5 * 2^1)
$secs = $ab->failure(); # => 20 (5 * 2^2)
$secs = $ab->failure(); # => 33 (5 * 2^3 - 7)
$secs = $ab->failure(); # => 80 (5 * 2^4)
$secs = $ab->failure(); # => 100 ( min(5 * 2^5, 100) )
$secs = $ab->success(); # => 0 (= delay_on_success)
```

Illustration using CLI show-backoff-delays (10 failures followed by 3 successes):

```
% show-backoff-delays -a Exponential --initial-delay 1 --max-delay 200 \
0 0 0 0 0 0 0 0 0 0 1 1 1
1
2
4
8
16
32
64
128
200
200
0
0
0
```

# DESCRIPTION

This backoff algorithm calculates the next delay as:

` initial_delay * exponent_base ** (attempts-1)`

Only the `initial_delay`

is required. `exponent_base`

is 2 by default (binary exponential). For the first failure attempt (`attempts`

= 1) the delay equals the initial delay. Then it is doubled, quadrupled, and so on (using the default exponent base of 2).

There are limits on the number of attempts (`max_attempts`) and total duration (`max_actual_duration`).

It is recommended to add a jitter factor, e.g. 0.25 to add some randomness to avoid "thundering herd problem".

# METHODS

## new

Usage:

` new(%args) -> obj`

This function is not exported.

Arguments ('*' denotes required arguments):

**consider_actual_delay**=>*bool*(default: 0)Whether to consider actual delay.

If set to true, will take into account the actual delay (timestamp difference). For example, when using the Constant strategy of delay=2, you log failure() again right after the previous failure() (i.e. specify the same timestamp). failure() will then return ~2+2 = 4 seconds. On the other hand, if you waited 2 seconds before calling failure() again (i.e. specify the timestamp that is 2 seconds larger than the previous timestamp), failure() will return 2 seconds. And if you waited 4 seconds or more, failure() will return 0.

**delay_on_success**=>*ufloat*(default: 0)Number of seconds to wait after a success.

**exponent_base**=>*ufloat*(default: 2)**initial_delay*** =>*ufloat*Initial delay for the first attempt after failure, in seconds.

**jitter_factor**=>*float*How much to add randomness.

If you set this to a value larger than 0, the actual delay will be between a random number between original_delay * (1-jitter_factor) and original_delay * (1+jitter_factor). Jitters are usually added to avoid so-called "thundering herd" problem.

The jitter will be applied to delay on failure as well as on success.

**max_actual_duration**=>*ufloat*(default: 0)Maximum number of seconds for all of the attempts (0 means unlimited).

If set to a positive number, will limit the number of seconds for all of the attempts. This setting is used to limit the amount of time you are willing to spend on a task. For example, when using the Exponential strategy of initial_delay=3 and max_attempts=10, the delays will be 3, 6, 12, 24, ... If failures are logged according to the suggested delays, and max_actual_duration is set to 21 seconds, then the third failure() will return -1 instead of 24 because 3+6+12 >= 21, even though max_attempts has not been exceeded.

**max_attempts**=>*uint*(default: 0)Maximum number consecutive failures before giving up.

0 means to retry endlessly without ever giving up. 1 means to give up after a single failure (i.e. no retry attempts). 2 means to retry once after a failure. Note that after a success, the number of attempts is reset (as expected). So if max_attempts is 3, and if you fail twice then succeed, then on the next failure the algorithm will retry again for a maximum of 3 times.

**max_delay**=>*ufloat*Maximum delay time, in seconds.

**min_delay**=>*ufloat*(default: 0)Maximum delay time, in seconds.

Return value: (obj)

# HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Algorithm-Backoff.

# SOURCE

Source repository is at https://github.com/perlancar/perl-Algorithm-Backoff.

# BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Algorithm-Backoff

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

# SEE ALSO

https://en.wikipedia.org/wiki/Exponential_backoff

Other `Algorithm::Backoff::*`

classes.

# AUTHOR

perlancar <perlancar@cpan.org>

# COPYRIGHT AND LICENSE

This software is copyright (c) 2019 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.