So in this session we are going to talk about Redis Replication.
So whenever we are setting Redis Replication.
Basically what we are doing is, we are setting a Master and aSlave instances.
So you'll be having Redis Master running on, one of the servers and then you can have N number
of Slaves running on different servers.
Okay.
So the scenario which we are going to see today. In this scenario we are going to create 3 Slaves instances
Okay and then all of these 3, these 3 instances are going to be a replica of Master.
So when I say Replica of Master, which means whatever key changes which are being done in Master that
is automatically going to be synced in each of these Replicas and Master remains as usual. whenever you
are setting Replication there is no difference in the Configuration of Master. However, in the Slave
you need to go and edit Redis.conf file and in the Redis.conf file, you need to provide Master
you know Master Redis Information.
So we'll see how to provide those information and also major point to remember is, that all these read
only, I mean all these Redis slave instances remains Read Only Mode.
So all the users can connect to all the Slave Nodes but they can only do a Read Only Operations.
So if, if any of the user tries to write any or tries to make any Key Changes in Redis Slave Instances,
they are going to get the error that these instances are read only instance. However, if any user is connected
to Master, they'll be able to perform Read and Write both the operations as usual.
So now in our environment, we are going to run 4 instances of Redis on the same server.
OK.
But in a typical Production Environment, you are going to actually run all the Slaves on a Separate
Server and also the Master is going to be on a Separate Server.
However, for the purpose of demonstration we are just going to create 4 copies of this Redis Software
and then we are going to make a change in the Port and then we'll be running all the 4 instances
on the same server and we'll be making Configuration Change in each of the Slave Instance.
So let's see how this works.
So this is the current folder structure which we have. So right now I am in Redis admin's home directory
and the software working Redis software, which we have installed previously, that was under Redis-stable
directory.
Now I am going to create exact copy of this software
and to the master instance I'm going to name Redis-M
and then all this slave instance from 1 to 3, I am going to name Redis-S1, Second instance
is going to be called Redis-S 2 and third instance going to be called Redis-S 3
OK now if you see this is our folder structure. So Master is here and then we have 3 Slaves.
So now the Master can be started as usual and we need to make changes in the Configuration File of Slaves
Redis Instance.
Let's go to Master Directory first and Let's see if we would like to make any changes in the Configuration File
Please notice that master is going to run on Port 6 3 7 0
So this also means that we'll not be able to run all the other instances on this port. okay Master
is going to use Port 6 3 7 0, then we will have to change the port for Slave 1 and make it some other port
probably we can change it to 6 3 7 1 and Slave 2 we can change it to 6 3 7 2 and Slave 3 we will be changing it to
6 3 7 3
So other than this if you notice there is one more property which is called log file. So by default when
we start this Redis instance, it is going to write all the logs in to /tmp/redis.log
So I'm going to rename it to, Redis _m.log. So we'll be able to know the logs which is
created by Redis Master Instance
Okay here is our RDB File names. So this also I am going to change it to _m Postfix. So
this is going to denote that this is a Master Node and then directory, I'm going to change it to
let's keep/home/redisadmin. Okay so that's all. We can now close this one and we will not
start Redis as of now.
Let's go back and let's make the configuration in Slave Nodes as well.
Now I'm going to modify Redis.conf File of first Slave.
Now here
Search for word replica.
OK.
So once you search with word replica, you need to come to this line where it says replica of
And this is the place where you need to make changes.
OK.
So here you need to define where your Redis Master Server
is running and what is the port on which Redis application is running.
So in this case since it is local host. So either we can write local host or we can define the port for
local host port is always 127.0.0.1
And as we know our Master is going to run on Port 6 3 7 0.
So here we need to provide the information of the port, where Master is running.
Okay.
So we have made the changes. Other than this change,
the second change which we need to make is, let's also change the RDB File name and add a post fix of S 1
So it will denote that this RDB is of Slave 1
and again in the directoty, I'm going to change it to Redis Admin
also we had Log file. So Log file also we can add a post fix which is S 1 and that's it. Now
we can Save this file and Exit.
Now we need to do the same in Redis - S 2 as well
Okay so this is Slave Node 2 and we need to make sure Slave Node 2 is running on Port 6 3 7 2
and log file name we can post fix _S 2
db file name also I'm going to post fix S 2 and directory, I'm going to change it to
/home/redisadmin
Okay and here
is the keyword replica of, here we need to provide information about Master.
So since Master is running on local host. We can provide 127.0.0.1 and Port, we can change it to the Port
on which Master is running. So it is going to run on Port 6 3 7 0
and that's is. Now we can save and exit.
and now similar thing we need to do here in Redis.conf of Slave 3
So just make sure you change the port.
It is going to run on port 6 373
and I'm post fixing S 3 in the Log File.
In the RDB File Name also i am post fixing S 3
and in the Directory Name, I have changed it to /home/redisadmin and now the most important
step is again making change in replica of.
So by default line remains commented. You need to remove # and make it uncomment
And then after replica of. You need to add IP address of the Master Instance and also the Port on which
Master Redis Instance is running. So it is 6 3 7 0
okay.
So we have made all the changes.
Now I would just like to verify
by going inside
Redis- S 1. I just want to cross-check that I had made the changes in the port
because I think we might have just missed it. Yeah
So what I guess we have actually missed making a change in in the Port of Slave 1.
Okay so we have made the change in port and change the port to 6 3 7 1.
So now if I go back to the architecture which i shown you.
So basically we are going to have 4 Nodes.
Master is going to run on Port 6 3 7 0.
Then we have slave or S 1, which is going to run on Port 6 3 7 1, Slave 2 is going to run on Port 6 3 7 2 and
slave 3 is going to run on Port 6373
and all of these 3 slaves are going to be a Replica of Master Node.
So let's clear the screen and let's start restarting each of the instance or starting each of the instance.
So I'll go inside Master Directory
and we'll start Redis server and also we need to provide path of the Configuration File and let's run it
as a background process
we can also verify the same by doing ps -ef and grep on the Redis name.
Now we can see there is a Redis process which is running and using Port 6 3 7 0.
So this is our master Node.
Now let's go to Redis- S 1 and under the src directory, we have Redis-server and
we also need to pass the name of the Redis.conf file and we are also going to start it as a background
process.
Now I see there's another Redis process which is running and it is running on 6 3 7 1
and now let's start the remaining 2 nodes.
So I'm going to go inside Redis- S 2
okay.
So now we have another Slave running which is on Port 6 3 7 2
Okay.
So now all our 4 instances are running and Master is running on 6 3 7 0 and slaves are running on
6 3 7 1, 6 3 7 2 and 6 3 7 3
Now if you remember all our logs are being stored under /tmp directory. So let's move
on to /tmp directory.
and here if you remember we have changed the Logs for all the Slave 1,2 and 3
We had added up post fix of S1, S2 and S3.
So this is how we know that these logs are coming from. This S 1 is coming from Slave 1.
So let's see what is being displayed in this log file.
Okay.
So if you see the content of this log file.
It says that this Redis instance was started and then it tried to connect to Master on this IP address
and port 6 3 7 0 and then it was able to successfully connect
and then.
So basically now they both are in sync
and if we check for Redis_S 2, we should be able to see the similar message. So here
also we see the similar message.
And for Redis S 3 also, we see the similar message.
Okay so all our Slave Nodes has been successfully able to sync up with Master Node and also able
to perform the sync up or Replication.
Now lets say what is the logs, which is displayed in Redis Master.
So here we see, in the Master we see that there are 3 entries.
One is denoting that there is a Replica which is running on 6 3 7 1, Port 6 3 7 1.
And it tried to connect to or on Made a request ofor sync request or replicate request to this Master Node.
And then again we have the similar information for 6 3 7 2 and then we have similar for 6 3 7 3.
So it means our Master and Slave.
They were able to sync up properly and it was able to connect.
Now let's connect to each of these nodes by using Redis-cli
And by providing the port by using -p and we know our Master is running on Port 6 3 7 0 and
if I do dbsize here.
So right now we do not have anything in the database.
It is blank.
So I'll use L push command and I'll add, I'll create a key name which is called Color.
I will add the values, which is Red, Green, Black and White.
So we have added some Color Names to this list, which is called Color
and now if I want to see all the values which is inside this key, which is Color. I can do lrange
key name 0 and -1.
So this is showing us all the values, which was available in this Key Name, which is called Color.
Okay so this is the situation of Master Node
and now let's exit from here.
And let's connect to one of the Slave Nodes. So our Slave Nodes are running starting from 6 3 7 1 then
6 3 7 2, and 6 3 7 3.
So let's connect to 6 3 7 1 and let's do the dbsize.
We can see we have 1 entry here, because we have only 1 List item, which is called Color.
And if I do Keys * i can see the Key Name Color is here.
And if I do lrange color 0 -1. We can see all the values has already been replicated
to this Slave Node.
Now let's exit from here and check the same in Node 6 3 7 2
Okay so we see the same information here as well.
Let's connect to 6 3 7 3. This is our third slave Node.
All right.
So here also we see the same information.
So whatever we had added in Master Node, it has only been replicated to all the 3 Slave Nodes.
Now let's try to, as you saw we're able to perform the Get Operations on all the Slave Nodes. So I
can do like, i'am able to run lrange which actually giving us all the values, which is available in
Color key.
Now let's try to write something in this Node, in one of the slave Nodes and see if It is working.
So I'll use Set command, and let's say we want to add a Product which is called Chair, Value is
100 and hit enter.
Now you see when we try to write something, something to one of the Replica Nodes. We get the error saying
Read Only.
You cannot write against a Read Only Replica.
So basically you cannot perform a write operation, to a Read Only Replica but you can perform or any
number of Users can connect to Read Only Replica and perform the Read Only Operations.
Now let's see the process again.
and we see we have 4 Instances of Redis running. So we will try to disconnect Master.
OK.
So I will shut down the services of Master
So this is a process ID for the Master Node. So i'll do kill hyphen, kill -9 and provide the
process ID. Hit enter and now let's check the process again. Okay.
So now we see Master process is dead and now let's go to the Logs and see what is the error we see
in the Slave Logs.
okay.
Now we see on the Slave 1, I'm able to see that there is connection refused errors.
Similarly if I go to Redis_S 2, which is Slave 2 log. Here also we see the same error
and in the Third Slave Node also, we get the same error.
Okay.
So if Master is down, Replicas are still running. It is going to give us error, that it is not able
to sync with Master.
However it doesn't mean that you cannot connect to Slave Nodes and you cannot perform Read Only Operation.
So you still can connect to all the Slave Nodes and you can still carry on performing the Read Only
Operations.
So I will connect to one of the Slave Nodes again
and here if you see, I'm still able to connect to Slave Node, which is running on 6371 and I'm able
to perform all the Read Only Operations. So that's all about Redis Replication.