What is backoff?
The 'backoff' npm package provides a mechanism to handle retry logic with exponential backoff and other backoff strategies. It is useful for scenarios where you need to retry operations that may fail intermittently, such as network requests or database queries.
What are backoff's main functionalities?
Exponential Backoff
This feature allows you to implement an exponential backoff strategy using the Fibonacci sequence. The code sample demonstrates how to set up a Fibonacci backoff with a randomisation factor, initial delay, and maximum delay. It also shows how to handle the 'ready' and 'fail' events.
const backoff = require('backoff');
const fibonacciBackoff = backoff.fibonacci({ randomisationFactor: 0.2, initialDelay: 10, maxDelay: 1000 });
fibonacciBackoff.on('ready', function(number, delay) {
console.log(`Retrying after ${delay} ms`);
// Perform the operation that needs to be retried
});
fibonacciBackoff.on('fail', function() {
console.log('Operation failed after maximum retries');
});
// Start the backoff process
fibonacciBackoff.backoff();
Retry with Custom Strategy
This feature allows you to implement a custom retry strategy using exponential backoff. The code sample demonstrates how to set up an exponential backoff with an initial delay and maximum delay. It also shows how to handle the 'ready' and 'fail' events.
const backoff = require('backoff');
const customStrategy = backoff.exponential({ initialDelay: 100, maxDelay: 10000 });
customStrategy.on('ready', function(number, delay) {
console.log(`Retry attempt #${number} after ${delay} ms`);
// Perform the operation that needs to be retried
});
customStrategy.on('fail', function() {
console.log('Operation failed after maximum retries');
});
// Start the backoff process
customStrategy.backoff();
Handling Errors
This feature demonstrates how to handle errors during the retry process. The code sample shows how to simulate an operation that may fail and how to retry the operation using exponential backoff until it succeeds or reaches the maximum number of retries.
const backoff = require('backoff');
const exponentialBackoff = backoff.exponential({ initialDelay: 100, maxDelay: 10000 });
exponentialBackoff.on('ready', function(number, delay) {
console.log(`Retry attempt #${number} after ${delay} ms`);
// Simulate an operation that may fail
const success = Math.random() > 0.5;
if (!success) {
exponentialBackoff.backoff();
} else {
console.log('Operation succeeded');
}
});
exponentialBackoff.on('fail', function() {
console.log('Operation failed after maximum retries');
});
// Start the backoff process
exponentialBackoff.backoff();
Other packages similar to backoff
retry
The 'retry' package provides a similar functionality to 'backoff' by allowing you to retry operations with configurable strategies. It supports exponential backoff, custom retry strategies, and error handling. Compared to 'backoff', 'retry' offers a more flexible API for defining custom retry logic.
promise-retry
The 'promise-retry' package is designed for retrying operations that return promises. It provides a simple API for retrying promise-based operations with configurable retry strategies, including exponential backoff. Compared to 'backoff', 'promise-retry' is more focused on promise-based workflows.
async-retry
The 'async-retry' package is another alternative that supports retrying asynchronous operations with configurable strategies. It works well with both promises and async/await syntax. Compared to 'backoff', 'async-retry' offers a more modern API that integrates seamlessly with async/await.
Exponential backoff implementation for Node.js
An exponential backoff implementation for Node.js.
Installation
npm install backoff
Usage
The Backoff object inherits from EventEmitter. One can listen for
backoff completion by listening for 'backoff' events. Registered handlers
will be called with the current backoff number and delay.
var Backoff = require('backoff');
var backoff = new Backoff();
backoff.on('backoff', function(number, delay) {
backoff.backoff();
});
backoff.backoff();
It's also possible to reset 'Backoff' instance. Once reset, a 'Backoff'
instance can be reused. On reset, the 'reset' event will be emitted.
var Backoff = require('backoff');
var backoff = new Backoff();
backoff.on('backoff', function(number, delay) {
backoff.backoff();
});
backoff.on('reset', function() {
console.log('reset');
});
backoff.backoff();
setTimeout(function() {
backoff.reset();
}, 5000);