backoff vs async-retry vs promise-retry vs retry vs retry-axios vs retry-request
JavaScript Retry Libraries
backoffasync-retrypromise-retryretryretry-axiosretry-requestSimilar Packages:

JavaScript Retry Libraries

Retry libraries in JavaScript provide mechanisms to automatically retry failed asynchronous operations, such as API calls or database queries, to enhance reliability and resilience in applications. These libraries help manage transient errors by implementing various retry strategies, allowing developers to configure parameters like the number of retries, delay between attempts, and conditions for retrying. This is particularly useful in scenarios where network issues or temporary service outages may occur, ensuring that applications can gracefully handle failures without crashing or requiring manual intervention.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
backoff684,465337-1110 years agoMIT
async-retry01,908-305 years agoMIT
promise-retry0317-116 years agoMIT
retry01,260-195 years agoMIT
retry-axios050257.8 kB03 months agoApache-2.0
retry-request06916.1 kB37 months agoMIT

Feature Comparison: backoff vs async-retry vs promise-retry vs retry vs retry-axios vs retry-request

Retry Strategy

  • backoff:

    backoff provides built-in support for exponential backoff strategies, allowing you to define the initial delay and the factor by which the delay increases with each retry, making it ideal for handling rate limits.

  • async-retry:

    async-retry allows for custom retry strategies, including exponential backoff and fixed delays. You can specify how long to wait between retries and how many attempts to make before failing.

  • promise-retry:

    promise-retry offers a simple retry strategy that can be customized with a delay function, allowing you to control the timing of retries based on the error type or other conditions.

  • retry:

    retry supports both fixed and exponential backoff strategies, giving you the flexibility to define how retries are handled based on your application's needs.

  • retry-axios:

    retry-axios allows you to configure retry strategies directly in your Axios requests, including setting retry limits and delays, making it easy to manage HTTP request failures.

  • retry-request:

    retry-request provides a straightforward retry mechanism for HTTP requests, allowing you to specify the number of retries and the delay between attempts, suitable for simple use cases.

Integration

  • backoff:

    backoff is a standalone library that can be used with any asynchronous operation, making it versatile for different use cases beyond just HTTP requests.

  • async-retry:

    async-retry is designed to work independently and can be integrated into any promise-based workflow, providing flexibility in various environments.

  • promise-retry:

    promise-retry is specifically tailored for promises, ensuring seamless integration with promise-based APIs and workflows, making it easy to implement in modern JavaScript applications.

  • retry:

    retry can be used with both callbacks and promises, making it a versatile choice for applications that may use either approach for asynchronous operations.

  • retry-axios:

    retry-axios is specifically designed for use with Axios, providing a seamless integration that allows you to add retry logic directly to your HTTP requests without additional setup.

  • retry-request:

    retry-request is built to work with the request library, making it ideal for legacy applications that rely on this library for HTTP requests.

Configuration Options

  • backoff:

    backoff allows for extensive configuration of backoff strategies, including setting maximum retry limits and customizing the backoff function, making it highly adaptable to specific requirements.

  • async-retry:

    async-retry offers a wide range of configuration options, including the ability to customize the number of retries, delay between attempts, and conditions for retrying, providing great flexibility.

  • promise-retry:

    promise-retry provides straightforward configuration options, allowing you to specify the number of retries and a delay function, making it easy to set up and use.

  • retry:

    retry offers flexible configuration options for retry attempts, delays, and error handling, allowing you to tailor the retry logic to fit your needs.

  • retry-axios:

    retry-axios allows you to configure retry options directly in your Axios instance, including retry limits and conditions, making it convenient for developers familiar with Axios.

  • retry-request:

    retry-request provides basic configuration options for retry attempts and delays, making it easy to use without overwhelming complexity.

Error Handling

  • backoff:

    backoff provides mechanisms to handle specific errors and can be configured to retry only on certain error types, making it useful for managing different failure scenarios.

  • async-retry:

    async-retry allows you to specify error types that should trigger a retry, giving you control over which errors are retried and which are not, enhancing error handling capabilities.

  • promise-retry:

    promise-retry allows for custom error handling by enabling you to define conditions under which retries should occur, ensuring that only relevant errors trigger a retry.

  • retry:

    retry provides basic error handling capabilities, allowing you to specify which errors should be retried, but may require additional logic for more complex scenarios.

  • retry-axios:

    retry-axios integrates error handling directly into Axios, allowing you to specify which HTTP status codes should trigger a retry, making it easy to manage API errors.

  • retry-request:

    retry-request allows you to define which HTTP status codes should trigger a retry, providing a straightforward way to handle common HTTP errors.

Documentation and Community Support

  • backoff:

    backoff is well-documented with clear examples, and it has a supportive community that can assist with common use cases and issues.

  • async-retry:

    async-retry has comprehensive documentation and a growing community, making it easy to find examples and support for implementation.

  • promise-retry:

    promise-retry has straightforward documentation that covers the basics, making it easy for developers to get started quickly with examples and use cases.

  • retry:

    retry has decent documentation, but may lack some advanced examples; however, it is widely used, providing a level of community support through forums and GitHub.

  • retry-axios:

    retry-axios has good documentation that integrates with Axios documentation, making it easy for developers familiar with Axios to implement retry logic effectively.

  • retry-request:

    retry-request has basic documentation that covers its usage, but as the request library is deprecated, community support may be limited.

How to Choose: backoff vs async-retry vs promise-retry vs retry vs retry-axios vs retry-request

  • backoff:

    Select backoff if you require advanced exponential backoff strategies with customizable delay and maximum retry limits. It is particularly useful for handling rate-limited APIs or services that enforce throttling.

  • async-retry:

    Choose async-retry if you need a simple and flexible retry mechanism that supports both promises and async/await syntax. It allows for custom backoff strategies and is lightweight, making it suitable for most use cases.

  • promise-retry:

    Opt for promise-retry if you want a straightforward solution specifically designed for promises. It provides built-in support for retrying failed promises with customizable retry conditions and delays, making it easy to integrate into promise-based workflows.

  • retry:

    Use retry if you need a general-purpose retry library that works with both callbacks and promises. It offers a simple API and allows for flexible configuration of retry attempts and delays, suitable for various asynchronous operations.

  • retry-axios:

    Choose retry-axios if you are using Axios for HTTP requests and want to add retry functionality seamlessly. It integrates directly with Axios, allowing you to specify retry logic directly in your Axios configuration, making it ideal for RESTful API interactions.

  • retry-request:

    Select retry-request if you are working with the request library and need a retry mechanism for HTTP requests. It provides a straightforward way to add retry capabilities to your request calls, making it suitable for legacy applications that still use the request library.

README for backoff

Backoff for Node.js

Build Status NPM version

Fibonacci and exponential backoffs for Node.js.

Installation

npm install backoff

Unit tests

npm test

Usage

Object Oriented

The usual way to instantiate a new Backoff object is to use one predefined factory method: backoff.fibonacci([options]), backoff.exponential([options]).

Backoff inherits from EventEmitter. When a backoff starts, a backoff event is emitted and, when a backoff ends, a ready event is emitted. Handlers for these two events are called with the current backoff number and delay.

var backoff = require('backoff');

var fibonacciBackoff = backoff.fibonacci({
    randomisationFactor: 0,
    initialDelay: 10,
    maxDelay: 300
});

fibonacciBackoff.failAfter(10);

fibonacciBackoff.on('backoff', function(number, delay) {
    // Do something when backoff starts, e.g. show to the
    // user the delay before next reconnection attempt.
    console.log(number + ' ' + delay + 'ms');
});

fibonacciBackoff.on('ready', function(number, delay) {
    // Do something when backoff ends, e.g. retry a failed
    // operation (DNS lookup, API call, etc.). If it fails
    // again then backoff, otherwise reset the backoff
    // instance.
    fibonacciBackoff.backoff();
});

fibonacciBackoff.on('fail', function() {
    // Do something when the maximum number of backoffs is
    // reached, e.g. ask the user to check its connection.
    console.log('fail');
});

fibonacciBackoff.backoff();

The previous example would print the following.

0 10ms
1 10ms
2 20ms
3 30ms
4 50ms
5 80ms
6 130ms
7 210ms
8 300ms
9 300ms
fail

Note that Backoff objects are meant to be instantiated once and reused several times by calling reset after a successful "retry".

Functional

It's also possible to avoid some boilerplate code when invoking an asynchronous function in a backoff loop by using backoff.call(fn, [args, ...], callback).

Typical usage looks like the following.

var call = backoff.call(get, 'https://duplika.ca/', function(err, res) {
    console.log('Num retries: ' + call.getNumRetries());

    if (err) {
        console.log('Error: ' + err.message);
    } else {
        console.log('Status: ' + res.statusCode);
    }
});

call.retryIf(function(err) { return err.status == 503; });
call.setStrategy(new backoff.ExponentialStrategy());
call.failAfter(10);
call.start();

API

backoff.fibonacci([options])

Constructs a Fibonacci backoff (10, 10, 20, 30, 50, etc.).

The options are the following.

  • randomisationFactor: defaults to 0, must be between 0 and 1
  • initialDelay: defaults to 100 ms
  • maxDelay: defaults to 10000 ms

With these values, the backoff delay will increase from 100 ms to 10000 ms. The randomisation factor controls the range of randomness and must be between 0 and 1. By default, no randomisation is applied on the backoff delay.

backoff.exponential([options])

Constructs an exponential backoff (10, 20, 40, 80, etc.).

The options are the following.

  • randomisationFactor: defaults to 0, must be between 0 and 1
  • initialDelay: defaults to 100 ms
  • maxDelay: defaults to 10000 ms
  • factor: defaults to 2, must be greater than 1

With these values, the backoff delay will increase from 100 ms to 10000 ms. The randomisation factor controls the range of randomness and must be between 0 and 1. By default, no randomisation is applied on the backoff delay.

backoff.call(fn, [args, ...], callback)

  • fn: function to call in a backoff handler, i.e. the wrapped function
  • args: function's arguments
  • callback: function's callback accepting an error as its first argument

Constructs a FunctionCall instance for the given function. The wrapped function will get retried until it succeds or reaches the maximum number of backoffs. In both cases, the callback function will be invoked with the last result returned by the wrapped function.

It is the caller's responsability to initiate the call by invoking the start method on the returned FunctionCall instance.

Class Backoff

new Backoff(strategy)

  • strategy: the backoff strategy to use

Constructs a new backoff object from a specific backoff strategy. The backoff strategy must implement the BackoffStrategyinterface defined bellow.

backoff.failAfter(numberOfBackoffs)

  • numberOfBackoffs: maximum number of backoffs before the fail event gets emitted, must be greater than 0

Sets a limit on the maximum number of backoffs that can be performed before a fail event gets emitted and the backoff instance is reset. By default, there is no limit on the number of backoffs that can be performed.

backoff.backoff([err])

Starts a backoff operation. If provided, the error parameter will be emitted as the last argument of the backoff and fail events to let the listeners know why the backoff operation was attempted.

An error will be thrown if a backoff operation is already in progress.

In practice, this method should be called after a failed attempt to perform a sensitive operation (connecting to a database, downloading a resource over the network, etc.).

backoff.reset()

Resets the backoff delay to the initial backoff delay and stop any backoff operation in progress. After reset, a backoff instance can and should be reused.

In practice, this method should be called after having successfully completed the sensitive operation guarded by the backoff instance or if the client code request to stop any reconnection attempt.

Event: 'backoff'

  • number: number of backoffs since last reset, starting at 0
  • delay: backoff delay in milliseconds
  • err: optional error parameter passed to backoff.backoff([err])

Emitted when a backoff operation is started. Signals to the client how long the next backoff delay will be.

Event: 'ready'

  • number: number of backoffs since last reset, starting at 0
  • delay: backoff delay in milliseconds

Emitted when a backoff operation is done. Signals that the failing operation should be retried.

Event: 'fail'

  • err: optional error parameter passed to backoff.backoff([err])

Emitted when the maximum number of backoffs is reached. This event will only be emitted if the client has set a limit on the number of backoffs by calling backoff.failAfter(numberOfBackoffs). The backoff instance is automatically reset after this event is emitted.

Interface BackoffStrategy

A backoff strategy must provide the following methods.

strategy.next()

Computes and returns the next backoff delay.

strategy.reset()

Resets the backoff delay to its initial value.

Class ExponentialStrategy

Exponential (10, 20, 40, 80, etc.) backoff strategy implementation.

new ExponentialStrategy([options])

The options are the following.

  • randomisationFactor: defaults to 0, must be between 0 and 1
  • initialDelay: defaults to 100 ms
  • maxDelay: defaults to 10000 ms
  • factor: defaults to 2, must be greater than 1

Class FibonacciStrategy

Fibonnaci (10, 10, 20, 30, 50, etc.) backoff strategy implementation.

new FibonacciStrategy([options])

The options are the following.

  • randomisationFactor: defaults to 0, must be between 0 and 1
  • initialDelay: defaults to 100 ms
  • maxDelay: defaults to 10000 ms

Class FunctionCall

This class manages the calling of an asynchronous function within a backoff loop.

This class should rarely be instantiated directly since the factory method backoff.call(fn, [args, ...], callback) offers a more convenient and safer way to create FunctionCall instances.

new FunctionCall(fn, args, callback)

  • fn: asynchronous function to call
  • args: an array containing fn's args
  • callback: fn's callback

Constructs a function handler for the given asynchronous function.

call.isPending()

Returns whether the call is pending, i.e. hasn't been started.

call.isRunning()

Returns whether the call is in progress.

call.isCompleted()

Returns whether the call is completed.

call.isAborted()

Returns whether the call is aborted.

call.setStrategy(strategy)

  • strategy: strategy instance to use, defaults to FibonacciStrategy.

Sets the backoff strategy to use. This method should be called before call.start() otherwise an exception will be thrown.

call.failAfter(maxNumberOfBackoffs)

  • maxNumberOfBackoffs: maximum number of backoffs before the call is aborted

Sets the maximum number of backoffs before the call is aborted. By default, there is no limit on the number of backoffs that can be performed.

This method should be called before call.start() otherwise an exception will be thrown..

call.retryIf(predicate)

  • predicate: a function which takes in as its argument the error returned by the wrapped function and determines whether it is retriable.

Sets the predicate which will be invoked to determine whether a given error should be retried or not, e.g. a network error would be retriable while a type error would stop the function call. By default, all errors are considered to be retriable.

This method should be called before call.start() otherwise an exception will be thrown.

call.getLastResult()

Returns an array containing the last arguments passed to the completion callback of the wrapped function. For example, to get the error code returned by the last call, one would do the following.

var results = call.getLastResult();
// The error code is the first parameter of the callback.
var error = results[0];

Note that if the call was aborted, it will contain the abort error and not the last error returned by the wrapped function.

call.getNumRetries()

Returns the number of times the wrapped function call was retried. For a wrapped function that succeeded immediately, this would return 0. This method can be called at any point in time during the call life cycle, i.e. before, during and after the wrapped function invocation.

call.start()

Initiates the call the wrapped function. This method should only be called once otherwise an exception will be thrown.

call.abort()

Aborts the call and causes the completion callback to be invoked with an abort error if the call was pending or running; does nothing otherwise. This method can safely be called mutliple times.

Event: 'call'

  • args: wrapped function's arguments

Emitted each time the wrapped function is called.

Event: 'callback'

  • results: wrapped function's return values

Emitted each time the wrapped function invokes its callback.

Event: 'backoff'

  • number: backoff number, starts at 0
  • delay: backoff delay in milliseconds
  • err: the error that triggered the backoff operation

Emitted each time a backoff operation is started.

Event: 'abort'

Emitted when a call is aborted.

Annotated source code

The annotated source code can be found at mathieuturcotte.github.io/node-backoff/docs.

License

This code is free to use under the terms of the MIT license.