CodeBlanks Logo Icon CodeBlanks

Retry and backoff

Learn Bull retry and backoff strategies.

Retry and backoff

If an error occurs while processing a message, Bull automatically retries. Backoff is the wait time between retries. It can be fixed or exponential. This lets temporary errors be overcome automatically.

Example: queue.add(data, { attempts: 3, backoff: { type: "exponential", delay: 1000 } });. The job is retried at most 3 times; the wait grows exponentially (1s, 2s, 4s). Without backoff, a failing job would be retried immediately and stress the system. Backoff is ideal for temporary network errors or service outages. attempts and backoff are used together; attempts sets the count, backoff sets the wait. If all retries fail, the job is permanently failed and queue.on("failed", ...) fires.

Backoff types

There are two backoff types: fixed (a constant wait between retries) and exponential (the wait grows exponentially). Exponential protects the system; each retry waits longer.

Example: { type: "fixed", delay: 2000 } waits 2 seconds between each retry. { type: "exponential", delay: 1000 } grows as 1s, 2s, 4s, 8s. Exponential does not stress the system during long outages; early retries are fast, later ones slow. Fixed is suitable when you want a short, predictable wait. The backoff delay is in milliseconds. Bull also lets you define custom backoff strategies, but fixed and exponential are enough for most cases.

Practice Add retry and backoff

Use the kuyruk object below. Add a message with retry and backoff using kuyruk.add({ action: "email" }, { attempts: 3, backoff: { type: "exponential", delay: 1000 } }) (use await). Print "Retry set" with console.log.

Starter code

index.js
const Bull = require("bull");
const kuyruk = new Bull("job-queue");

// Inside an async function, add a message with attempts and backoff
// write here

// Print "Retry set"
// write here

Sample solution

index.js
const Bull = require("bull");
const kuyruk = new Bull("job-queue");
async function add() {
  await kuyruk.add({ action: "email" }, { attempts: 3, backoff: { type: "exponential", delay: 1000 } });
}
add();
console.log("Retry set");

Expected output

Retry set

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the attempts value

Question
queue.add(data, { ____: 3, backoff: { type: "exponential", delay: 1000 } });
Answer
queue.add(data, { attempts: 3, backoff: { type: "exponential", delay: 1000 } });

Explanation: attempts sets the retry count.

2 Match each type to its trait

Term Meaning
fixed Constant wait
exponential Exponentially growing wait
attempts Retry count
delay Wait duration (ms)

3 Why use backoff?

index.js
{ attempts: 3, backoff: { type: "exponential" } }
  • To back up Redis
  • To respond faster
  • To automatically overcome temporary errors Correct answer
  • To update the user interface

Explanation: Backoff overcomes temporary errors automatically.

All lessons in Advanced