#Redis Failover

This 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 given machine. In that condition, we would both promote the appropriate slaves to master (the Redis command is: slaveof no one) and call failover_host on the bad host.

# This is a function we can execute to manually initiate a failover for an individual node.
def failover_node(node)
  node_index = $master_arr.index(node)
  # There is no remove_node for redis::distributed, so we must rebuild the ring
  #  but first swap slave/master.
  $master_arr[node_index] = $slave_arr[node_index]
  $slave_arr[node_index] = node
  $redis = Redis::Distributed.new $master_arr
end

# This function will failover an entire host machine.
#  e.g. failover_host('localhost')
def failover_host(host)
  $master_arr.each {|x|
    if x[:host] == host
      failover_node(x)
    end
  }
end

Again, do keep a close eye on the Redis Cluster with Sentinel projects, which will likely be the best supported version of HA for distributed servers in the future.

Advertisements

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