Throttle “greedy” tenants’ background jobs to ensure more or less fair distribution of resources between clients in a multi-tenant application.
This is a tiny Sidekiq middleware that will re-route tenant’s jobs after certain threshold to throttled queues (defined by you), where they will be processed with reduced priority.
Installation
-
Install the gem and add to the application’s Gemfile by executing:
bundle add sidekiq-fair_tenant
-
Add
fair_tenant_queues
section to thesidekiq_options
in your job class:class SomeJob sidekiq_options \ queue: 'default', + fair_tenant_queues: [ + { queue: 'throttled_2x', threshold: 100, per: 1.hour }, + { queue: 'throttled_4x', threshold: 10, per: 1.minute }, + ] end
-
Add tenant detection logic into your job class:
class SomeJob + def self.fair_tenant(*_perform_arguments) + # Return any string that will be used as tenant name + "tenant_1" + end end
-
Add throttled queues with reduced weights to your Sidekiq configuration:
# config/sidekiq.yml :queues: - - default + - [default, 4] + - [throttled_2x, 2] + - [throttled_4x, 1]
See Sidekiq Advanced options to learn more about queue weights.
And there is more in the gem’s README.