So in this session we are going to learn about Redis Transactions
and basically Redis Transaction is supported by these set of 5 commands.
OK.
which we are going to use. Now to give you an example of Inconsistent Transaction. So let's say we have,
you know, User 1 which is going to see the total number of available courses and then it is going to
enroll for the course.
So let's have an user 1 queries Redis, to see the total number of courses available along with the price.
Redis replies that there are total 3 courses available and the price is 500$ and you
know, when user 1 tries to enroll for the course. Just before that there is another user User 2. It enrolls for
all the three courses
Okay.
Successfully and now when User 1 tries to enroll.
Redis
And if Redis allows it to enroll. It is going to be inconsistent state, because we have only 3
courses available to enroll.
And basically we have already allowed 3, User2 for three enrollments and if we allow User
1 also for the enrollment, then basically we are going to have you know, more number of enrollments
than the available seats.
So if you are, you know, by Atomic Transaction, what it means is that basically when User 1 is issuing
this transaction. User 1 is going to issue this transaction as a Transaction.
Okay
So if, you know, if the value of the Ticket is changing or let's say the availability of the Ticket is
changing. That basically the transaction is going to get failed.
Okay.
So in transaction basically all the commands execute successfully or the transaction fails.
Okay.
That is the property of a transaction. So by that what I mean is, Transaction basically is going to guarantee
you that all operation which, you are performing is going to be executed sequentially and also either
all of them are going to be successful or none of them are going to be successful.
Okay.
So it is going to make sure that you don't run into an Inconsistent State.
Okay.
Now here let's say we have a Key, which is called Ticket Available and now let's say we just want to see
how many tickets available and we want to decrement the value of ticket and that's it.
Okay.
And both of these basically should be running as a Transaction. It should not be running as Individual
Command.
Okay.
So by Individual Command, what I mean is you know, running Get Ticket Available.
Okay.
This is running as a Single Command and then you have the option to Increment Ticket Available.
Okay.
and then let's say i wrote a wrong Command.
It doesn't impact previous commands.
Okay.
So these, all of these three commands which we ran, it is executed one by one.
Now we want to do, is we want to run set of commands as a Transaction. Now to start a transaction
you type multi, okay.
That is how you indicate Redis that basically now you are going to execute a transaction.
You don't want to execute commands line by line.
Okay.
So now if I type ticket, Get Ticket Available you see the response which I have get is Queued, which means
you know this command is not executed. It has been Queued.
And if I do
Decrement Ticket Available and then we want to see the value again
Okay now we have issued 3 commands but none of them has been executed by Redis.
So it is only going to be executed when I type
exec.
Okay.
So basically that is a way of telling that Execute this Transaction.
So once I hit enter. Now you see all the three commands has been executed together.
OK.
And this this
No transaction basically has been executed.
And we got the Consistent Reply.
Now let's say what happens, if there is an Error in one of the commands
so once you write Execute. The previous transaction is already ended here.
And if we want to start a New Transaction. We need to write Multi again.
So I write Multi. Hit enter.
and now I'm going to write Get Ticket Available and then we want to decrement the value.
However, this time, let's say,you know, I don't write Decrement Ticket Available.
I probably wrote let's say, wrong command and once I hit enter. It is going to give me an error.
OK.
But since we are running it as a Transaction. If I run the next command. OK let's say if I run
Decrement Ticket Available again
and then I run Get Ticket Available.
OK.
So what do you think is going to happen, when I run Execute in this
OK.
So if you're thinking that this is going to get successful. It is wrong, because you see one of our command
in this transaction is failed.
So basically this whole transaction is going to be treated as a Failed Transaction.
Okay.
So I'm going to hit enter and you see that Execution is Aborted. Transaction is Discarded because of
previous errors. So when you have error in transaction. The whole transaction is aborted.
It is not that one line of transaction is going to run and another line of transaction is not going to run.
and the same has been explained here in the example as well.
So if you just want to take a note, you can take a note from here.
I'm going to give the link of this Blog in the Resource Section.
Now we have something called Discarding a Transaction. Now let's say
let me clear the screen first and let's say, in the middle of the transaction
we realize that we don't want to continue with the transaction.
We just want to discard this transaction.
Then you can use Discard
Command.
So let's say rather than, you know, decrementing the value.
By mistake we incremented the value of ticket available.
And now I do not want this transaction to run.
So if I type Execute, basically it is going to execute this transaction and the value of ticket available
is going to be increased. Whereas actually we wanted it to be decreased.
So in this case I can issue Discard Command and hit enter.
And now we are out of this transaction.
Okay.
So this Multi is now closed. This Transaction is now closed.
And if I type Get Ticket Available. Now we're still going to see 4 okay.
We don't see the Increased Value, which is it should have been 5 in this case
now since we are running on a Single Client.
And we are able to perform operations sequentially. Now let's assume
what happens if there are Multiple Clients or Multiple Applications are trying to, you know, get
the Values or Perform Operations on the same keys.
Okay.
What is going to happen and how you avoid or how you ensure that transaction is run, you know with
in, in a Consistent Mode.
Okay, so in that case basically what we need is, something which is called a Watch Statement.
Okay.
In Redis.
So you see, right now we have ticket available value of 4 and I am going to start a new transaction.
But before I start a new transaction, I'm going to make sure I run Watch Ticket Available.
So basically what it is going to do is, it is going to ensure that, if the value of the good available
is modified by any other client, this command is
Basically this transaction is going to return us Null.
OK.
Which is Redis way of saying that someone else or some other client also modified the value of Ticket Available.
So it cannot modify the value of Ticket Available.
Okay now let's say, I wanted to decrease the value of Ticket Available, by issuing this Command.
However let's say we had another Client. Okay which already issued a different Command on this
Ticket Available keys. So to simulate that scenario. I'm going to open a New Session and then we'll
be logged into this Redis and then we are going to issue some commands or let's say probably I am
going to increase the value of ticket available or let's say decrease the value of the ticket available.
Okay.
And I'm going to run this 3 time.
Now if you see, we have value of ticket available, modified by some other client.
And in this case, this client 1 is unaware that, you know, value of ticket available has been changed
and it still goes on and, you know, enrolls.
And which results in reducing the Ticket Available Value.
okay.
Oh I guess we made a mistake here.
So after Watch we did not start the transaction with Multi
Command.
Okay.
So that's why this is executing individually. It is not executing as a transaction.
Okay.
So yeah.
So in this case, let me first run a Set
Command and Set the value of ticket available to Let's say 4. Okay
And then I'm going to run Watch Ticket Available and
so after the Watch Command, you just need to make sure you are running it as a transaction.
Okay.
So the way to do that is, you use Multi
and then I'm going to run these commands. So get ticket available and
decreased or Decrement Ticket Available.
But before we do that again, we are going to simulate that there is another client, which is changing
the value and now we are going to come back to client 1, which is unaware that someone else has also
changed the value.
However since we were running it with the watch Command on ticket available. So if any other client has
changed the value of ticket available. This whole transaction is going to get failed.
Okay.
So now, here if I write Execute Statement and if I hit enter. It is going to reply us with a Nil.
Okay.
So what it means is, that someone else has changed the value of this key and that is Redis way of saying
that, that's why this command has failed.
Now let's say
if no other client has changed the value of ticket available. In that case what would have happened.
So let's see that.
So I set the Ticket Value to 4, which is the initial value.
And then we are going to write
okay again we want to introduce Watch on this key
and then I'm going to run a transaction by hitting Multi.
And then we are going to see the value of ticket available and then we are going to decrement the
value of ticket available
and then I'm going to execute this. Hit enter.
And now you see the transaction has been successfully executed because there was no interference in
this transaction by any other client. Okay
So these are the ways, how you handle transaction in Redis.