sidekiq-fair_tenant

Services & Skills

Share on

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

  1. Install the gem and add to the application’s Gemfile by executing:

    bundle add sidekiq-fair_tenant
  2. Add fair_tenant_queues section to the sidekiq_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
  3. 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
  4. 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.

Further reading

Explore more open source projects

Schedule call

Irina Nazarova CEO at Evil Martians

Evil Martians transform growth-stage startups into unicorns, build developer tools, and create open source products. Hire us to design and build your product