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:

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

Start Redis Command

Lets Verify it:

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:

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

Lets Verify it:

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.

Method1:

Method2:

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.

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.

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.

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.

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.

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.

Lets Start the Redis Master Slave

Method1:

Method2:

 Now we will test the Redis master slave setup on shell

Now check same on Redis Slave

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: