skip to content

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%

answer

  1. no weights means strict order
  2. all weights 1 means random
  3. weight = chance of being checked first
  4. unlisted queues are never fetched
  5. separate processes or capsules for isolation

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.

solid answer

~50 s

`config/sidekiq.yml` (or repeated `-q` flags) lists the queues a process watches. Each processor thread fetches with one blocking Redis pop across that list, and Redis takes from the first non-empty queue in the order given. With no weights the order is **strict**: a later queue is served only when every earlier one is empty, so a flood in a high queue starves the low ones. With weights such as `[thumbnails_upload, 4]`, `[default, 2]`, `[thumbnails_backfill, 1]`, Sidekiq expands each name by its weight and shuffles the list before every fetch, so a queue is checked first with probability proportional to its weight; nothing starves, but nothing is guaranteed a fixed share either. All weights equal to 1 means random order. A queue missing from the list, such as one named by `queue_as` but forgotten in the file, is never fetched.

code

ruby · 9 lines
ruby
# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
  # One thread that only ever runs backfill jobs, so the
  # default capsule from sidekiq.yml stays free for uploads.
  config.capsule("backfill") do |cap|
    cap.concurrency = 1
    cap.queues = %w[thumbnails_backfill]
  end
end

go deeper

for a junior

Remember that a Sidekiq process only runs the queues it lists, and that listing order or weights decide which queue it asks first.

for a middle

Explain strict, random and weighted modes: the blocking pop takes the first non-empty queue, and weights reshuffle the order per fetch.

for a senior

Recognise starvation from queue latency, and choose between weights, separate processes and capsules according to the latency each queue needs.

for a principal

Design queue topology around service levels: which work shares threads, which gets dedicated capacity, and how that maps to processes and hosts.

## Where the queue list lives A Sidekiq worker process watches a fixed list of queues. It reads that list from `config/sidekiq.yml` (found automatically, or passed with `-C`) or from repeated `-q` flags; with neither, the list is just `default`. The same file holds other process settings such as `:concurrency:`, and it is run through ERB and may carry per-environment sections such as `:production:` that override the top level. ```yaml # config/sidekiq.yml for a stock-photo site :concurrency: 10 :queues: - [thumbnails_upload, 4] - [default, 2] - [thumbnails_backfill, 1] ``` The equivalent command line is `bundle exec sidekiq -q thumbnails_upload,4 -q default,2 -q thumbnails_backfill,1`. ## Reading the file correctly - Keys may be written as `:concurrency:` or `concurrency:`; Sidekiq symbolizes them either way. - Weights are written as two-element arrays, `[name, weight]`; on the command line they become `name,weight`. - A `:production:` or `:staging:` section is merged over the top-level settings for that environment. - Command-line flags win over the file, and the file wins over defaults; with no queues anywhere, the process watches only `default`. - The file configures the process's **default capsule** only; extra capsules are declared in an initializer. ## How a thread picks its next job Each of the process's threads loops on one operation: a **blocking pop across several Redis lists at once**. Redis returns a job from the **first non-empty list in the order the command names them**, or waits up to two seconds and lets the thread try again. So the whole question of priority is the order in which the process names its queues on each fetch. Sidekiq derives one of three modes from the weights: | Weights in the list | Mode | Order used on every fetch | |---|---|---| | none given (all treated as 0) | **strict** | exactly the listed order | | every queue weight 1 | **random** | a fresh random order | | any other numbers | **weighted** | each name repeated by its weight, shuffled, duplicates removed | In weighted mode with weights 4, 2 and 1, the list is seven entries long before shuffling, so `thumbnails_upload` comes out first about four times in seven, `default` about twice in seven and `thumbnails_backfill` once in seven. Note what this does and does not promise: - it sets **which queue is asked first**, not a fixed ratio of jobs processed; - an empty queue costs nothing, because Redis simply moves on to the next name; - with all three queues full, throughput tends toward the weights, but it is still random per fetch. ## When a queue starves **Starvation** means jobs sit in a queue indefinitely even though workers are busy. It happens in these situations: 1. **Strict order with a busy head queue.** If uploads never stop arriving, `thumbnails_backfill` is fetched only in the gaps, which may be never during peak hours. 2. **A queue nobody listens to.** A job with `queue_as :mailers` or `sidekiq_options queue: "mailers"` lands in Redis, but if no process lists `mailers`, it waits forever. It is a very common cause of "Sidekiq is not running my job" reports. 3. **Long jobs everywhere.** Weights decide which queue is asked first only when a thread is free. If every thread is busy with a 40-minute backfill job, an urgent upload waits however high its weight. Weighted mode fixes the first case, a deploy-time check that every `queue_as` name is listed fixes the second, and the third needs isolation rather than weights. ## Isolating work instead of weighting it When one class of work must never wait behind another, give it its own threads: - **Separate processes**: run one process with `-q thumbnails_upload` and another with `-q thumbnails_backfill`, each with its own concurrency. - **Capsules**: since Sidekiq 7, an initializer can declare extra capsules inside one process, each with its own queues and thread count, for example a capsule with `concurrency = 1` for a queue whose jobs must run one at a time. - Keep strict ordering for genuinely ordered priorities, such as a small `critical` queue that must always go first, and accept that lower queues only run in its gaps. Weights are the cheap tool for "mostly these, but keep the others moving"; separate threads are the tool for latency guarantees.

  • Does a weight of 4 against 1 mean the upload queue gets 80 percent of the throughput?
    Not exactly. The weights set the probability that each queue is checked first on a fetch: with 4, 2 and 1 the upload queue is first four times in seven. If it is empty, the fetch falls through to the next queue at no cost. Only when every queue is full does throughput approach the weight ratio, and even then it is random per fetch, not a quota.
  • How would you catch a queue that no process is listening to?
    Compare the queues jobs are pushed to with the queues processes watch. The Queues tab of the Web UI shows each queue's size and latency, and `Sidekiq::Queue.new("mailers").latency` gives the age of its oldest job from the console; a steadily growing latency on one queue while workers sit idle is the signature. A test can assert every `queue_as` name appears in `config/sidekiq.yml`.

Weighted queues work like a raffle drum refilled before every draw: the upload queue holds four tickets, default two, backfill one. Whichever queue is drawn is asked first, and if its line is empty the clerk simply asks the next one drawn. More tickets mean being asked first more often, not a guaranteed share of the customers served.

  • Saying weights guarantee each queue a fixed share of processed jobs
  • Believing a queue named in queue_as is fetched without being listed
  • Expecting strict order to still run low queues while high ones are full
  • Thinking queue weights can preempt a long job already running
  • Assuming config/sidekiq.yml is read by Active Job rather than the sidekiq process