Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

sidekiq-throttled

> 开发工具
Open source

Concurrency and rate-limit throttling for Sidekiq

799 stars0 likes3 views
WebsiteGitHub

About

Concurrency and rate-limit throttling for Sidekiq

= Sidekiq::Throttled :ci-link: https://github.com/ixti/sidekiq-throttled/actions/workflows/ci.yml :ci-badge: https://img.shields.io/github/actions/workflow/status/ixti/sidekiq-throttled/ci.yml?branch=main&style=for-the-badge :gem-link: http://rubygems.org/gems/sidekiq-throttled :gem-badge: https://img.shields.io/gem/v/sidekiq-throttled?style=for-the-badge :doc-link: http://www.rubydoc.info/gems/sidekiq-throttled :doc-badge: https://img.shields.io/badge/Documentation-API-blue?style=for-the-badge


{ci-link}[image:{ci-badge}[CI Status]] {gem-link}[image:{gem-badge}[Latest Version]] {doc-link}[image:{doc-badge}[API Documentation]]


Concurrency and threshold throttling for https://github.com/sidekiq/sidekiq[Sidekiq].

== Installation

Add this line to your application's Gemfile:

[source,ruby]

gem "sidekiq-throttled"

And then execute:

$ bundle

Or install it yourself as:

$ gem install sidekiq-throttled

== Usage

Add somewhere in your app's bootstrap (e.g. config/initializers/sidekiq.rb if you are using Rails):

[source,ruby]

require "sidekiq/throttled"

Once you've done that you can include Sidekiq::Throttled::Job to your job classes and configure throttling:

[source,ruby]

class MyJob include Sidekiq::Job include Sidekiq::Throttled::Job

sidekiq_options :queue => :my_queue

sidekiq_throttle( # Allow maximum 10 concurrent jobs of this class at a time. concurrency: { limit: 10 }, # Allow maximum 1K jobs being processed within one hour window. threshold: { limit: 1_000, period: 1.hour } )

def perform # ... end

end

TIP: Sidekiq::Throttled::Job is aliased as Sidekiq::Throttled::Worker, thus if you're using Sidekiq::Worker naming convention, you can use the alias for consistency:

[source,ruby]

class MyWorker include Sidekiq::Worker include Sidekiq::Throttled::Worker

...

end

=== Requeue Strategy The default requeue strategy :enqueue puts jobs immiediately back on the queue if they are being limited, with a potential cooldown_period. This is often an appropriate strategy but in some situations may cause the system to repeatedly dequeue and reenque the same job causing excessive redis CPU usage.

An alternative requeue strategy :schedule is available that schedules the work for the future. This can be less appropriate for highly concurrent jobs, but may be a good way to reduce redis load when the number of jobs to be processed is modest.

The alternative :schedule strategy may be configured globally with config.default_requeue_options = { with: :schedule }

It may be configured on a per job basis with:

ruby
  sidekiq_throttle(
    # Allow 5 jobs to processed within 1 minute, using the 
    threshold: { limit: 5, period: 1.minute },
    requeue: { with: :schedule }
  )

It is also possible to requeue jobs to another queue:

ruby
  sidekiq_throttle(
    # Allow 5 jobs to processed within 1 minute, using the 
    threshold: { limit: 5, period: 1.minute },
    requeue: { to: :other_queue, with: :schedule }
  )

=== Web UI

To add a Throttled tab to your sidekiq web dashboard, require it durring your application initialization.

[source,ruby]

require "sidekiq/throttled/web"

=== Configuration

[source,ruby]

Sidekiq::Throttled.configure do |config|

Period in seconds to exclude queue from polling in case it returned

{config.cooldown_threshold} amount of throttled jobs in a row. Set

this value to nil to disable cooldown manager completely.

Default: 1.0

config.cooldown_period = 1.0

Exclude queue from polling after it returned given amount of throttled

jobs in a row.

Default: 100 (cooldown after hundredth throttled job in a row)

config.cooldown_threshold = 100 end

[WARNING] .Cooldown Settings

If a queue contains a thousand jobs in a row that will be throttled, the cooldown will kick-in 10 times in a row, meaning it will take 10 seconds before all those jobs are put back at the end of the queue and you actually start processing other jobs.

You may want to adjust the cooldown_threshold and cooldown_period, keeping in mind that this will also impact the load on your Redis server.

==== Middleware(s)

Sidekiq::Throttled relies on following bundled middlewares:

  • Sidekiq::Throttled::Middlewares::Server

The middleware is automatically injected when you require sidekiq/throttled. In rare cases, when this causes an issue, you can change middleware order manually:

[source,ruby]

Sidekiq.configure_server do |config|

...

config.server_middleware do |chain| chain.prepend(Sidekiq::Throttled::Middlewares::Server) end

end

See: https://github.com/sidekiq/sidekiq/blob/main/lib/sidekiq/middleware/chain.rb

=== Observer

You can specify an observer that will be called on throttling. To do so pass an :observer option with callable object:

[source,ruby]

class MyJob include Sidekiq::Job include Sidekiq::Throttled::Job

MY_OBSERVER = lambda do |strategy, *args| # do something end

sidekiq_options queue: :my_queue

sidekiq_throttle( concurrency: { limit: 10 }, threshold: { limit: 100, period: 1.hour }, observer: MY_OBSERVER )

def perform(*args) # ... end

end

Observer will receive strategy, *args arguments, where strategy is a Symbol :concurrency or :threshold, and *args are the arguments that were passed to the job.

=== Dynamic throttling

You can throttle jobs dynamically with :key_suffix option:

[source,ruby]

class MyJob include Sidekiq::Job include Sidekiq::Throttled::Job

sidekiq_options queue: :my_queue

sidekiq_throttle( # Allow maximum 10 concurrent jobs per user at a time. concurrency: { limit: 10, key_suffix: -> (user_id) { user_id } } )

def perform(user_id) # ... end

end

You can also supply dynamic values for limits and periods by supplying a proc for these values. The proc will be evaluated at the time the job is fetched and will receive the same arguments that are passed to the job.

[source,ruby]

class MyJob include Sidekiq::Job include Sidekiq::Throttled::Job

sidekiq_options queue: :my_queue

sidekiq_throttle( # Allow maximum 1000 concurrent jobs of this class at a time for VIPs and 10 for all other users. concurrency: { limit: ->(user_id) { User.vip?(user_id) ? 1_000 : 10 }, key_suffix: ->(user_id) { User.vip?(user_id) ? "vip" : "std" } }, # Allow 1000 jobs/hour to be processed for VIPs and 10/day for all others threshold: { limit: ->(user_id) { User.vip?(user_id) ? 1_000 : 10 }, period: ->(user_id) { User.vip?(user_id) ? 1.hour : 1.day }, key_suffix: ->(user_id) { User.vip?(user_id) ? "vip" : "std" } } )

def perform(user_id) # ... end

end

You also can use several different keys to throttle one worker.

[source,ruby]

class MyJob include Sidekiq::Job include Sidekiq::Throttled::Job

sidekiq_options queue: :my_queue

sidekiq_throttle( # Allow maximum 10 concurrent jobs per project at a time and maximum 2 jobs per user concurrency: [ { limit: 10, key_suffix: -> (project_id, user_id) { "project_id:#{project_id}" } }, { limit: 2, key_suffix: -> (project_id, user_id) { "user_id:#{user_id}" } } ] # For :threshold it works the same )

def perform(project_id, user_id) # ... end

end

IMPORTANT: Don't forget to specify :key_suffix and make it return different values if you are using dynamic limit/period options. Otherwise, you risk getting into some trouble.

[source,ruby]

class MyJob include Sidekiq::Job include Sidekiq::Throttled::Job

sidekiq_options queue: :my_queue

sidekiq_throttle( concurrency: { limit: 10 }, # Allow 500 jobs per minute, 5,000 per hour, and 50,000 per day: threshold: [ { limit: 500, period: 1.minute, key_suffix: "minutely" }, { limit: 5_000, period: 1.hour, key_suffix: "hourly" }, { limit: 50_000, period: 1.day, key_suffix: "daily" }, ] )

def perform(project_id, user_id) # ... end

end

NOTE: key_suffix does not have to be a proc/lambda, it can just be a string value. This can come in handy to set throttle limits for different ranges of time

=== Concurrency throttling fine-tuning

Concurrency throttling is based on distributed locks. Those locks have default time to live (TTL) set to 15 minutes. If your job takes more than 15 minutes to finish, lock will be released and you might end up with more jobs running concurrently than you expect.

This is done to avoid deadlocks - when by any reason (e.g. Sidekiq process was OOM-killed) cleanup middleware wasn't executed and locks were not released.

If your job takes more than 15 minutes to complete, you can tune concurrency lock TTL to fit your needs:

[source,ruby]

Set concurrency strategy lock TTL to 1 hour.

sidekiq_throttle(concurrency: { limit: 20, ttl: 1.hour.to_i })

=== Scheduling based concurrency tuning

The default concurrency throttling algorithm immediately requeues throttled jobs. This can lead to a lot of wasted work picking up the same set of still throttled jobs repeatedly. This churn also often starves lower priority jobs/queues. The :schedule requeue strategy delays checking the runability of throttled jobs until likely to be runnable. This future time is estimated based on the expected runtime of the job and current number of throttled jobs. This eliminates -- or greatly reduces -- the negative impacts to non-throttled job types and queues and reduces wasted work constantly rechecking the same still throttled jobs.

Config items

  • limit — Maximum number of this job allowed to run simultaneously.
  • avg_job_duration — Expected runtime (in seconds) for this job type.
    Choose a value on the high end of what’s plausible; if you set this too low under heavy load, job scheduling will become sub-optimal.
  • lost_job_threshold — Duration (in seconds) representing how long a job “owns” its concurrency slot before being considered lost.
  • ttl — Deprecated alias for lost_job_threshold.
  • max_delay — The maximum number of seconds to delay a job when it is throttled.
    Prevents excessively long scheduling delays as the backlog grows.
    Default: the smaller of 30 minutes or 10 × avg_job_duration.

[source,ruby]

sidekiq_throttle( concurrency: { # only run 10 of this job at a time limit: 10, # these jobs finish in less than 30 seconds avg_job_duration: 30, # if it doesn't release its lease in 2 minutes it's considered lost lost_job_threshold: 120, # maximum delay allowed when throttled max_delay: 300 },

requeue using Sidekiq's scheduler

requeue: { with: :schedule } )

== Supported Ruby Versions

This library aims to support and is tested against the following Ruby versions:

  • Ruby 3.2.x
  • Ruby 3.3.x
  • Ruby 3.4.x
  • Ruby 4.0.x

If something doesn't work on one of these versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby versions, however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.

== Supported Sidekiq Versions

This library aims to support and work with following Sidekiq versions:

  • Sidekiq 8.0.x
  • Sidekiq 8.1.x

And (might work with) the following Sidekiq Pro versions:

  • Sidekiq Pro 8.0.x
  • Sidekiq Pro 8.1.x

== Development

bundle install bundle exec appraisal generate

Issues· 30 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyconcurrencysidekiqthresholdthrottling

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具