Redis replication: couldn’t be any easier

how-to
Aug 29, 20124 mins

Redis supports master-slave replication and it’s extremely easy to setup. And the beauty of setting up replication is that all you have to do is fire up a slave instance and have it point to some other Redis instance (which then becomes the master node).

Accordingly, open up the redis.conf file for an intended secondary (or slave node) and find the section containing a slaveof phrase. Uncomment it and add the IP address & port of the master node:

<span class='line-number'>1</span>
<code class=''><span class='line'>slaveof ec2-23-21-a8-21.compute-1.amazonaws.com 6379</span>

Then cycle the secondary node (so the redis.conf file is re-read). That’s it.

When you fire up the slave node and run the INFO command, you’ll see a few new items, namely:

<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<code class=''><span class='line'>role:slave
</span><span class='line'>master_host:ec2-23-21-a8-21.compute-1.amazonaws.com
</span><span class='line'>master_port:6379
</span><span class='line'>master_link_status:down
</span><span class='line'>master_last_io_seconds_ago:-1
</span><span class='line'>master_sync_in_progress:1
</span><span class='line'>master_sync_left_bytes:-1
</span><span class='line'>master_sync_last_io_seconds_ago:0
</span><span class='line'>master_link_down_since_seconds:14</span>

You’ll note that it takes a few moments for a data sync to occur, thus, after a few seconds, rerun the INFO command to see:

<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<code class=''><span class='line'>role:slave
</span><span class='line'>master_host:ec2-23-21-a8-21.compute-1.amazonaws.com
</span><span class='line'>master_port:6379
</span><span class='line'>master_link_status:up
</span><span class='line'>master_last_io_seconds_ago:6
</span><span class='line'>master_sync_in_progress:0</span>

Running the same INFO command on the master node will yield a new field as well:

<span class='line-number'>1</span>
<span class='line-number'>2</span>
<code class=''><span class='line'>role:master
</span><span class='line'>slave0:10.255.3.143,34647,online</span>

A master node can have 0..N slaves (hence the naming scheme of slaveN); what’s more, slaves can become a master by commenting out or unsetting the SLAVEOF command in their corresponding configuration.

It should be noted that Redis does support configuration changes at runtime via the CONFIG SET command, which means you don’t even need to cycle a particular node to synchronize it with a master.

andrew_glover

When Andrew Glover isn't listening to “Funkytown” or “Le Freak” he enjoys speaking on the No Fluff Just Stuff Tour. He also writes articles for multiple online publications including IBM's developerWorks and O'Reilly’s ONJava and ONLamp portals. Andrew is also the co-author of Java Testing Patterns, which was published by Wiley in September 2004; Addison-Wesley’s Continuous Integration; and Manning’s Groovy in Action.

More from this author