# Retry Strategies

Message retry strategies allow you to specify how much of a delay should occur before retrying a message.

Delays between retries can be useful when messages fail handling due to race conditions, service unavailability, or concurrency reasons.

By default, @node-ts/bus uses a `DefaultRetryStrategy` that exponentially increases the delay between each retry attempt. Additionally, it will introduce a random variance of 10% for each delay to help unblock messages that are failing when processed at the same time.&#x20;

Additional strategies can be implemented to suit your application.

## Choosing a Retry Strategy

A retry strategy can be provided to the bus configuration on initialization by using `.withRetryStrategy()`

For example:

```typescript
const bus = await Bus.configure()
  .withRetryStrategy(DefaultRetryStrategy)
  .initialize()
```

## Custom retry strategies

A custom retry strategy can be provided by implementing the `RetryStrategy` from `` @node-ts/bus-core.` ``&#x20;

{% code title="retry-strategy.ts" %}

```typescript
export type Milliseconds = number

/**
 * Defines how a message retry strategy is to be implemented that calculates the delay between subsequent
 * retries of a message.
 */
export interface RetryStrategy {
  /**
   * Calculate the delay between retrying a failed message
   * @param currentAttempt How many attempts at handling the message have failed
   * @returns The number of milliseconds to delay retrying a failed message attempt
   */
  calculateRetryDelay (currentAttempt: number): Milliseconds
}

```

{% endcode %}

An example of a retry strategy is as follows

{% code title="default-retry-strategy.ts" %}

```typescript
import { Milliseconds, RetryStrategy } from './retry-strategy'

const MAX_DELAY_MS = 2.5 * 60 * 60 * 1000 // 2.5 hours
const JITTER_PERCENT = 0.1

/**
 * A default message retry strategy that exponentially increases the delay between retries
 * from 5ms to 2.5 hrs for the first 10 attempts. Each retry delay includes a jitter of
 * up to 10% to avoid deadlock-related errors from continually blocking.
 */
export class DefaultRetryStrategy implements RetryStrategy {
  calculateRetryDelay (currentAttempt: number): Milliseconds {
    const numberOfFailures = currentAttempt + 1
    const constantDelay: Milliseconds = Math.pow(5, numberOfFailures)
    
    const jitterAmount = Math.random() * JITTER_PERCENT * constantDelay
    const jitterDirection = Math.random() > 0.5 ? 1 : -1
    const jitter = jitterAmount * jitterDirection
    
    const delay = Math.round(constantDelay + jitter)
    return Math.min(delay, MAX_DELAY_MS)
  }
}

```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bus.node-ts.com/guide/retry-strategies.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
