Calculating an Exponential Back Off Delay Based on Failed Attempts

February 19, 2013 — 4 Comments

I implemented the following formula found on Wikipedia in C#.

529ec05329177ea4052fd5b469edf682

Furthermore, I added the ability to set a maximum delay that is quite useful when you want to control your delays. The calculated delay is in seconds.

public static class DelayCalculator
{
    public static Int64 ExponentialDelay(int failedAttempts,
                                    int maxDelayInSeconds = 1024)
    {
        //Attempt 1     0s     0s
        //Attempt 2     2s     2s
        //Attempt 3     4s     4s
        //Attempt 4     8s     8s
        //Attempt 5     16s    16s
        //Attempt 6     32s    32s

        //Attempt 7     64s     1m 4s
        //Attempt 8     128s    2m 8s
        //Attempt 9     256s    4m 16s
        //Attempt 10    512     8m 32s
        //Attempt 11    1024    17m 4s
        //Attempt 12    2048    34m 8s

        //Attempt 13    4096    1h 8m 16s
        //Attempt 14    8192    2h 16m 32s
        //Attempt 15    16384   4h 33m 4s

        var delayInSeconds = ((1d / 2d) * (Math.Pow(2d, failedAttempts) - 1d));

        return maxDelayInSeconds < delayInSeconds
            ? Convert.ToInt64(maxDelayInSeconds)
            : Convert.ToInt64(delayInSeconds);
    }
}

 

The code from this Post is part of the Brisebois.WindowsAzure NuGet Package

To install Brisebois.WindowsAzure, run the following command in the Package Manager Console

PM> Install-Package Brisebois.WindowsAzure

Get more details about the Nuget Package.

4 responses to Calculating an Exponential Back Off Delay Based on Failed Attempts

  1. 

    Actually I use a Fibonacci formula for something like this but I don’t think one is better than the other. Chances that an operation could fail every 2 seconds exactly (which is a fraction of the delay of every attempts using your exponential formula) are pretty slim.

    Like

Trackbacks and Pingbacks:

  1. Dew Drop – February 20, 2013 (#1,502) | Alvin Ashcraft's Morning Dew - February 20, 2013

    […] Calculating an Exponential Back Off Delay Based on Failed Attempts (Alexandre Brisebois) […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s