Skip to main content
In any job processing system, some jobs will inevitably fail. BullMQ provides powerful retry mechanisms with built-in and custom backoff strategies to handle failures gracefully.

When Jobs Fail

A job is considered failed when:
  1. The processor throws an exception
  2. The job becomes stalled and exceeds the maxStalledCount setting
Exceptions must be Error objects for BullMQ to work correctly. Always throw proper Error instances. Consider using the ESLint no-throw-literal rule to enforce this.

Basic Job Retries

Enable automatic retries using the attempts option:
Without a backoff strategy, jobs are retried immediately upon failure.
Retried jobs respect their priority. When moved back to the waiting state, they maintain their original priority ordering.

Built-in Backoff Strategies

BullMQ provides two built-in backoff strategies: fixed and exponential.

Fixed Backoff

Retry after a constant delay:
Timeline example:
  • Attempt 1: Fails immediately
  • Attempt 2: After 1 second
  • Attempt 3: After 1 second

Fixed Backoff with Jitter

Add randomness to prevent thundering herd problems:
number
default:"0"
Value between 0 and 1. A jitter of 0.5 with delay 1000 produces random delays between 500ms and 1000ms.

Exponential Backoff

Retry with exponentially increasing delays:
Formula: 2^(attempt - 1) × delay Timeline example with 1000ms base:
  • Attempt 1: Fails immediately
  • Attempt 2: After 1 second (2^0 × 1000)
  • Attempt 3: After 2 seconds (2^1 × 1000)
  • Attempt 4: After 4 seconds (2^2 × 1000)
  • Attempt 5: After 8 seconds (2^3 × 1000)

Exponential Backoff with Jitter

Example delays with jitter 0.5:
  • Attempt 2: Between 1500ms and 3000ms
  • Attempt 3: Between 3000ms and 6000ms
  • Attempt 4: Between 6000ms and 12000ms
Jitter helps prevent multiple jobs from retrying simultaneously, which can overwhelm downstream services.

Default Backoff Strategy

Set a default backoff strategy for all jobs in a queue:

Custom Backoff Strategies

Implement your own backoff logic:
Timeline example:
  • Attempt 2: After 1 second (1 × 1000)
  • Attempt 3: After 2 seconds (2 × 1000)
  • Attempt 4: After 3 seconds (3 × 1000)

Advanced Custom Backoff

Access more parameters for sophisticated strategies:

Special Return Values

number
Return 0 to retry immediately. Jobs move to the end of the waiting list (priority 0) or maintain priority for prioritized jobs.
number
Return -1 to prevent retry. The job moves directly to the failed state.

Using Custom Backoff Types

Define multiple custom backoff strategies:
Use the custom backoff types when adding jobs:

Practical Examples

Example 1: API Calls with Retry

Example 2: Database Operations

Example 3: Email with Rate Limiting

Monitoring Retries

Track retry attempts and failures:

Best Practices

1

Use exponential backoff for external APIs

Exponential backoff with jitter prevents overwhelming recovering services.
2

Set appropriate attempt limits

Balance between persistence and resource waste. Most jobs should succeed within 3-5 attempts.
3

Add jitter to prevent thundering herds

Use jitter (0.3-0.5) when many jobs might fail simultaneously.
4

Don't retry permanent failures

Use custom backoff strategies to return -1 for validation errors or other permanent failures.
5

Log retry attempts

Monitor attemptsMade to identify problematic jobs or services.
6

Consider job-specific strategies

Use custom backoff types for different job categories with different retry requirements.

Stopping Retries

To prevent a job from retrying, use the UnrecoverableError:
See the Stop Retrying Jobs pattern for more details.

Rate Limiting

Control job processing rate

Stalled Jobs

Understand and prevent stalled jobs

Unrecoverable Error

Prevent job retries

Job Options

Configure job behavior

API Reference