1
00:00:02,030 --> 00:00:05,350
So in this session we are going to learn about Redis Transactions

2
00:00:07,860 --> 00:00:12,610
and basically Redis Transaction is supported by these set of 5 commands.

3
00:00:12,680 --> 00:00:12,950
OK.

4
00:00:12,960 --> 00:00:21,930
which  we are going to use. Now to give you an example of Inconsistent Transaction. So let's say we have,

5
00:00:22,010 --> 00:00:28,110
you know, User 1 which is going to see the total number of available courses and then it is going to

6
00:00:28,110 --> 00:00:29,770
enroll for the course.

7
00:00:29,790 --> 00:00:34,890
So let's have an user 1 queries Redis,  to see the total number of courses available along with the price.

8
00:00:35,480 --> 00:00:42,040
Redis replies that there are total 3 courses available and the price is 500$ and you

9
00:00:42,070 --> 00:00:49,940
know, when user 1 tries to enroll for the course. Just before that there is another user User 2. It enrolls for

10
00:00:50,130 --> 00:00:51,430
all the three courses

11
00:00:51,460 --> 00:00:51,840
Okay.

12
00:00:51,870 --> 00:00:55,700
Successfully and now when User 1 tries to enroll.

13
00:00:55,820 --> 00:00:56,260
Redis

14
00:00:56,580 --> 00:01:04,350
And if Redis  allows it to enroll. It is going to be inconsistent state, because we have only 3

15
00:01:04,590 --> 00:01:06,090
courses available to enroll.

16
00:01:06,090 --> 00:01:13,270
And basically we have already allowed 3, User2 for three enrollments and if we allow User

17
00:01:13,280 --> 00:01:18,440
1 also for the enrollment, then basically we are going to have you know, more number of enrollments

18
00:01:18,450 --> 00:01:20,290
than the available seats.

19
00:01:23,040 --> 00:01:31,770
So if you are, you know, by Atomic Transaction, what it means is that basically when User 1 is issuing

20
00:01:31,770 --> 00:01:36,260
this transaction. User 1 is going to issue this transaction as a Transaction.

21
00:01:36,570 --> 00:01:36,870
Okay

22
00:01:39,630 --> 00:01:48,830
So if, you know, if the value of the Ticket is changing or let's say the availability of the Ticket is

23
00:01:48,830 --> 00:01:52,040
changing. That basically the transaction is going to get failed.

24
00:01:52,690 --> 00:01:53,830
Okay.

25
00:01:54,080 --> 00:02:01,240
So in transaction basically all the commands execute successfully or  the transaction fails.

26
00:02:01,280 --> 00:02:01,520
Okay.

27
00:02:01,520 --> 00:02:09,110
That is the property of a  transaction. So by that what I mean is, Transaction basically is going to guarantee

28
00:02:09,110 --> 00:02:15,680
you that all operation which, you are performing is going to be executed sequentially and also either

29
00:02:15,710 --> 00:02:20,390
all of them are going to be successful or none of them are going to be successful.

30
00:02:21,350 --> 00:02:21,680
Okay.

31
00:02:21,710 --> 00:02:26,060
So it is going to make sure that you don't run into an Inconsistent State.

32
00:02:34,210 --> 00:02:34,470
Okay.

33
00:02:34,480 --> 00:02:42,520
Now here let's say we have a Key, which is called Ticket Available and now let's say we just want to see

34
00:02:42,520 --> 00:02:47,950
how many tickets available and we want to decrement the value of ticket and that's it.

35
00:02:48,160 --> 00:02:48,530
Okay.

36
00:02:48,610 --> 00:02:53,200
And  both of these basically should be running as a Transaction. It should not be running as Individual

37
00:02:53,200 --> 00:02:53,590
Command.

38
00:02:53,770 --> 00:02:54,100
Okay.

39
00:02:54,130 --> 00:02:59,920
So by Individual Command, what I mean is you know,  running Get Ticket Available.

40
00:03:00,090 --> 00:03:00,330
Okay.

41
00:03:00,340 --> 00:03:06,470
This is running as a Single Command and then you have the option to Increment Ticket Available.

42
00:03:08,430 --> 00:03:08,890
Okay.

43
00:03:08,890 --> 00:03:11,460
and then let's say i wrote a  wrong Command.

44
00:03:14,020 --> 00:03:16,080
It doesn't impact previous commands.

45
00:03:16,090 --> 00:03:16,550
Okay.

46
00:03:17,700 --> 00:03:21,900
So these, all of these three commands which we ran, it is executed one by one.

47
00:03:21,900 --> 00:03:29,640
Now we want to do, is we want to run set of commands as a Transaction. Now to start a transaction

48
00:03:29,850 --> 00:03:32,870
you type multi, okay.

49
00:03:32,900 --> 00:03:39,050
That is how you indicate Redis that basically now you are going to execute a transaction.

50
00:03:39,050 --> 00:03:42,310
You don't want to execute commands line by line.

51
00:03:42,650 --> 00:03:43,830
Okay.

52
00:03:43,910 --> 00:03:53,220
So now  if I type ticket, Get Ticket Available you see the response which I have get is Queued, which means

53
00:03:53,390 --> 00:03:56,890
you know this command is not executed.  It has been Queued.

54
00:03:57,300 --> 00:03:58,170
And if I do

55
00:04:02,520 --> 00:04:08,210
Decrement Ticket Available and then we want to see the value again

56
00:04:11,510 --> 00:04:16,790
Okay now we have issued 3 commands but none of them has been executed by Redis.

57
00:04:16,910 --> 00:04:20,620
So it is only going to be executed when I type

58
00:04:20,750 --> 00:04:21,630
exec.

59
00:04:21,770 --> 00:04:22,090
Okay.

60
00:04:22,100 --> 00:04:27,240
So basically that is a way of telling that Execute this Transaction.

61
00:04:27,290 --> 00:04:31,760
So once I hit enter. Now you see all the three commands has been executed together.

62
00:04:33,450 --> 00:04:33,980
OK.

63
00:04:34,010 --> 00:04:35,040
And this this

64
00:04:35,300 --> 00:04:38,320
No transaction basically has been executed.

65
00:04:38,840 --> 00:04:42,850
And we got the Consistent Reply.

66
00:04:42,850 --> 00:04:46,450
Now let's say what happens, if there is an Error in one of the commands

67
00:04:51,470 --> 00:04:55,790
so once you write Execute. The previous transaction is already ended here.

68
00:04:56,000 --> 00:05:00,710
And if we want to start a New Transaction. We need to write Multi again.

69
00:05:00,710 --> 00:05:02,720
So I write Multi.  Hit enter.

70
00:05:06,790 --> 00:05:15,070
and now I'm going to write Get  Ticket Available and then we want to decrement the value.

71
00:05:15,850 --> 00:05:20,950
However, this time, let's say,you know, I don't write Decrement Ticket Available.

72
00:05:20,950 --> 00:05:29,110
I probably wrote let's say, wrong command and once I hit enter. It is going to give me an error.

73
00:05:29,370 --> 00:05:29,790
OK.

74
00:05:30,130 --> 00:05:39,500
But since we are running it as a Transaction. If I run the next command.  OK let's say if I run

75
00:05:39,510 --> 00:05:40,760
Decrement Ticket Available again

76
00:05:43,920 --> 00:05:46,150
and then I run  Get Ticket Available.

77
00:05:48,360 --> 00:05:48,620
OK.

78
00:05:48,660 --> 00:05:52,410
So what do you think is going to happen, when I run Execute in this

79
00:05:55,930 --> 00:05:56,210
OK.

80
00:05:56,280 --> 00:06:03,730
So if you're thinking that this is going to get successful. It is wrong, because you see one of our command

81
00:06:04,350 --> 00:06:06,300
in this transaction is failed.

82
00:06:06,450 --> 00:06:11,450
So basically this whole transaction is going to be treated as a Failed Transaction.

83
00:06:11,460 --> 00:06:11,710
Okay.

84
00:06:11,740 --> 00:06:20,840
So I'm going to hit enter and you see that Execution is Aborted. Transaction is Discarded because of

85
00:06:20,840 --> 00:06:29,540
previous errors. So when you have error in transaction. The whole transaction is aborted.

86
00:06:29,610 --> 00:06:34,410
It is not that one line of transaction is going to run and another line of transaction is not going to run.

87
00:06:34,410 --> 00:06:40,440
and the same has been explained here in the example as well.

88
00:06:40,440 --> 00:06:44,510
So if you just want to take a note, you can take a note from here.

89
00:06:44,520 --> 00:06:55,400
I'm going to give the link of this Blog in the Resource Section.

90
00:06:55,500 --> 00:06:58,680
Now we have something called Discarding a Transaction. Now let's say

91
00:07:02,650 --> 00:07:06,640
let me clear the screen first and let's say, in the middle of the transaction

92
00:07:06,640 --> 00:07:09,820
we realize that we don't want to continue with the transaction.

93
00:07:09,820 --> 00:07:12,670
We just want to discard this transaction.

94
00:07:12,670 --> 00:07:13,860
Then you can use Discard

95
00:07:13,860 --> 00:07:20,840
Command.

96
00:07:21,670 --> 00:07:24,900
So let's say rather than, you know, decrementing the value.

97
00:07:24,940 --> 00:07:29,640
By mistake we incremented the value of ticket available.

98
00:07:30,310 --> 00:07:33,370
And now I do not want this transaction to run.

99
00:07:33,400 --> 00:07:38,500
So if I type Execute, basically it is going to execute this transaction and the value of ticket available

100
00:07:38,500 --> 00:07:43,210
is going to be increased. Whereas actually we wanted it to be decreased.

101
00:07:43,990 --> 00:07:48,010
So in this case I can issue Discard Command and hit enter.

102
00:07:48,430 --> 00:07:51,160
And now we are out of this transaction.

103
00:07:51,160 --> 00:07:51,440
Okay.

104
00:07:51,460 --> 00:07:56,320
So this Multi is now closed. This Transaction is now closed.

105
00:07:56,410 --> 00:08:04,670
And if I type Get Ticket Available.  Now we're still going to see 4 okay.

106
00:08:04,680 --> 00:08:10,290
We don't see the Increased Value, which is it should have been 5 in this case

107
00:08:18,440 --> 00:08:21,730
now since we are running on a Single Client.

108
00:08:22,730 --> 00:08:25,900
And we are able to perform operations sequentially. Now let's assume

109
00:08:25,890 --> 00:08:32,300
what happens if there are Multiple Clients  or Multiple Applications are trying to, you know, get

110
00:08:32,300 --> 00:08:35,300
the Values or Perform Operations on the same keys.

111
00:08:35,440 --> 00:08:35,730
Okay.

112
00:08:35,750 --> 00:08:43,640
What is going to happen and how you avoid or how you ensure that transaction is run, you know with

113
00:08:43,680 --> 00:08:45,720
in, in a Consistent Mode.

114
00:08:46,620 --> 00:08:54,490
Okay, so in that case basically what we need is, something which is called a Watch Statement.

115
00:08:54,660 --> 00:08:55,020
Okay.

116
00:08:55,030 --> 00:08:55,690
In Redis.

117
00:09:04,820 --> 00:09:11,470
So you see, right now we have ticket available value of 4 and I am going to start a new transaction.

118
00:09:11,480 --> 00:09:19,650
But before I start a new transaction, I'm going to make sure I run Watch Ticket Available.

119
00:09:19,670 --> 00:09:25,990
So basically what it is going to do is,  it is going to ensure that, if the value of the good available

120
00:09:26,000 --> 00:09:31,110
is modified by any other client, this command is

121
00:09:31,110 --> 00:09:33,790
Basically this transaction is going to return us Null.

122
00:09:34,010 --> 00:09:34,600
OK.

123
00:09:34,620 --> 00:09:40,350
Which is Redis way of saying that someone else or some other client also modified the value of Ticket Available.

124
00:09:40,370 --> 00:09:43,210
So it cannot modify the value of Ticket Available.

125
00:09:54,920 --> 00:10:00,950
Okay now let's say, I wanted to decrease the value of Ticket Available, by issuing this Command.

126
00:10:01,490 --> 00:10:09,210
However let's say we had another Client. Okay which already issued a different Command on this

127
00:10:09,210 --> 00:10:14,910
Ticket Available keys. So to simulate that scenario.  I'm going to open a New Session and then we'll

128
00:10:14,930 --> 00:10:24,410
be logged into this Redis and then we are going to issue some commands or let's say probably I am

129
00:10:24,410 --> 00:10:32,060
going to increase the value of ticket available or let's say decrease the value of the ticket available.

130
00:10:33,070 --> 00:10:33,660
Okay.

131
00:10:33,680 --> 00:10:36,830
And I'm going to run this 3 time.

132
00:10:36,830 --> 00:10:41,360
Now if you see, we have value of ticket available, modified by some other client.

133
00:10:41,660 --> 00:10:48,900
And in this case, this client 1 is unaware that, you know, value of ticket available has been changed

134
00:10:48,990 --> 00:10:51,620
and it still goes on and, you know, enrolls.

135
00:10:51,690 --> 00:10:54,990
And which results in reducing the Ticket Available Value.

136
00:10:58,340 --> 00:10:58,820
okay.

137
00:11:03,140 --> 00:11:04,850
Oh I guess we made a mistake here.

138
00:11:04,850 --> 00:11:09,680
So after Watch we did not start the transaction with Multi

139
00:11:09,680 --> 00:11:10,190
Command.

140
00:11:10,250 --> 00:11:10,580
Okay.

141
00:11:10,580 --> 00:11:16,220
So that's why this is executing individually. It is not executing as a transaction.

142
00:11:16,220 --> 00:11:16,490
Okay.

143
00:11:16,520 --> 00:11:18,220
So yeah.

144
00:11:18,230 --> 00:11:24,240
So in this case, let me first run a Set

145
00:11:24,240 --> 00:11:32,710
Command and Set the value of ticket available to Let's say 4. Okay

146
00:11:32,720 --> 00:11:35,300
And then I'm going to run  Watch Ticket Available and

147
00:11:44,720 --> 00:11:48,840
so after the Watch Command, you just need to make sure you are running it as a transaction.

148
00:11:48,920 --> 00:11:49,240
Okay.

149
00:11:49,260 --> 00:11:52,040
So  the way to do that is, you use Multi

150
00:11:56,190 --> 00:12:02,290
and then I'm going to run these commands. So get ticket available and

151
00:12:05,900 --> 00:12:08,590
decreased or Decrement Ticket Available.

152
00:12:08,630 --> 00:12:14,390
But before we do that again, we are going to simulate that there is another client, which is changing

153
00:12:14,390 --> 00:12:22,500
the value and now we are going to come back to client 1, which is unaware that someone else has also

154
00:12:22,500 --> 00:12:24,090
changed the value.

155
00:12:24,180 --> 00:12:30,450
However since we were running it with the watch Command on ticket available. So if any other client has

156
00:12:30,450 --> 00:12:34,290
changed the value of  ticket available. This whole transaction is going to get failed.

157
00:12:34,500 --> 00:12:35,660
Okay.

158
00:12:35,730 --> 00:12:43,310
So now, here if I write Execute Statement and if I hit enter. It is going to reply us with a Nil.

159
00:12:43,530 --> 00:12:43,860
Okay.

160
00:12:43,890 --> 00:12:52,820
So what it means is, that someone else has changed the value of this key and that is Redis way of saying

161
00:12:52,820 --> 00:13:01,910
that, that's why this command has failed.

162
00:13:02,050 --> 00:13:02,810
Now let's say

163
00:13:06,420 --> 00:13:11,160
if no other client has changed the value of ticket available. In that case what would have happened.

164
00:13:12,450 --> 00:13:13,240
So let's see that.

165
00:13:13,800 --> 00:13:16,850
So I set the  Ticket  Value to 4, which is the initial value.

166
00:13:16,860 --> 00:13:18,510
And then we are going to write

167
00:13:24,780 --> 00:13:28,880
okay again we want to introduce Watch on this key

168
00:13:31,770 --> 00:13:34,610
and then I'm going to run a transaction by hitting Multi.

169
00:13:34,680 --> 00:13:43,830
And then we are going to see the value of  ticket available and then we are going to decrement the

170
00:13:43,830 --> 00:13:45,690
value of ticket available

171
00:13:48,850 --> 00:13:52,000
and then I'm going to execute this. Hit enter.

172
00:13:52,480 --> 00:13:57,340
And now you see the transaction has been successfully executed because there was no interference in

173
00:13:57,340 --> 00:14:02,440
this transaction by any other client. Okay

174
00:14:02,450 --> 00:14:05,480
So these are the ways, how you handle transaction in Redis.
