What is the best way to use Redis in a Multi-threaded Rails environment? (Puma / Sidekiq)
You use a separate global connection pool for your application code. Put something like this in your redis.rb initializer:
require 'connection_pool'REDIS = ConnectionPool.new(size: 10) { Redis.new }
Now in your application code anywhere, you can do this:
REDIS.with do |conn| # some redis operationsend
You'll have up to 10 connections to share amongst your puma/sidekiq workers. This will lead to better performance since, as you correctly note, you won't have all the threads fighting over a single Redis connection.
All of this is documented here: https://github.com/mperham/sidekiq/wiki/Advanced-Options#connection-pooling