Introduction
In this lab, you will explore Redis transactions. Redis transactions allow you to execute a group of commands as a single, atomic operation, ensuring data consistency. You'll learn how to start a transaction with the MULTI command, queue commands, and then either execute or discard the transaction. You'll practice setting keys, getting values, and incrementing counters within transactions.
Starting a Redis Transaction
In this step, you'll learn how to initiate a transaction in Redis using the MULTI command. Redis transactions ensure that a series of commands are executed as a single, atomic unit. This means either all commands succeed, or none do, guaranteeing data integrity.
First, connect to the Redis server using the redis-cli command in your terminal:
redis-cli
Now that you're in the redis-cli environment, you can begin a transaction. Type the following command and press Enter:
MULTI
You should see the following output:
OK
This confirms that Redis has entered transaction mode. Any subsequent commands you enter will be queued and executed together when you use the EXEC command.
Let's queue our first command. We'll set a key named mykey with the value myvalue. Type the following command and press Enter:
SET mykey "myvalue"
The output should be:
QUEUED
This indicates that the SET command has been successfully added to the transaction queue. It will not be executed until we explicitly tell Redis to do so.
Keep the redis-cli environment open for the next step.
Queueing More Commands and Executing the Transaction
In this step, you'll add more commands to the transaction queue and then execute the entire transaction using the EXEC command.
Now, let's queue a command to retrieve the value of mykey:
GET mykey
You should see:
QUEUED
Next, let's add another command to set a different key, anotherkey, with the value anothervalue:
SET anotherkey "anothervalue"
The output should be:
QUEUED
Finally, let's queue an INCR command to increment a counter named mycounter. If mycounter doesn't exist, Redis will create it and initialize it to 0 before incrementing:
INCR mycounter
You should see:
QUEUED
You've now queued several commands within the transaction. To execute them all at once, use the EXEC command:
EXEC
The output should look similar to this:
1) OK
2) "myvalue"
3) OK
4) (integer) 1
Let's break down the output:
1) OK: Result of theSET mykey "myvalue"command.2) "myvalue": Result of theGET mykeycommand.3) OK: Result of theSET anotherkey "anothervalue"command.4) (integer) 1: Result of theINCR mycountercommand.
All commands within the transaction were executed atomically.
Keep the redis-cli environment open for the next step.
Verifying the Transaction Execution
In this step, you'll verify that the commands in the previous transaction were executed correctly by retrieving the values of the keys you set.
To check the values, use the GET command for each key:
GET mykey
GET anotherkey
GET mycounter
You should see the following output:
"myvalue"
"anothervalue"
"1"
This confirms that the transaction was executed successfully, and the values of the keys were updated as expected.
Keep the redis-cli environment open for the next step.
Cancelling a Transaction with DISCARD
In this step, you'll learn how to cancel a Redis transaction using the DISCARD command. This is useful if you decide you don't want to execute the queued commands.
First, connect to the Redis server and start a new transaction:
MULTI
Now, let's queue some commands:
SET mykey "newvalue"
INCR mycounter
You should see the QUEUED response for each command.
Now, instead of executing the transaction, let's cancel it. Use the DISCARD command:
DISCARD
You should see:
OK
This confirms that the transaction has been cancelled, and all queued commands have been discarded. To verify, you can check the value of mykey and mycounter. They should not have been updated.
Remember to exit the redis-cli environment to ensure the command is logged:
exit
Summary
In this lab, you have learned how to use Redis transactions to execute multiple commands atomically. You practiced starting transactions with MULTI, queueing commands, executing transactions with EXEC, and cancelling transactions with DISCARD. This knowledge is crucial for maintaining data consistency in Redis when performing complex operations.


