Rails, Redis and Sentinel
Asked Answered
B

1

7

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?

Bun answered 6/5, 2015 at 14:52 Comment(0)
C
9

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
Cogency answered 6/5, 2015 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.