title: What is the best way to use Redis in a Multi-threaded Rails environment? (Puma / Sidekiq)
body: <p>I'm using Redis in my application, both for Sidekiq queues, and for model caching.</p>
  
  <p>What is the best way to have a Redis connection available to my models, considering that the models that will be hitting Redis will be called both from my Web application (ran via Puma), and from background jobs inside Sidekiq?</p>
  
  <p>I'm currently doing this in my initializers:</p>
  
  <pre><code>Redis.current = Redis.new(host: 'localhost', port: 6379)
  </code></pre>
  
  <p>And then simply use <code>Redis.current.get</code> / <code>Redis.current.set</code> (and similar) throughout the code...</p>
  
  <p>This should be thread-safe, as far as I understand, since the Redis Client only runs one command at a time, using a Monitor.</p>
  
  <p>Now, Sidekiq has its own connection pool to Redis, and recommends doing</p>
  
  <pre><code>Sidekiq.redis do |conn|
     conn.get
     conn.set
  end
  </code></pre>
  
  <p>As I understand it, this would be better than the approach of just using Redis.current because you don't have multiple workers on multiple threads waiting on each other on a single connection when they hit Redis.</p>
  
  <p>However, how can I make this connection that I get from Sidekiq.redis available to my models? (without having to pass it around as a parameter in every method call)</p>
  
  <p>I can't set Redis.current inside that block, since it's global, and I'm back to everyone using the same connection (plus switching between them randomly, which might even be non-thread-safe)</p>
  
  <p>Should I store the connection that I get from Sidekiq.Redis into a Thread-local variable, and use that thread-local variable everywhere?</p>
  
  <p>In that case, what do I do in the "Puma" context? How do I set the thread-local variable?</p>
  
  <p>Any thoughts on this are greatly appreciated.</p>
  
  <p>Thank you!</p>