Redis Transactions

RedisRedisBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL redis(("Redis")) -.-> redis/RedisGroup(["Redis"]) redis/RedisGroup -.-> redis/access_cli("Connect Using CLI") redis/RedisGroup -.-> redis/store_string("Set String Value") redis/RedisGroup -.-> redis/fetch_string("Get String Value") redis/RedisGroup -.-> redis/increment_int("Increase Integer Value") subgraph Lab Skills redis/access_cli -.-> lab-552106{{"Redis Transactions"}} redis/store_string -.-> lab-552106{{"Redis Transactions"}} redis/fetch_string -.-> lab-552106{{"Redis Transactions"}} redis/increment_int -.-> lab-552106{{"Redis Transactions"}} end

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.

Remember to exit the redis-cli environment to ensure the command is logged:

exit

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.

Reconnect to the Redis server:

redis-cli

Since we have an existing transaction, we need to re-enter transaction mode:

MULTI

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 the SET mykey "myvalue" command.
  • 2) "myvalue": Result of the GET mykey command.
  • 3) OK: Result of the SET anotherkey "anothervalue" command.
  • 4) (integer) 1: Result of the INCR mycounter command.

All commands within the transaction were executed atomically.

Remember to exit the redis-cli environment to ensure the command is logged:

exit

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.

Reconnect to the Redis server:

redis-cli

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.

Remember to exit the redis-cli environment to ensure the command is logged:

exit

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:

redis-cli
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.