QueueKeeper

Home / Guides

Oban retry storms

A retry storm is many jobs of one worker failing at once and being rescheduled: a third-party API is down, a credential expired, or a deploy broke a code path. Each failure records an error, sets the job to retryable and schedules the next attempt with backoff, so the same broken dependency keeps getting hit and real failures get buried.

Why it lasts so long

Workers default to max_attempts: 20. Oban's default backoff is exponential with 15 seconds of padding and some jitter; per Oban's docs, with maximum jitter the 20th attempt comes about 13 days and 8 hours after the first. A storm that starts today can keep producing attempts for days, and jobs that run out of attempts end up discarded.

Find the worker

Jobs whose last attempt failed in the last five minutes and are waiting to retry, per worker. A failed job keeps the attempted_at of the attempt that failed; scheduled_at is the next attempt.

SELECT worker, queue, count(*) AS failed_recently
FROM oban_jobs
WHERE state = 'retryable'
  AND attempted_at >= timezone('utc', now()) - interval '5 minutes'
GROUP BY worker, queue
ORDER BY failed_recently DESC;

The same query from Elixir (add prefix: to Repo.all/2 if Oban uses a non-default prefix):

import Ecto.Query

cutoff = DateTime.add(DateTime.utc_now(), -300, :second)

from(j in Oban.Job,
  where: j.state == "retryable" and j.attempted_at >= ^cutoff,
  group_by: [j.worker, j.queue],
  select: {j.worker, j.queue, count(j.id)}
)
|> MyApp.Repo.all()

Read the error

errors is an array of maps with attempt, at and error; on PostgreSQL it is a jsonb[], so the last entry is errors[array_length(errors, 1)]. Grouping by the first line of the error usually shows one cause:

SELECT split_part(errors[array_length(errors, 1)] ->> 'error', E'\n', 1) AS last_error,
       count(*) AS jobs,
       max(attempt) AS max_attempt
FROM oban_jobs
WHERE state = 'retryable'
  AND worker = 'MyApp.Workers.Sync'
  AND attempted_at >= timezone('utc', now()) - interval '1 hour'
GROUP BY 1
ORDER BY jobs DESC
LIMIT 10;

Error text can contain customer data from exception messages, so be careful where you paste it.

Stop the bleeding

  • Pause the queue if the worker has its own queue: Oban.pause_queue(queue: :sync) pauses it on every node, Oban.resume_queue(queue: :sync) resumes it. Pausing stops every worker in that queue, not just the failing one.
  • Cancel the waiting retries for one worker, then retry them once the cause is fixed. Oban.retry_all_jobs/1 skips available and executing jobs.
import Ecto.Query

query = from(j in Oban.Job,
  where: j.worker == "MyApp.Workers.Sync" and j.state == "retryable")

{:ok, _count} = Oban.cancel_all_jobs(query)

# later, after the fix
from(j in Oban.Job,
  where: j.worker == "MyApp.Workers.Sync" and j.state == "cancelled")
|> Oban.retry_all_jobs()

Prevent the next one

  • Return {:cancel, reason} from perform/1 for errors that will never succeed on retry (a deleted record, a 4xx you can't fix), so they stop instead of burning 20 attempts.
  • Set max_attempts per worker to what the job is worth retrying.
  • Implement backoff/1 when the default curve is wrong for the dependency, for example a fixed delay while a rate-limited API recovers.
  • Alert on discarded jobs as well: they are where a storm ends, and they fail silently.

How queue_keeper_check detects this

:retry_storm counts, per worker, jobs that failed or were retried within window (default 300 seconds):

  • retryable jobs whose last failed attempt started in the window,
  • executing jobs on attempt 2 or later that started in the window,
  • discarded or cancelled jobs on attempt 2 or later that were discarded or cancelled in the window,
  • with include_completed: true, also completed jobs on attempt 2 or later (off by default because it needs a full table scan).

It fires at min_retries (default 20) and is critical at twice that. A job that failed before the window and is still waiting out a long backoff is not counted. The separate :discarded check fires when a worker discards 5 or more jobs in 3600 seconds.

QueueKeeperCheck.run(MyApp.Repo, retry_storm: [window: 300, min_retries: 20])

If you run Oban's Pruner (default max_age 60 seconds), discarded and cancelled rows older than that are gone, so shrink the windows or raise max_age.

def deps do
  [
    {:queue_keeper_check, "~> 0.4.1"}
  ]
end

queue_keeper_check is a free, MIT-licensed, read-only health check for Oban (v0.4.1, early). It only runs SELECTs on oban_jobs, never reads job args, meta or errors, and sends nothing anywhere. Tested with Oban 2.24 on PostgreSQL 17, SQLite and MySQL 8.4; not tested with Oban Pro.

Documentation on HexDocs · Source on GitHub · Hex package

Related guides