in Technology

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 ''
tar xzf redis-2.8.19.tar.gz
cd redis-2.8.19

‘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> set foo bar
OK> get foo

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> set foo bar
redis> get foo

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

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.

vi redis.conf
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 If the bind option is not specified all the interfaces will listen for incoming connections.

Lets comment it, if not.

# bind 
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 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

src/redis-server redis.conf
src/redis-server slave.conf
/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> keys *
(empty list or set)> set foo bar
OK> get foo
"bar"> quit
Now check same on Redis Slave
redis-cli -p 6380 -a abc123> keys *
1) "foo"> get foo

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.