Sidekiq 7: Rate limiting with capsules

Updated . Posted . Visible to the public.

Sidekiq 7 adds a new feature called capsules Show archive.org snapshot .

Use cases:

  • a chrome queue limited to 1 for e.g. PDF processing to not overload the application server
  • an api queue, that limits a queue to 2 to protect the API server from too many requests in parallel

Example:

Sidekiq.configure_server do |config|
  # Edits the default capsule
  config.queues = %w[critical default low]
  config.concurrency = 5

  # Define a new capsule which processes jobs from the `unsafe` queue one at a time
  config.capsule("unsafe") do |cap|
    cap.concurrency = 1
    cap.queues = %w[unsafe]
  end
end

Some notes:

  • To know the total threads per sidekiq server your need to sum up the concurrencies. In the example above this would be 6 threads in total. Instead of config.concurrency = 5 you also might have this config in a sidekiq.yml file saying :concurrency: 5.
  • In case you run multiple sidekiq servers (processes), the total number of concurrent capsules differs. In the example above with e.g. two application servers with a sidekiq role the maximum number of jobs in the unsafe queue is 2. And the total number of threads 12.
Last edit
Maximilian Berger
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2023-05-22 10:06)