title: Implementing a multi-tenant (namespaced) setup using Sidekiq's Redis connection pool
body: <p>In addition to Sidekiq, I have a worker that also makes heavy use of Redis to store keys. Since <code>Sidekiq</code> maintains it's own redis connection pool, the worker takes advantage of that and always connects through <code>Sidekiq</code>'s pool. </p>
  
  <pre><code>class MyRedisWorker
    include Sidekiq::Worker
  
    def perform
      # ...
    end
  
    def run_redis_cmd(cmd, *args)
      Sidekiq.redis { |conn| conn.send(cmd, *args) }
    end
  end
  </code></pre>
  
  <p>I am now moving over to a multi-tenant model which allows multiple tenants to run on the same app instance and DB (In Postgres this corresponds to just using a different <code>schema</code> for each tenant)</p>
  
  <p>Since each tenant will run its own version of the Worker against Redis and have its own <code>Sidekiq</code> queues, I need to namespace all Redis connections so keys don't conflict. I know Sidekiq provides such an option to use in the initializer -</p>
  
  <pre><code>Sidekiq.configure_server do |config|
    config.redis = { url: URL, network_timeout: 3, namespace: my_tenant_name }
  end
  
  Sidekiq.configure_client do |config|
    config.redis = { url: URL, network_timeout: 3, namespace: my_tenant_name }
  end
  </code></pre>
  
  <p>My question is, Sidekiq doesn't maintain multiple sets of pools, each of which can be namespaced individually, right? Looks like it just maintains one top-level pool for which you can optionally provide (up to) 1 namespace. Even the worker just calls <code>Sidekiq.redis { .... }</code> at the top-level, without specifying which pool.</p>
  
  <p>What's the best way around this? I don't necessarily need a dedicated redis pool for each tenant, I just need a way when retrieving a redis connection that Sidekiq will allow me to specify what namespace I want to use for that connection. </p>
  
  <p>Thanks!</p>