Giter Site home page Giter Site logo

exponential-backoff's Introduction

exponential-backoff

Build Status Maven Central

A small Java API for running arbitrary tasks with exponential backoff

Usage

Sometimes you want to run a task and retry if it fails. For things like network and external service calls, retrying immediately may not be the most efficient use of your time. It is unlikely your service is going to be able to respond just a few milliseconds later (it might be down, for example). With exponential backoff, successive attempts are retried after waiting an amount of time, exponentially greater than the previous wait time.

While this is great for increasing client reliability, in the case where you may have multiple concurrent clients, using plain exponential backoff may still result in poor performance (see: https://www.awsarchitectureblog.com/2015/03/backoff.html)

This is where the concept of jitter (randomness) comes in. Plainly, using using jitter with exponential backoff means choosing a random wait versus a plain exponential wait time. The algorithm actually chooses between the random time or the exponential time, with equal probability given to both. While this may seem counter-intuitive, using jitter can greatly increase performance for competing clients (again, see the link above).

This library allows you to run arbitrary tasks which will retry with exponential backoff (and potentially jitter). If an exception is thrown anywhere in your task (and not caught), the task will be retried, waiting some amount of time between the next execution (determined by the backoff algorithm). There are a few examples in the unit tests ExponentialBackOffTest.java, however the general usage is as follows:

final BackOffResult<String> result = ExponentialBackOff.<String>builder()
        .withBase(100)
        .withCap(5000)
        .withMaxAttempts(5)
        .withJitter()
        .withTask(() -> "Do something")
        .retryIf(result -> result.equals("retry"))
        .withExceptionHandler(e -> System.out.println("Do something with " + e))
        .execute();
        
if (result.status == BackOffResult.SUCCESSFUL) {
    System.out.println("All done! Returned " + result.data.orElse("empty"));
} else {
    System.out.println("Failed to complete task within 5 attempts");
}

See the JavaDocs or source code for more specific information on usage. The preferred approach is to use the builder to generate and execute the tasks.

Dependency Information

Found on Maven Central (http://search.maven.org)

Gradle:

compile 'me.ccampo:exponential-backoff:1.1.3'

Maven:

<dependency>
  <groupId>me.ccampo</groupId>
  <artifactId>exponential-backoff</artifactId>
  <version>1.1.3</version>
</dependency>

Development

To build (OS X or -nix)

./gradlew build

or on Windows

gradlew.bat build

exponential-backoff's People

Contributors

ccampo133 avatar eaddario avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.