Redis master slave setup on single machine

What is Redis Master Slave?

Redis Master Slave setup is same as any other Master Slave setup. It allows you to maintain multiple copies of same data automatically. That will serve the purpose of backup and scaling. You can use Redis Slave to serve the request the reading requests and hence your master load decreases.

We will start with installation of Redis Server, then Redis Slave and then will setup Redis Mater Slave replication.

About Redis:

Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs.

Many languages have Redis bindings, including: ActionScript, C, C++, C#, Clojure, Common Lisp, Dart, Erlang, Go, Haskell, Haxe, Io, Java, JavaScript (Node.js), Lua, Objective-C, Perl, PHP, Pure Data, Python, R, Ruby, Scala, Smalltalk and Tcl.

Install Redis

There are too many ways to install Redis, we will discuss two ways:

  1. One from source code that works for most of Unix based Operating system and Mac OS X.
  2. Another using apt package manager(that works on Debian/Ubuntu systems).

If you want to run both Master Slave on same machine and not familiar with Unix Systems; better to do that setup with source code(method1).

Install Redis by Method1(through source code):

Open Shell/Terminal/Command Prompt:
wget 'http://download.redis.io/releases/redis-2.8.19.tar.gz'
tar xzf redis-2.8.19.tar.gz
cd redis-2.8.19
make

‘make’ will take few seconds or up to 1-2 minutes to finish and your Redis is installed

Start Redis Command
src/redis-server &
Lets Verify it:
src/redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"

By default Redis runs on port 6379, you can change it very easily from redis.conf file.

Install Redis by Method2 (using apt package manager):

Open Shell/Terminal/Command Prompt:
sudo apt-get update
sudo apt-get install redis-server

Your installation is done and it will start automatically, lets check…

Lets Verify it:
redis-cli
redis 127.0.0.1:6379> set foo bar
OK
redis 127.0.0.1:6379> get foo
"bar"

Till this point single instance of Redis is running fine, if you face any issue, please comment below with error message. Next thing is to run Redis in Master Slave mode. It means we will run two instances of Redis. Here we will run both the instances on same machine. But on production you should install it on different machines, otherwise it will not be HA.

Stop Redis
redis 127.0.0.1:6379>shutdown

Config changes to convert Redis instance to Redis Master:

Now we will change in config file to run mater slave. Steps are very simple and config properties are self explanatory.

Method1:
vi redis.conf
Method2:
sudo vi /etc/redis/redis.conf
Change Listening Interface:

By default Redis listens for connections from all the network interfaces available on the server. It is possible to listen to just one or multiple interfaces using the “bind” configuration directive, followed by one or more IP addresses. like bind 192.168.1.100 10.0.0.1 If the bind option is not specified all the interfaces will listen for incoming connections.

Lets comment it, if not.

bind 127.0.0.1
# bind 127.0.0.1 
Set Master password:

We should use redis with password authentication, to make redis connection secure. But it will solved one more issue. Without password sentinels works like charm. But with password authentication in Redis most of the people do same mistake. That is why we are taking password approach, you can skip this step if you don’t want to use Redis with password.

# requirepass foobared
requirepass abc123

Config changes to setup Redis Slave:

copy redis.conf to slave.conf if you want to run master and slave on same machine. Otherwise just use redis.conf on other machine and do the above two steps again on new machine conf file.

cp redis.conf slave.conf
vi slave.conf
Change slave port

Also change the port if both the master and slave are running on same machine. You can’t run two instances with same port in single machine.

#port 6379
port 6380
Declare the Master Details:

In slave.conf file set master host and port details, so that master slave can communicate. If on different machine add master machine IP address.

# slaveof <masterip> <masterport>
slaveof 127.0.0.1 6379
Master password in slave:

Set password that is required by Slave to connect to Master Redis server. Means the password we set earlier for master.

# masterauth <master-password>
masterauth abc123

Lets Start the Redis Master Slave

Method1:
src/redis-server redis.conf
src/redis-server slave.conf
Method2:
/usr/bin/redis-server /etc/redis/redis.conf
/usr/bin/redis-server /etc/redis/slave.conf
 Now we will test the Redis master slave setup on shell
redis-cli -p 6379 abc123
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> quit
Now check same on Redis Slave
redis-cli -p 6380 -a abc123
127.0.0.1:6380> keys *
1) "foo"
127.0.0.1:6380> get foo
"bar"
127.0.0.1:6380>quit

Setup of Redis master slave is done, if you want to run one more slave, just repeat the steps after we copied the  redis.conf to slave.conf.

References: