Retry

Introduction

The retry strategy enables the re-execution of a user-defined callback if the previous execution fails. This approach gives you the option to either run the original callback again or specify a new callback for subsequent attempts. Implementing a retry strategy can boost the overall reliability of the system. However, it's essential to note that this improvement comes at the cost of increased resource utilization. If high availability is not a critical requirement, you may find the hedging strategy is more appropriate.

Installation

npm i @forts/resilience4ts-retry

Create and Configure a Retry

import { Retry } from '@forts/resilience4ts-retry';

const retry = Retry.of('my-retry', {
  maxAttempts: 3,
  backoff: 1000,
});

const result = await retry.on(async () => {
  // do something
});

Options

Config PropertyDefault ValueDescription
wait500Wait in milliseconds before retrying.
maxAttempts3Maximum number of attempts to retry.
maxInterval60000Maximum wait in milliseconds between retries.
retryModeRetryMode.LinearStrategy to use for calculating backoff.
validateResultFunction returning a boolean indicating whether the result should be retried.
whitelistErrorsArray of errors that should be ignored, skipping retry.
onRuntimeErrorCallback function to execute when an error occurs.