# sidekiq-fair_tenant

> Sidekiq middleware to re-route excess jobs of “greedy” users to slower queues

- URL: https://evilmartians.com/opensource/sidekiq-fair_tenant
- GitHub: https://github.com/Envek/sidekiq-fair_tenant
- Authors: Andrey Novikov

---

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:

   ```sh
   bundle add sidekiq-fair_tenant
   ```

2. Add `fair_tenant_queues` section to the `sidekiq_options` in your job class:

   ```diff
    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:

   ```diff
    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:

   ```diff
    # config/sidekiq.yml
    :queues:
   -  - default
   +  - [default, 4]
   +  - [throttled_2x, 2]
   +  - [throttled_4x, 1]
   ```

   See [Sidekiq Advanced options](https://github.com/sidekiq/sidekiq/wiki/Advanced-Options#queues) to learn more about queue weights.

And there is more in the gem’s [README](https://github.com/Envek/sidekiq-fair_tenant#readme).
