# Exponential backoff

In a variety of computer networks, binary exponential backoff or truncated binary exponential backoff refers to an algorithm used to space out repeated retransmissions of the same block of data, often as part of network congestion avoidance.

Wikipedia Exponential backoff

Here is an implementation in Java

``` /** * Calculate Exponential backoff * * @param attempt * number that we are checking * @param maxDelayInSeconds * Max amount of time to wait * @param multiplier * How much of backoff to perform * @return */ public static long backoff(final int attempt, final long maxDelayInSeconds, final double multiplier) { final double delayInSec = (Math.pow(2.0, attempt) - 1.0) * .5; return Math.round(Math.min(delayInSec * multiplier, maxDelayInSeconds)); }```

# Example

Here we have exponential backoff defined with three different parameters for the `muliplier and 120 seconds as the max time.`

``` ```  System.out.println(String.format("Attempt\t\t 1\t4\t8\n"));   for (int i = 0; i < 10; i++) { final long b1 = backoff(i, 120, 1); final long b2 = backoff(i, 120, 4); final long b3 = backoff(i, 120, 8);   System.out.println(String.format("%d\t\t %d\t%d\t%d", i, b1, b2, b3)); }``` 1 4 8 0 0 0 0 1 1 2 4 2 2 6 12 3 4 14 28 4 8 30 60 5 16 62 120 6 32 120 120 7 64 120 120 8 120 120 120 9 120 120 120 From the results above we can see that changing the multiplier can have significant implications. The larger the multiplier the faster we will be approaching our maxDelay and we will have longer paused between each attempt. In next post, we will create a Data Retrieval Service that will utilize Exponential backoff. ```
``` ```
``` ```
``` Post navigation ← Previous PostNext Post → Leave a Comment Your email address will not be published. Required fields are marked *Type here..Name* Email* Website Save my name, email, and website in this browser for the next time I comment. Δdocument.getElementById( "ak_js_1" ).setAttribute( "value", ( new Date() ).getTime() ); ```
``` ```
``` ```
``` ```
``` Copyright © 2024 Grzegorz Bugaj ```
``` var astra = {"break_point":"921","isRtl":"","is_scroll_to_id":"","is_scroll_to_top":"","is_header_footer_builder_active":"1"}; var starter_templates_zip_preview = {"AstColorPaletteVarPrefix":"--ast-global-color-","AstEleColorPaletteVarPrefix":["ast-global-color-0","ast-global-color-1","ast-global-color-2","ast-global-color-3","ast-global-color-4","ast-global-color-5","ast-global-color-6","ast-global-color-7","ast-global-color-8"]}; !function(e,n){if("undefined"!=typeof EnlighterJS){var o={"selectors":{"block":"pre.EnlighterJSRAW","inline":"code.EnlighterJSRAW"},"options":{"indent":4,"ampersandCleanup":true,"linehover":true,"rawcodeDbclick":false,"textOverflow":"break","linenumbers":true,"theme":"enlighter","language":"generic","retainCssClasses":false,"collapse":false,"toolbarOuter":"","toolbarTop":"{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}","toolbarBottom":""}};(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console); /(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+\$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)\$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1); ```