Algori...::MIMD(3pm)

πŸ“¦
version 0.010 Β· 5 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

okay so bestie, welcome to Algorithm::Backoff::MIMD β€” that's Multiplicative Increment, Multiplicative Decrement (MIMD) backoff, and yes the name is a whole mouthful but we're gonna get through it together, fr fr.


✨ VERSION ✨

This doc describes version 0.010 of Algorithm::Backoff::MIMD, which ships in the Perl distribution Algorithm-Backoff, released on 2024-02-24. πŸ’…


🌸 SYNOPSIS 🌸

first you gotta instantiate the thing, like:

use Algorithm::Backoff::MIMD;

# 1. instantiate

my $ab = Algorithm::Backoff::MIMD->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
    min_delay              => 2, # optional, default 0
    #max_delay             => 100, # optional
    initial_delay              => 3,   # required
    delay_multiple_on_failure  => 2,   # required
    delay_multiple_on_success  => 0.5, # required
);

then you log your wins and your Ls and it hands you back a new number of seconds to delay. the timestamp is optional but it has to be monotonically increasing, okay? no time travel. πŸ•°οΈ

# 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=3,
# delay_multiple_on_failure=2, delay_multiple_on_success=0.5, min_delay=2:

my $secs;
$secs = $ab->failure();   # => 3    (= initial_delay)
$secs = $ab->failure();   # => 6    (3 * 2)
$secs = $ab->failure();   # => 12   (6 * 2)
$secs = $ab->success();   # => 6    (12 * 0.5)
$secs = $ab->success();   # => 3    (6 * 0.5)
$secs = $ab->success();   # => 2    (max(3*0.5, min_delay=2))
$secs = $ab->failure();   # => 4    (2 * 2)

and here's a little illustration using the CLI show-backoff-delays β€” that's 4 failures followed by 5 successes, followed by 3 failures:

% show-backoff-delays -a MIMD --initial-delay 3 --min-delay 2 \
    --delay-multiple-on-failure 2 --delay-multiple-on-success 0.5 \
    0 0 0 0   1 1 1 1 1   0 0 0
3
6
12
24
12
6
3
2
2
4
8
16

πŸ¦‹ DESCRIPTION πŸ¦‹

So the tea is: when you fail, this backoff algorithm computes the next delay like this:

D1 = initial_delay
D2 = max(min(D1 * delay_multiple_on_failure, max_delay), min_delay)
...

and when you succeed, the next delay goes:

D1 = initial_delay
D2 = max(min(D1 * delay_multiple_on_success, max_delay), min_delay)
...

initial_delay, delay_multiple_on_failure, and delay_multiple_on_success are required, no skipping them. and heads up: initial_delay and min_delay should be larger than zero β€” otherwise every single next delay is just gonna be zero. πŸ’€ yeah. all of them.

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

Also, it is recommended to add a jitter factor, e.g. 0.25, to add some randomness to avoid the "thundering herd problem". 🐎🐎🐎 nobody wants a stampede.


πŸ‘‘ METHODS πŸ‘‘

πŸŽ€ new πŸŽ€

Usage:

new(%args) -> obj

This function is not exported, so don't go looking for it in your namespace.

Arguments ('*' denotes required arguments):

  • consider_actual_delay => bool (default: 0) β€” Whether to consider actual delay.

If set to true, it 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. you 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. you 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. time math, bestie. ⏳

  • delay_multiple_on_failure* => ufloat β€” How much to multiple previous delay, upon failure (e.g. 1.5).
  • delay_multiple_on_success* => ufloat β€” How much to multiple previous delay, upon success (e.g. 0.5).
  • 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 the so-called "thundering herd" problem.

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

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

If set to a positive number, it 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. budget's blown, we're out. πŸ™ˆ

  • 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. we love a fresh start. 🫢

  • max_delay => ufloat β€” Maximum delay time, in seconds.
  • min_delay => ufloat (default: 0) β€” Maximum delay time, in seconds.

Return value: (obj) β€” yes, you get an object back, that's the whole point.


🌈 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>.


πŸ’œ SEE ALSO πŸ’œ

  • Algorithm::Backoff::LILD
  • Algorithm::Backoff::LIMD
  • Algorithm::Backoff::MILD
  • Algorithm::Backoff
  • Other Algorithm::Backoff::* classes. the whole family, iconic. πŸ‘‘

πŸ“ AUTHOR πŸ“

perlancar <perlancar@cpan.org> β€” thank you for your service, king. 🫑


🧚 CONTRIBUTING 🧚

To contribute, you can send patches by email/via RT, or send pull requests on GitHub. (iykyk)

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

% prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me. no cap.


This software is copyright (c) 2024, 2019 by perlancar <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.


πŸ’€ 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. receipts, bestie, always bring receipts. πŸ“


and that's the whole page, mwah πŸ’‹ β€” this was perl v5.40.1, dated 2025-05-04, from the Algori...::MIMD(3pm) manual. go forth and back off (respectfully). πŸ¦‹βœ¨

πŸ“¦ where this page comes from