1
00:00:02,370 --> 00:00:05,760
So in this session we are going to talk about Redis Replication.

2
00:00:08,920 --> 00:00:13,270
So whenever we are setting Redis Replication.

3
00:00:13,270 --> 00:00:18,290
Basically what we are doing is,  we are setting a Master and aSlave instances.

4
00:00:18,580 --> 00:00:24,520
So you'll be having Redis Master running on, one of the servers and then you can have N number

5
00:00:24,520 --> 00:00:27,970
of Slaves running on different servers.

6
00:00:28,730 --> 00:00:29,070
Okay.

7
00:00:29,080 --> 00:00:36,760
So the scenario which  we are going to see today.  In this scenario we are going to create 3 Slaves instances

8
00:00:38,060 --> 00:00:44,570
Okay and then all of these 3,  these 3 instances are going to be a replica of Master.

9
00:00:45,080 --> 00:00:50,480
So when I say Replica of Master,  which means whatever key changes which are being done in Master that

10
00:00:50,480 --> 00:00:59,340
is automatically going to be synced in each of these Replicas and Master remains as usual. whenever you

11
00:00:59,340 --> 00:01:06,480
are setting Replication there is no difference in the Configuration of Master.  However,  in the Slave

12
00:01:06,570 --> 00:01:15,240
you need to go and edit Redis.conf file and in the Redis.conf file,  you need to provide Master

13
00:01:15,270 --> 00:01:17,610
you know Master Redis Information.

14
00:01:17,610 --> 00:01:25,140
So we'll see how to provide those information and also major  point to remember is, that all these read

15
00:01:25,140 --> 00:01:29,550
only,  I mean all these Redis slave instances remains Read Only Mode.

16
00:01:30,330 --> 00:01:37,090
So all the users can connect to all the Slave Nodes but they can only do a Read Only Operations.

17
00:01:37,200 --> 00:01:45,500
So if,  if any of the user tries to write any or tries to make any Key Changes in Redis Slave Instances,

18
00:01:45,540 --> 00:01:54,380
they are going to get the error that these instances are read only instance.  However,  if any user is connected

19
00:01:54,380 --> 00:02:03,770
to Master,  they'll be able to perform Read and Write both the operations as usual.

20
00:02:03,780 --> 00:02:10,700
So now in our environment,  we are going to run 4  instances of Redis on the same server.

21
00:02:11,230 --> 00:02:11,660
OK.

22
00:02:11,680 --> 00:02:17,430
But in a typical Production Environment, you are going to actually run all the Slaves on a Separate

23
00:02:17,430 --> 00:02:22,560
Server and also the Master is going to be on a Separate Server.

24
00:02:22,560 --> 00:02:28,830
However,  for the purpose of demonstration we are just going to create 4 copies of this Redis Software

25
00:02:29,340 --> 00:02:34,290
and then we are going to make a change in the Port and then we'll be running all the 4 instances

26
00:02:34,290 --> 00:02:43,500
on the same server and we'll be making Configuration Change in each of the Slave Instance.

27
00:02:43,710 --> 00:02:45,060
So let's see how this works.

28
00:02:55,800 --> 00:03:00,930
So this is the current folder structure which we have.  So right now I am in Redis admin's home directory

29
00:03:01,890 --> 00:03:08,500
and the software working Redis software, which  we have installed previously,  that was under Redis-stable

30
00:03:08,520 --> 00:03:09,800
directory.

31
00:03:09,840 --> 00:03:13,740
Now I am going to create exact copy of this software

32
00:03:17,780 --> 00:03:23,960
and to the master instance I'm going to name Redis-M

33
00:03:26,810 --> 00:03:33,340
and then all this slave instance from 1 to 3,  I am going to name  Redis-S1, Second instance

34
00:03:33,360 --> 00:03:39,240
is going to be called Redis-S 2  and  third instance going to be called Redis-S 3

35
00:03:42,540 --> 00:03:51,480
OK now if you see this is our folder structure. So Master is here and then we have 3 Slaves.

36
00:03:51,500 --> 00:04:01,180
So now the Master can be started as usual and we need to make changes in the Configuration File of Slaves

37
00:04:01,190 --> 00:04:01,970
Redis  Instance.

38
00:04:05,970 --> 00:04:13,830
Let's go to Master Directory first and Let's  see  if we would like to make any changes in the Configuration File

39
00:04:21,610 --> 00:04:25,470
Please notice that master is going to run on Port 6 3 7 0

40
00:04:28,790 --> 00:04:34,880
So this also means that we'll not be able to run all the other instances on this port.  okay Master

41
00:04:34,880 --> 00:04:42,170
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

42
00:04:42,320 --> 00:04:50,210
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

43
00:04:50,210 --> 00:04:51,590
6 3 7 3

44
00:05:02,900 --> 00:05:07,790
So other than this if you notice there is one more property which is called log file.  So by default when

45
00:05:07,790 --> 00:05:17,310
we start this Redis  instance,  it is going to write all the logs in to /tmp/redis.log

46
00:05:17,330 --> 00:05:27,230
So I'm going to rename it to,  Redis _m.log. So we'll be able to know the logs which is

47
00:05:27,230 --> 00:05:29,930
created by Redis  Master Instance

48
00:05:37,540 --> 00:05:46,580
Okay here is our RDB File names. So this also I am going to change it  to _m  Postfix. So

49
00:05:46,580 --> 00:05:52,530
this is going to denote that this is a Master Node  and then directory,  I'm going to change it to

50
00:05:59,230 --> 00:06:08,020
let's keep/home/redisadmin.  Okay so that's all. We can now close this one and we will not

51
00:06:08,020 --> 00:06:09,370
start Redis  as of now.

52
00:06:09,370 --> 00:06:26,490
Let's go back and let's make the configuration in Slave Nodes  as well.

53
00:06:26,510 --> 00:06:30,770
Now I'm going to modify Redis.conf File of first Slave.

54
00:06:35,310 --> 00:06:35,840
Now here

55
00:06:35,850 --> 00:06:38,180
Search  for  word replica.

56
00:06:51,430 --> 00:06:51,780
OK.

57
00:06:51,820 --> 00:06:59,190
So once you search with  word replica,  you need to come to this line where it says  replica of

58
00:06:59,670 --> 00:07:02,290
And this is the place where you need to make changes.

59
00:07:02,440 --> 00:07:02,770
OK.

60
00:07:02,800 --> 00:07:06,410
So here you need to define where your Redis Master Server

61
00:07:06,690 --> 00:07:11,580
is running and what is the  port on which Redis application is running.

62
00:07:12,750 --> 00:07:21,620
So in this case since it is local host.  So either we can write local host or we can define the port for

63
00:07:21,630 --> 00:07:27,340
local host port is always 127.0.0.1

64
00:07:27,540 --> 00:07:31,320
And as we know our Master is going to run on  Port  6 3 7 0.

65
00:07:31,320 --> 00:07:35,820
So here we need to provide the information of the port,  where Master is running.

66
00:07:36,270 --> 00:07:37,150
Okay.

67
00:07:37,290 --> 00:07:41,010
So we have made the changes.  Other than this change,

68
00:07:44,710 --> 00:07:52,420
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

69
00:07:52,510 --> 00:07:58,910
So it will  denote that this RDB is of Slave 1

70
00:08:09,770 --> 00:08:12,670
and again in the directoty,  I'm going to change it to Redis Admin

71
00:08:18,670 --> 00:08:26,590
also we had Log file.  So Log file also we can add a post fix which is  S 1 and that's it.  Now

72
00:08:26,590 --> 00:08:28,180
we can Save this file and Exit.

73
00:08:38,120 --> 00:08:41,920
Now we need to do the same in Redis - S 2  as well

74
00:08:57,590 --> 00:09:03,770
Okay so this is Slave Node 2 and we need to make sure Slave Node  2 is running on Port 6 3 7 2

75
00:09:11,810 --> 00:09:16,000
and log file name we can post fix _S 2

76
00:09:27,360 --> 00:09:35,110
db file name also I'm going to post fix S 2 and directory,   I'm going to change it to

77
00:09:38,660 --> 00:09:39,660
/home/redisadmin

78
00:09:42,670 --> 00:09:43,690
Okay and here

79
00:09:47,460 --> 00:09:51,330
is the keyword replica of,   here we need to provide information about Master.

80
00:09:57,870 --> 00:10:06,490
So since Master is running on local host.  We can provide 127.0.0.1 and  Port,  we can change it  to the Port

81
00:10:06,490 --> 00:10:10,930
on which Master is running.  So it is going to run on Port 6 3 7 0

82
00:10:14,710 --> 00:10:16,480
and that's  is.  Now we can save and exit.

83
00:10:24,510 --> 00:10:31,140
and now similar thing we need to do here in Redis.conf of Slave 3

84
00:10:35,380 --> 00:10:37,490
So just make sure you change the port.

85
00:10:37,490 --> 00:10:39,760
It is going to run on port 6 373

86
00:10:55,510 --> 00:10:58,240
and I'm post fixing S 3 in the Log File.

87
00:11:06,770 --> 00:11:09,780
In the RDB File Name also i am post fixing S 3

88
00:11:19,680 --> 00:11:26,420
and in the Directory Name,  I have changed it to /home/redisadmin and now the most important

89
00:11:26,420 --> 00:11:29,390
step is again making change in replica of.

90
00:11:29,390 --> 00:11:35,590
So by default line remains commented. You need to remove # and make it uncomment

91
00:11:35,900 --> 00:11:45,160
And then after replica of.  You need to add IP address of the Master Instance and also the Port on which

92
00:11:45,170 --> 00:11:49,000
Master Redis Instance is running.  So it is 6 3 7 0

93
00:11:53,950 --> 00:11:54,270
okay.

94
00:11:54,290 --> 00:11:56,010
So we have made all the changes.

95
00:11:56,030 --> 00:11:57,890
Now I would just like to verify

96
00:12:02,020 --> 00:12:03,030
by going inside

97
00:12:03,030 --> 00:12:09,670
Redis- S 1.  I just want to cross-check that I had made the changes  in the port

98
00:12:14,570 --> 00:12:18,240
because I think we might have just missed it. Yeah

99
00:12:18,980 --> 00:12:26,050
So what I guess we have actually missed making a change in in the Port of Slave 1.

100
00:12:26,120 --> 00:12:34,800
Okay so we have made the change in port and change the port to 6 3 7 1.

101
00:12:34,840 --> 00:12:40,090
So now if I go back to the architecture which i shown you.

102
00:12:40,180 --> 00:12:43,280
So basically we are going to have 4 Nodes.

103
00:12:43,300 --> 00:12:46,360
Master is going to run on Port 6 3 7 0.

104
00:12:46,360 --> 00:12:54,820
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

105
00:12:54,940 --> 00:12:57,670
slave 3 is going to run on Port 6373

106
00:13:01,020 --> 00:13:07,130
and all of these 3 slaves are going to be a Replica of Master Node.

107
00:13:16,270 --> 00:13:23,500
So let's clear the screen and let's start restarting each of the instance or starting each of the instance.

108
00:13:23,770 --> 00:13:26,520
So I'll go inside Master Directory

109
00:13:40,110 --> 00:13:48,510
and we'll start Redis server and also we need to provide path of the Configuration File and let's run it

110
00:13:48,510 --> 00:13:50,030
as a background process

111
00:13:54,330 --> 00:14:02,410
we can also verify the same by doing ps -ef and grep on the Redis name.

112
00:14:02,550 --> 00:14:06,680
Now we can see there is a Redis process which is  running and using Port 6 3 7 0.

113
00:14:06,690 --> 00:14:07,970
So this is our master Node.

114
00:14:18,650 --> 00:14:28,430
Now let's go to Redis- S 1 and under the src directory,  we have Redis-server and

115
00:14:28,430 --> 00:14:34,640
we also need to pass the name of the Redis.conf file and we are also going to start it as a background

116
00:14:34,640 --> 00:14:35,120
process.

117
00:14:40,290 --> 00:14:47,100
Now I see there's another Redis process which is running and it is running on 6 3 7 1

118
00:14:52,860 --> 00:14:54,940
and now let's start the remaining 2 nodes.

119
00:15:01,020 --> 00:15:04,010
So I'm going to go inside Redis- S 2

120
00:15:21,570 --> 00:15:21,980
okay.

121
00:15:22,000 --> 00:15:25,570
So now we have another Slave running which is on Port 6 3 7 2

122
00:15:49,800 --> 00:15:50,130
Okay.

123
00:15:50,140 --> 00:15:56,140
So now all our 4 instances are running and Master is running on 6 3 7 0 and slaves are running on

124
00:15:56,140 --> 00:15:59,080
6 3 7 1, 6 3 7 2 and 6 3 7 3

125
00:16:01,920 --> 00:16:07,500
Now if you remember  all our logs are being stored under /tmp directory. So let's move

126
00:16:07,500 --> 00:16:09,150
on to /tmp directory.

127
00:16:11,880 --> 00:16:16,660
and here if you remember we have changed the Logs for all the Slave 1,2 and 3

128
00:16:16,680 --> 00:16:20,670
We had added up post fix of S1, S2 and S3.

129
00:16:20,670 --> 00:16:27,620
So this is how we know that these logs are coming from. This S 1 is coming from Slave 1.

130
00:16:28,170 --> 00:16:33,130
So let's see what is being displayed in this log file.

131
00:16:34,420 --> 00:16:34,710
Okay.

132
00:16:34,720 --> 00:16:37,170
So if you see the content of this log file.

133
00:16:40,640 --> 00:16:47,540
It says that this Redis instance was started and then it tried to connect to Master on this IP address

134
00:16:47,600 --> 00:16:52,370
and port 6 3 7 0 and then it was able to successfully connect

135
00:16:55,060 --> 00:16:55,740
and then.

136
00:16:55,780 --> 00:16:58,270
So basically now they both are in sync

137
00:17:03,230 --> 00:17:09,530
and if we check for Redis_S 2, we should be able to see the similar message.  So here

138
00:17:09,530 --> 00:17:10,940
also we see the similar message.

139
00:17:17,820 --> 00:17:20,520
And for Redis S 3 also, we see the  similar message.

140
00:17:20,970 --> 00:17:27,960
Okay so all our Slave Nodes has been successfully able to sync up with Master  Node and also able

141
00:17:27,960 --> 00:17:32,520
to perform the sync up or Replication.

142
00:17:32,520 --> 00:17:49,530
Now lets say what is the logs, which is displayed in Redis Master.

143
00:17:49,560 --> 00:17:54,990
So here we see,  in the Master we see that there are 3 entries.

144
00:17:55,120 --> 00:18:01,260
One is denoting that there is a Replica which is running on 6 3 7 1,  Port 6 3 7 1.

145
00:18:01,270 --> 00:18:11,210
And it  tried to connect to or on Made a  request ofor sync request or replicate request to this Master Node.

146
00:18:11,380 --> 00:18:17,900
And then again we have the similar information for 6 3 7 2 and then we have similar for 6 3 7 3.

147
00:18:18,070 --> 00:18:19,630
So it means our Master and Slave.

148
00:18:19,630 --> 00:18:22,240
They were able to sync up properly and it was able to connect.

149
00:18:25,230 --> 00:18:30,380
Now let's connect to each of these nodes by using Redis-cli

150
00:18:31,170 --> 00:18:40,000
And by providing the port by using -p and we know our Master is running on Port 6 3 7 0 and

151
00:18:40,000 --> 00:18:41,570
if I do dbsize here.

152
00:18:41,590 --> 00:18:44,110
So right now we do not have anything in the database.

153
00:18:45,400 --> 00:18:46,090
It is blank.

154
00:18:50,630 --> 00:18:57,680
So I'll use L push command and I'll add, I'll create a key name which is called Color.

155
00:18:57,680 --> 00:19:03,380
I will add the values,  which is  Red, Green, Black and White.

156
00:19:03,380 --> 00:19:07,280
So we have added some Color Names to this list, which is called Color

157
00:19:10,550 --> 00:19:18,750
and now if I want to see all the values which is  inside this key,  which is Color.  I can do lrange

158
00:19:18,780 --> 00:19:24,170
key name  0 and  -1.

159
00:19:24,200 --> 00:19:31,230
So this is showing us all the values, which was available in this Key Name, which is called Color.

160
00:19:32,200 --> 00:19:34,820
Okay so this is the situation of Master Node

161
00:19:38,990 --> 00:19:40,910
and now let's exit from here.

162
00:19:41,060 --> 00:19:52,770
And let's connect to one of the Slave Nodes.  So our Slave Nodes are running starting from 6 3 7 1 then

163
00:19:52,780 --> 00:19:54,760
6 3 7 2, and  6 3 7 3.

164
00:19:55,930 --> 00:20:00,020
So let's connect to 6 3 7 1 and let's do the dbsize.

165
00:20:00,670 --> 00:20:06,930
We can see we have 1 entry here, because we have only 1 List item, which is called Color.

166
00:20:08,620 --> 00:20:14,120
And if I do Keys * i  can see the Key Name Color is here.

167
00:20:14,350 --> 00:20:22,570
And if I do lrange color 0   -1. We can see all the values has already been replicated

168
00:20:22,570 --> 00:20:24,250
to this Slave Node.

169
00:20:27,780 --> 00:20:32,750
Now let's exit from here and check the same in Node 6 3 7 2

170
00:20:42,960 --> 00:20:45,440
Okay so we see the same information here as well.

171
00:20:47,600 --> 00:21:03,600
Let's connect to 6 3 7 3. This  is our third slave Node.

172
00:21:03,670 --> 00:21:03,980
All right.

173
00:21:03,990 --> 00:21:07,050
So here also we see the same information.

174
00:21:07,050 --> 00:21:17,000
So whatever we had added in Master Node, it has only been replicated to all the 3 Slave Nodes.

175
00:21:17,000 --> 00:21:24,850
Now let's try to,  as you saw we're able to perform the Get Operations on all the Slave Nodes.  So I

176
00:21:24,850 --> 00:21:33,190
can do like, i'am able to run lrange which  actually giving us all the values, which is available in

177
00:21:33,190 --> 00:21:34,290
Color key.

178
00:21:34,330 --> 00:21:40,840
Now let's try to write something in this Node,  in one of the slave Nodes and see if It is working.

179
00:21:40,840 --> 00:21:47,880
So I'll use Set command,  and let's say we want to add a Product which is called Chair, Value is

180
00:21:47,880 --> 00:21:50,720
100 and hit enter.

181
00:21:50,740 --> 00:21:56,340
Now you see when we try to write something,  something to one of the Replica Nodes.  We get the error saying

182
00:21:56,860 --> 00:21:57,510
Read Only.

183
00:21:57,520 --> 00:22:06,080
You cannot write against a Read Only Replica.

184
00:22:06,130 --> 00:22:13,660
So basically you cannot perform a write operation,  to a Read Only  Replica but you can perform or any

185
00:22:13,660 --> 00:22:20,320
number of Users can connect to Read Only  Replica and perform the Read Only  Operations.

186
00:22:20,490 --> 00:22:23,210
Now let's see the process again.

187
00:22:24,000 --> 00:22:30,990
and we see  we have 4 Instances of Redis running. So we will try to disconnect Master.

188
00:22:31,820 --> 00:22:32,050
OK.

189
00:22:32,100 --> 00:22:36,180
So I will shut down the services of Master

190
00:22:39,700 --> 00:22:49,210
So this is a process ID for the Master Node. So i'll do kill hyphen,  kill -9 and provide the

191
00:22:49,360 --> 00:22:56,130
process ID. Hit enter and now let's check the process again. Okay.

192
00:22:56,130 --> 00:23:07,040
So now we see Master process is dead and now let's go to the Logs and see what is the error we see

193
00:23:07,040 --> 00:23:08,340
in the Slave Logs.

194
00:23:15,210 --> 00:23:15,540
okay.

195
00:23:15,560 --> 00:23:21,970
Now we see on the Slave 1,  I'm able to see that there is  connection refused errors.

196
00:23:22,490 --> 00:23:30,100
Similarly if I go to Redis_S 2,  which  is Slave 2 log.  Here  also we see the same error

197
00:23:36,850 --> 00:23:39,930
and in the Third Slave Node also,  we get the same error.

198
00:23:40,680 --> 00:23:40,930
Okay.

199
00:23:40,950 --> 00:23:47,790
So if Master is down,  Replicas are still running.  It is going to give us  error, that it is not able

200
00:23:47,790 --> 00:23:48,790
to sync with Master.

201
00:23:48,790 --> 00:23:55,170
However it doesn't mean that you cannot connect to Slave Nodes and you cannot perform Read Only Operation.

202
00:23:55,170 --> 00:24:01,050
So you still can connect to all the Slave Nodes and you can still carry on performing the Read Only

203
00:24:01,050 --> 00:24:04,640
Operations.

204
00:24:05,200 --> 00:24:07,390
So I will connect to one of the Slave Nodes again

205
00:24:18,430 --> 00:24:25,610
and here if you see,  I'm still able to connect to Slave Node,  which is running on 6371  and I'm able

206
00:24:25,620 --> 00:24:32,110
to perform all the Read Only Operations.  So that's all about Redis Replication.
