What is the best practice to share a Redis connection pool between Rails & Sidekiq ?
I've done that in an initializer :
Sidekiq.configure_client do |config|
pool = ConnectionPool.new(size: 1, timeout: 5) { Redis.new(host: redis_config['host'], port: redis_config['port'], db: redis_config['database']) }
config.redis = pool
Redis.current = pool
end
Sidekiq.configure_server do |config|
pool = ConnectionPool.new(size: 10, timeout: 5) { Redis.new(host: redis_config['host'], port: redis_config['port'], db: redis_config['database']) }
config.redis = pool
Redis.current = pool
config.server_middleware do |chain|
chain.add Kiqstand::Middleware
end
end
But setting the Rails Redis pool in the Sidekiq block is not very clean... Any ideas ?
After a look into Sidekiq sources, Sidekiq.server? method should be a better option. I've change the initializer code for this :
# Redis config
Redis.current = ConnectionPool.new(size: (Sidekiq.server? ? 15 : 1), timeout: 5) do
Redis.new host: redis_config['host'], port: redis_config['port'], db: redis_config['database']
end
# Sidekiq config
Sidekiq.configure_client do |config|
config.redis = Redis.current
end
Sidekiq.configure_server do |config|
config.redis = Redis.current
config.server_middleware do |chain|
chain.add Kiqstand::Middleware
end
end