When Jobs Fail
A job is considered failed when:- The processor throws an exception
- The job becomes stalled and exceeds the
maxStalledCountsetting
Basic Job Retries
Enable automatic retries using theattempts 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:- 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: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
- 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:- 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: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 theUnrecoverableError:
Related Topics
Rate Limiting
Control job processing rate
Stalled Jobs
Understand and prevent stalled jobs
Unrecoverable Error
Prevent job retries
Job Options
Configure job behavior
