ruby-on-railsredisredis-sentinelredis-rails

Rails, Redis and Sentinel


I have a Redis cluster of 4 nodes, 1 master and 3 slaves, monitored by Sentinel.

Now in rails I need to connect to this cluster, reading from the nearest replica, and writing to the master, the same as I do with MongoDB.

Using the redis-rails gem, how is it possible to configure the cache_store to specify the Sentinels instead of a single node?


Solution

  • I may have missed it, but I wasn't aware that you could configure it to read from the slaves? However, this is my master + 2 slave configuration:

    config.cache_store = :redis_store, {
        url: 'redis://prestwick/1',
        sentinels: [{host: 'prestwick.i', port: 26379}, {host: 'carnoustie.i', port: 26379}, {host: 'birkdale.i', port: 26379}],
        role: 'master',
        expires_in: 1.hour
      }
    

    And in case it's useful, my configuration for a generic REDIS object and Sidekiq (this is in config/initializers/001_redis.rb):

    redis_options = {
      url: 'redis://prestwick/0',
      sentinels: [{host: 'prestwick.i', port: 26379}, {host: 'carnoustie.i', port: 26379}, {host: 'birkdale.i', port: 26379}],
      role: 'master'
    }
    redis_sidekiq_options = {
      url: 'redis://prestwick/2',
      sentinels: [{host: 'prestwick.i', port: 26379}, {host: 'carnoustie.i', port: 26379}, {host: 'birkdale.i', port: 26379}],
      role: 'master'
    }
    
    REDIS = Redis.new(redis_options)
    
    Sidekiq.configure_server do |config|
      config.redis = redis_sidekiq_options
    end
    Sidekiq.configure_client do |config|
      config.redis = redis_sidekiq_options
    end