Redis Master/Slave Config

While there is the #Redis Sentinel project, which is your best bet for a more complete HA (High Availability) solution, we wanted a simple solution that would work for our distributed Redis nodes we recently described, which unfortunately will not work with Sentinel. We recommend you have slave nodes (ideally in a different site than the master Redis hosts to allow for better HA) that stream copies of any changes made in the master. Fortunately, Redis already has a concept of master/slave replication. In your config for these slave nodes, you simply have to specify: slaveof [master host] [master port].

This code assumes that the way we set up these slaves is in a top level range of ports above the master nodes on each machine. For example, if we had 3 processes per node and 3 slaves on that same node for 2 hosts:

# Set up pointers to the slave processes
#
# A slave will exist on a different host (if available) at opposite ports
#  so slave_port = (first_port+procs_per_host*2)-(master_port)-1
#  e.g. master1 = host1, port 1
#       slave1  = host2, port 6
#       master2 = host1, port 2
#       slave2  = host2, port 5
#       master3 = host1, port 3
#       slave3  = host2, port 4
#       master4 = host2, port 1
#       slave4  = host1, port 6
#       master5 = host2, port 2
#       slave5  = host1, port 5
#       master6 = host2, port 3
#       slave6  = host1, port 4
$slave_arr = Array.new(REDIS_NUMPROCS) {|i|
  {
    :host => REDIS_HOST[i/redis_procs_per_host],
    :db => 0,
    :port => REDIS_FIRST_PORT + redis_procs_per_host + i%redis_procs_per_host
  }
}.reverse

We can then use these pointers for executing failovers, which we will describe tomorrow.

Advertisements

One thought on “Redis Master/Slave Config

  1. […] is simple #failover code for our Redis master/slave config that we would want to wrap with some sort of monitoring to detect when to initiate a failover for a […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s