Byte Friendly

Random thoughts on programming and related topics.

Using Sidekiq With Redis_failover Gem

| Comments

Sidekiq works pretty well with standard redis client. This is an example of configuring it (from official wiki):

1
2
3
4
5
6
7
8
Sidekiq.configure_server do |config|
  config.redis = { :url => 'redis://redis.example.com:7372/12', :namespace => 'mynamespace' }
end

# When in Unicorn, this block needs to go in unicorn's `after_fork` callback:
Sidekiq.configure_client do |config|
  config.redis = { :url => 'redis://redis.example.com:7372/12', :namespace => 'mynamespace' }
end

But, what if you need a special redis client that can’t be initialized with redis URL? No worries. Setter for Sidekiq#redis also accepts a ConnectionPool. Here we have full control over how we initialize our redis client. Here’s how you may initialize Sidekiq with a bunch of RedisFailover clients:

1
2
3
4
5
6
7
8
9
10
Sidekiq.configure_server do |config|
  pool = ConnectionPool.new(:size => 10, :timeout => 3) do
    zkservers = 'localhost:2181,localhost:2182,localhost:2183'
    RedisFailover::Client.new namespace: 'my_service', zkservers: zkservers
  end

  config.redis = pool
end

# do the same in Sidekiq.configure_client

You can even read Sidekiq’s concurrency setting and pass it to the connection pool initializer. Now your sidekiq farm doesn’t care about redis crashes :)

Happy sidekiqing :)

Comments