Redis pool share between Sidekiq & Rails

What is the best practice for sharing a Redis connection pool between Rails and Sidekiq?

I did this in the 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 up the Rails Redis pool in the Sidekiq block is not very clean ... Any ideas?

+4
source share
1 answer

After looking at Sidekiq sources, Sidekiq.server? method should be the best option. I changed 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 
+2
source

Source: https://habr.com/ru/post/1486641/


All Articles