Read more

Sidekiq 7: Rate limiting with capsules

Emanuel
May 22, 2023Software engineer at makandra GmbH

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

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more 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.
  • The queues configured for a capsule are still accessible for all other jobs. In the example above it is possible to run 6 jobs in the critical queue. The order of the jobs is configured with the priority for each queue.
Posted by Emanuel to makandra dev (2023-05-22 12:06)