skip to content

Sidekiq Adapter

Sidekiq runs jobs from Redis in threaded processes, behind Active Job's :sidekiq adapter or as native Sidekiq::Job classes. Interviewers probe its retry and dead sets and when it beats Solid Queue.

part ofRuby on Railsread it →
on this pageshow

explore

questions

6

In a Rails 8.1 app, what changes when Sidekiq runs jobs through Active Job's :sidekiq adapter rather than as native Sidekiq::Job classes?

level: middleimportance: must knowfreq 58%

answer

  1. one adapter line in config
  2. a wrapper job carries the serialized hash
  3. GlobalID vs JSON-only arguments
  4. provider_job_id holds Sidekiq's jid
  5. 8.1 deprecates Rails' bundled adapter

basics

~10 s

With config.active_job.queue_adapter = :sidekiq, ApplicationJob classes keep perform_later, GlobalID arguments, callbacks and retry_on, and Sidekiq runs them inside a wrapper job. Native Sidekiq::Job classes skip Active Job: JSON-only arguments, Sidekiq's own API, less overhead.

solid answer

~40 s

Setting `config.active_job.queue_adapter = :sidekiq` keeps jobs as `ApplicationJob` subclasses: you call `perform_later`, arguments go through Active Job serialization (so a `Photo` travels as a GlobalID), and callbacks, `retry_on` and `discard_on` all work. The adapter pushes a `Sidekiq::ActiveJob::Wrapper` job whose single argument is the serialized Active Job hash, and Sidekiq's `jid` becomes the job's `provider_job_id`. Native `include Sidekiq::Job` classes skip that layer: JSON-only arguments, `perform_async`, and less CPU spent per job. In Rails 8.1 the adapter Rails itself bundles is deprecated; the sidekiq gem (7.3.3 and later) ships its own adapter and replaces Rails' class when it loads, so a Sidekiq 8 app keeps the same setting and sees no warning. Active Job classes can still call `sidekiq_options`, because the gem mixes `Sidekiq::Job::Options` into them.

code

ruby · 7 lines
ruby
# Rails console, queue_adapter = :sidekiq, sidekiq 8.1
job = WatermarkJob.perform_later(Photo.find(42))
job.job_id           # => "5d1e..."  Active Job's UUID
job.provider_job_id  # => "a91c..."  Sidekiq's jid

# The same work as a native job: ids only
WatermarkNativeJob.perform_async(42)   # => "7be0..." (jid)

go deeper

for a junior

Remember the one-line switch, config.active_job.queue_adapter = :sidekiq, and that jobs then keep using perform_later while Sidekiq does the running.

for a middle

Walk through the wrapper: serialize, push Sidekiq::ActiveJob::Wrapper with the hash, execute on the worker, and how job_id and provider_job_id differ.

for a senior

Decide per job class: Active Job for portability, records and retry_on; native jobs where overhead matters; and know the 8.1 deprecation moves the adapter into the gem.

for a principal

Frame the choice as an abstraction budget: a portable job API eases a later backend move, while native jobs couple the code to one vendor for speed and features.

## Two ways into the same Redis queues A Rails application can hand work to **Sidekiq** in two styles, and both can live in one codebase. - **Through Active Job.** Jobs subclass `ApplicationJob`, are enqueued with `perform_later`, and the application selects Sidekiq with one setting: `config.active_job.queue_adapter = :sidekiq`. - **Natively.** Classes `include Sidekiq::Job` and are enqueued with `perform_async`, `perform_in` or `perform_bulk`. Active Job is never involved. The worker process is the same `bundle exec sidekiq` either way, reading the same queues. ## What the adapter does under the hood ```ruby # config/application.rb config.active_job.queue_adapter = :sidekiq # app/jobs/watermark_job.rb class WatermarkJob < ApplicationJob queue_as :thumbnails sidekiq_options backtrace: 10 # Sidekiq-level option on an Active Job class def perform(photo) WatermarkStamper.new(photo).stamp! end end WatermarkJob.perform_later(photo) # photo travels as a GlobalID ``` 1. `perform_later` builds the job and calls `serialize`, which turns the record into a GlobalID string and records the job class, `job_id`, queue name and execution counters. 2. The adapter calls `Sidekiq::ActiveJob::Wrapper.set(wrapped: WatermarkJob, queue: "thumbnails").perform_async(serialized_hash)`. 3. Sidekiq returns a `jid`, which Active Job stores as `provider_job_id`; `perform_later` itself returns the job instance, or `false` if enqueuing failed. 4. In the worker, `Sidekiq::ActiveJob::Wrapper#perform` calls `ActiveJob::Base.execute`, which deserializes the hash, loads the photo again through GlobalID, runs the Active Job callbacks and finally `WatermarkJob#perform`. The `wrapped` key is why the Web UI shows `WatermarkJob` rather than the wrapper's name, and why the wrapper's options are merged with `WatermarkJob`'s own `sidekiq_options`. ## What each path gives you | Concern | Active Job with `:sidekiq` | Native `Sidekiq::Job` | |---|---|---| | Enqueue call | `perform_later`, `set(wait:)`, `perform_all_later` | `perform_async`, `perform_in`, `perform_bulk` | | Arguments | Active Job serializers: records via GlobalID, symbols, times | JSON-native values only; strict mode raises otherwise | | Error rules | `retry_on` / `discard_on` in the job, then Sidekiq's retries if the error escapes | Sidekiq's retry options and blocks only | | Sidekiq options | `sidekiq_options` still available | `sidekiq_options` | | Switching backend | change one config value | rewrite the jobs | | Cost per job | extra serialization and callback work | lowest overhead | Sidekiq's own README measures that cost: in its benchmark Active Job jobs processed at a clearly lower rate than equivalent `Sidekiq::Job` classes, the gap coming from argument deserialization and callbacks. For most applications that is noise next to the job's real work; for millions of tiny jobs it is not. ## Rails 8.1: whose adapter is it? For years Rails shipped its own `ActiveJob::QueueAdapters::SidekiqAdapter`. **Rails 8.1 deprecates it**, with a warning that it will be removed in Rails 8.2 and that `sidekiq` 7.3.3 or later provides the adapter instead. The sidekiq gem's Rails integration loads its own adapter file, which removes Rails' constant and defines a replacement under the same name. The consequences: - an app on Sidekiq 8 keeps `queue_adapter = :sidekiq` unchanged and gets the gem's adapter without a warning; - an app pinned to an older Sidekiq gets the deprecated built-in adapter and the warning; - the payload class is now `Sidekiq::ActiveJob::Wrapper`; the older wrapper constant survives only as an alias so jobs already sitting in Redis still run. The 8.1 change does not mean Rails stopped supporting Sidekiq; ownership of the adapter moved to the gem. ## Traps at the boundary - `perform_later` returns the job object (or `false`), never a `jid`; read Sidekiq's id from `provider_job_id`. - `sidekiq_options queue: "x"` on an Active Job class has no effect on routing, because the adapter always passes the job's own queue name; use `queue_as`. - `sidekiq_options retry: 3` on an Active Job class does apply: it limits the Sidekiq-level retries that start when an exception escapes. - An app that pins an older sidekiq gem on Rails 8.1 sees the deprecation warning on boot, which is the cue to upgrade the gem rather than to rewrite jobs. ## Choosing between them - Prefer **Active Job** when jobs pass records, when the team wants one job API across backends (Solid Queue in one app, Sidekiq in another), or when jobs rely on `retry_on`, callbacks or continuations. - Prefer **native jobs** for very high-volume, small jobs where per-job overhead shows, or when you need Sidekiq-only features such as iterable jobs. - Mixing is fine: a stock-photo site might keep mailers and record-oriented jobs on Active Job and run its thumbnail fan-out natively.

  • If a WatermarkJob raises and declares no retry_on, who retries it?
    The exception escapes `ActiveJob::Base.execute`, so it reaches the `Sidekiq::ActiveJob::Wrapper` job and Sidekiq treats it like any failed job: it goes to the retry set with Sidekiq's backoff and ends in the dead set if every retry fails. Active Job's own rules only apply when the class declares them; then those retries are fresh pushes that never appear in the Retries tab.
  • Can one app run both styles against the same Sidekiq process?
    Yes. Both styles push ordinary Sidekiq payloads, so one `bundle exec sidekiq` process listening on the right queues runs `ApplicationJob` subclasses through the wrapper and native classes directly. Teams often keep Active Job as the default and move only the hottest, simplest jobs to `Sidekiq::Job`.

  • Saying Rails 8.1 dropped Sidekiq, so jobs must move to Solid Queue
  • Claiming perform_later returns Sidekiq's jid rather than the job instance
  • Believing Active Job classes cannot use sidekiq_options at all
  • Assuming native Sidekiq::Job arguments get GlobalID serialization too
  • Thinking Sidekiq runs the ApplicationJob class directly with no wrapper

Assumes Rails 8.1 with the sidekiq 8.1 gem; Rails 8.1 deprecates its bundled :sidekiq adapter in favour of the one sidekiq 7.3.3+ ships.

open as a page

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%

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.

open as a page

In a Rails app using Sidekiq 8, how do you define a native Sidekiq::Job, enqueue it with perform_async, and which arguments may it take?

level: juniorimportance: should knowfreq 50%

basics

~20 s

A native job is a plain class that includes Sidekiq::Job and defines perform; ThumbnailJob.perform_async(photo.id, "medium") pushes it to Redis and returns a job id. Arguments must be plain JSON: strings, numbers, booleans, nil, arrays and string-keyed hashes.

open as a page

A new Rails 8.1 app ships with Solid Queue; when would you still choose Sidekiq for background jobs, and what do you give up?

level: middleimportance: should knowfreq 52%

basics

~20 s

Choose Sidekiq when job volume or latency outgrows a database-backed queue, when Redis already runs, or when you want its automatic retries, dead set and Web UI. You give up Solid Queue's no-Redis setup, recurring.yml and concurrency limits, and Mission Control.

open as a page

In config/sidekiq.yml, how do queue weights change the order a Sidekiq 8 process fetches from its queues, and when can a queue starve?

level: middleimportance: should knowfreq 42%

basics

~20 s

Without weights, Sidekiq checks queues strictly in listed order, so a busy first queue starves the rest. With weights, it reshuffles the list on every fetch so each queue is checked first in proportion to its weight. Unlisted queues are never fetched.

open as a page

After raising a Sidekiq process's concurrency to 25 in a Rails 8.1 app, jobs start failing with ActiveRecord::ConnectionTimeoutError; why, and how do you size it?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Each Sidekiq thread that queries needs its own Active Record connection, but the pool still holds the generated five, so waiting threads time out after checkout_timeout. Run Sidekiq with RAILS_MAX_THREADS=25 so concurrency and max_connections match.

open as a page