Redis is a popular key value store that can be used for storing different types of data and retrieve it back. It is very fast in response as all data it stores is in memory(well redis has built-in persistence as well, in which it will keep the snapshots in disk to restore from).
The main advantage of redis is the persistence of data. Unlike memcached, you can have the same data back again in redis even after a restart. This kind of persistence gives redis an edge over other tools like memcached, and makes redis much more than a volatile caching server. Due to this persistence, you can even consider redis as a noSQL database.
Another advantage of redis is the number of data types it supports. Below are the list of data types redis supports currently.
Redis is an awesome choice if you are looking for a highly scalable data store that needs to be accessible to multiple applications, processes, and servers. It is the best inter process communication tool out there.
The basic requirement for making this tutorial work is two VMCentral virtual machines with redis-server packages installed.
Installing redis is quite simple in Ubuntu. Its a simple apt-get command (after adding an apt repository.). In this tutorial, we will be doing a master slave replication configuration for redis. The first step of course will be to install redis-server.
#sudo add-apt-repository ppa:chris-lea/redis-server
#sudo apt-get update
#sudo apt-ge t install redis-server
Follow the same above mentioned process and series of commands on both the vm’s.
Now the next step is to configure our redis master server. Configuring redis master is also very simple.
Configuring Redis Master
Open the main redis configuration file : /etc/redis/redis.conf and make sure it has the following configurations.
The “tcp-keepalive”, option in the above configuration defines the amount of time in seconds, in which the system will check the clients connected with TCP pings to determine if it is still connected.
commenting out the “bind 127.0.0.1” in the above shown configuration will make redis listen on all interfaces rather than only localhost. Due to this reason, we will have to make sure our redis server is configured with a very complex password, to prevent from getting compromised.
“requirepass” option is pretty straightforward. Simply give a complex password.
“maxmemory-policy noeviction” will keep the data in redis indefinitely.
the remaining two options “appendonly” & “appendfilename” are used for backing up redis data.
As we have made our required redis master configuration inside /etc/redis/redis.conf, let’s restart redis-server.
#sudo service redis-server restart
Configuring Redis Slave
Configuring redis-slave is also almost similar to how we configured redis master. Let’s modify our slave server’s /etc/redis/redis.conf file, and make sure it has the following lines.
slaveof redis-master-ip 6379
Here also commenting out “bind 127.0.0.1” will make redis to listen on all interfaces rather than only loopback. And “requirepass” option will set a password for accessing this redis slave server.
“slaveof” & “masterauth” are the two main options that makes this redis server a slave. “slaveof” option defines the master server IP and port. And “masterauth” defines the credentials for accessing master redis server (the password that we defined in redis.conf of master server under the option “requirepass”)
Once you have modified redis.conf with the above options on redis slave server, the next step is to restart redis service.
#sudo service redis-server restart
How to connect to Redis, and check Replication?
You can always connect to redis and check the replication status from cli itself. Let’s connect to redis master server (please note the fact that the below command should be fired from the master server, else you should replace 127.0.0.1 in the command with the public ip of the server.)
#redis-cli -h 127.0.0.1 -p 6379
Now you will have to enter our master server’s password for authenticating(the password we gave inside /etc/redis/redis.conf file while configuring redis master server.). This can be done as shown below.
Now let’s see the status by running INFO command as shown below.
The output will have many details, and to confirm our master slave replication setup worked, we need to look into the section called “Replication”
Note: Full output of the command is not shown above. The replication section will also show the slave IP, slave status.
Similarly if you fire the same above INFO command after getting connected to slave-redis server, you should be able to see the “role” as slave under “Replication” section, and master ip and status as well.
You can turn off the replication feature from slave server anytime when required with one single command. This is really handy when you want to quickly make the slave server a redis-master, during crisis.
127.0.0.1:6379>SLAVEOF NO ONE
If all goes well, the correct response for the above command should be “OK”. If you again fire up the INFO command after the above SLAVEOF command, you should be able to see that the slave server is no more a slave but a master (under #Replication section).
Basically this failover part should be handled inside the application logic(the application that connects to redis server). Or you can script this part by converting the slave as a master (on finding a failure on the master) and send all redis bound requests to the new master.
Note: Please note the fact that we discussed about one master and one slave configuration here..You can actually have as many slaves as you want, by the exact same method described in this tutorial.