skip to content

With Sidekiq 8 in a Rails app, how is a job whose perform raises retried, and when does it reach the dead set?

level: middleimportance: must knowfreq 60%

answer

  1. two Redis sorted sets
  2. 25 retries by default, about three weeks
  3. delay grows with retry_count to the fourth
  4. retry: 0 vs retry: false
  5. dead set capped at 10,000 jobs, six months

basics

~20 s

Sidekiq puts a failed job in its retry set and reruns it with growing delays, 25 times over about three weeks by default. When retries run out it moves the job to the dead set, which keeps 10,000 jobs for six months.

solid answer

~40 s

When `perform` raises, Sidekiq's retry handler records the error on the payload, increments `retry_count` and adds the job to the **retry set**, a Redis sorted set scored by the next run time. The delay is roughly `retry_count ** 4 + 15` seconds plus random jitter, so the default 25 retries span about three weeks; a poller in each process pushes due retries back onto their queue. When the count is exhausted, the `sidekiq_retries_exhausted` block runs and the job goes to the **dead set**, trimmed to 10,000 jobs and six months, where it waits for someone to press Retry in the Web UI or call the API. `sidekiq_options retry: 5` changes the count; `retry: 0` sends a failing job straight to the dead set; `retry: false` neither retries nor keeps it.

code

ruby · 8 lines
ruby
# Rails console after deploying a fix to the image store client
rs = Sidekiq::RetrySet.new
rs.size                      # => 1840
rs.select { |job| job.display_class == "ThumbnailJob" }.each(&:retry)

ds = Sidekiq::DeadSet.new
ds.size                      # => 37
ds.retry_all                 # push every dead job back onto its queue

go deeper

for a junior

Know that a failing Sidekiq job is retried automatically, 25 times by default, and ends in the dead set where someone retries it by hand from the Web UI.

for a middle

Explain the mechanics: retry and dead sets as Redis sorted sets, the fourth-power backoff, the poller, and how retry: 0, retry: false and dead: false differ.

for a senior

Tune retries per job to the failure mode, alert on retry-set growth, clear backlogs safely after a fix, and keep the Web UI behind admin authentication.

for a principal

Decide which failures deserve three weeks of retries and which should die fast, and who owns the dead set so jobs are not silently lost after six months.

## The life of a failing Sidekiq job Sidekiq assumes a failure is usually a bug or a flaky dependency that will be fixed or recover, so by default it **retries automatically** rather than dropping the job. Everything happens in Redis, in two sorted sets that the Web UI shows as the **Retries** and **Dead** tabs. Here is the path for a stock-photo site's `ThumbnailJob` that raises because the image store is timing out: 1. The processor thread catches the exception around `perform`. 2. Sidekiq writes `error_class`, `error_message`, `failed_at` (first failure) or `retried_at` onto the payload and increments `retry_count`. 3. If retries remain, it computes the next run time and adds the payload to the `retry` sorted set with that time as the score. 4. A poller in every Sidekiq process checks the retry and scheduled sets every few seconds and pushes due jobs back onto their queue (or onto `retry_queue` if one is set). 5. If no retries remain, Sidekiq calls the class's `sidekiq_retries_exhausted` block, moves the payload into the `dead` sorted set and runs any global death handlers. ## How long between attempts The default delay before the next attempt is `retry_count ** 4 + 15` seconds plus a random jitter of up to `10 * (retry_count + 1)` seconds. That gives roughly: | Retry | Delay before it runs | |---|---| | 1st | about 15-25 seconds | | 5th | about 5 minutes | | 10th | about 2 hours | | 20th | about 1.5 days | | 25th | about 3.8 days | Added up, the default **25 retries take about three weeks**, which is deliberate: long enough for a fix to be deployed and the job to succeed on a later attempt. A class can replace the formula with `sidekiq_retry_in { |count, exception, job| ... }`, returning seconds, or the symbols `:discard` or `:kill` to stop retrying. ## Retry options per job ```ruby class ThumbnailJob include Sidekiq::Job sidekiq_options retry: 8, retry_queue: "thumbnails_retry", backtrace: 10 sidekiq_retries_exhausted do |job, exception| Photo.where(id: job["args"].first).update_all(thumbnail_state: "failed") end end ``` - `retry: true` (the default) uses the global maximum, 25 unless `max_retries` was changed. - `retry: 8` retries eight times, then the job dies. - `retry: 0` does not retry, but the failed job is **still saved to the dead set**. - `retry: false` does not retry and does **not** save the job anywhere; only death handlers see it. - `retry_for: 48.hours` retries by elapsed time instead of by count; since Sidekiq 8.1 it replaces the count rather than combining with it. - `dead: false` lets retries run but discards the job instead of keeping it in the dead set. - `backtrace: 10` stores ten lines of backtrace so the Web UI can show where it failed. ## The dead set The dead set ("the morgue" in Sidekiq's code) is where jobs go to be looked at by a person. Sidekiq never retries them on its own. It keeps the newest **10,000** dead jobs (`dead_max_jobs`) and drops entries older than **six months** (`dead_timeout_in_seconds`), so it is a holding area, not an archive. From there a job can be retried, deleted, or left to expire. In code the same operations exist on `Sidekiq::RetrySet` and `Sidekiq::DeadSet`, for example `Sidekiq::DeadSet.new.retry_all` after deploying a fix. Manual retries decrement `retry_count` first so they do not eat into the job's remaining attempts. ## Seeing it in the Web UI The Web UI is a Rack app mounted in `config/routes.rb` after `require "sidekiq/web"`: - **Retries** lists the retry set with the error, the retry count and the next run time, with Retry Now, Delete and Kill buttons. - **Dead** lists the dead set with Retry and Delete actions, singly or in bulk. - **Busy**, **Queues** and **Scheduled** show running threads, queue sizes and latency, and future jobs. The UI has **no authentication of its own**, so it must sit behind the app's admin authentication or Rack middleware added through `Sidekiq::Web.configure`. It also needs a Rack session, which an API-only Rails app has to enable before mounting it. ## Active Job jobs on Sidekiq An `ApplicationJob` running through the `:sidekiq` adapter follows the same path whenever an exception escapes `perform`. Jobs that use Active Job's own retry rules are re-enqueued by Active Job as new pushes, which is why those attempts never show up in the Retries tab.

  • A job should not be retried, but the team still wants to see failures in the UI. Which option?
    `sidekiq_options retry: 0`. The job is not retried, but when it fails it is moved straight to the dead set, so it appears in the Dead tab with its error and can be retried by hand. `retry: false` would skip both the retries and the dead set; only global death handlers would hear about the failure.
  • How do you put the Sidekiq Web UI behind your admins in a Rails 8.1 app?
    Wrap the mount in a route constraint that checks the signed-in user, for example `constraints(AdminConstraint.new) { mount Sidekiq::Web => "/sidekiq" }`, or add Rack middleware with `config.use(Rack::Auth::Basic, "Jobs") { |user, password| ... }` inside `Sidekiq::Web.configure`. Never mount it bare: the UI can delete queues and rerun jobs.

  • Saying Sidekiq drops a failed job unless the class opts into retries
  • Believing the dead set keeps every failed job forever
  • Treating retry: 0 and retry: false as the same setting
  • Expecting Sidekiq to retry jobs in the dead set by itself
  • Assuming the Web UI ships with its own login screen