Updated February 17, 2023
Introduction to Redis Transactions
Redis transactions enable us to execute multiple commands in a single step. It will be centered on the multiple commands watch, multi, exec, and discard that are used in transactions to make important guarantees. When using redis, all transaction commands are serialized and executed sequentially. Requests sent to other clients are not served in the middle of a redis transaction, which is a defined set of commands.
Key Takeaways
- At the time of using transaction in redis, we are using the multi command for starting the transaction, also we are using the exec command for ending the transaction and executing all the commands.
- We are using the discard command to abort the transaction. Exec, multi, and abort are important commands.
What are Redis Transactions?
We are using exec commands in transactions to trigger the execution of all commands. If the client loses the connection to the server in the transaction context before calling the exec command, it will not perform any of the operations. If we use the exec command, then all operations are carried out. When using append-only files, Redis will ensure that the syscall for write transactions on disc uses a single write.
If the redis server crashes or is killed by the administrator, a partial number of operations can be registered. Redis will detect the same conditions when restarting and will exit with an error. When using the redis check tool, we can append the file that was removing the partial transaction, which will aid in restarting the server.
Using Redis Transactions
To use the redis transaction we are following the below steps. In the below step, we are starting the redis server.
1. For using redis transaction first we are starting the redis server using the below commands.
Code:
# redis-server
Output:
2. Now we are logging to the redis server by using the redis cli command. We are login into the localhost as follows.
Code:
# redis-cli
Output:
3. Now in this step we are executing the multi-command. Basically, the redis transaction is entered by using the multi command. We can see that multi command will give the ok response. By using multi command new transaction is started.
Code:
MULTI
Output:
4. After executing multi commands now we are executing multiple redis commands. In the below example, we can see that instead of executing all the commands redis will queue all the commands as follows.
Code:
HMSET tran tran_field "redis transactions"
HGET tran tran_field
INCR que
Output:
5. After defining the redis transactions, we can now call the exec command to execute all of the commands that were queued in transactions, as shown below. After executing the exec command, we can begin a new transaction by executing the multi command.
Code:
EXEC
Output:
How to Setup Transactions in Redis?
Basically, we are using multi commands to set up the transaction. The multi-command teaches redis to begin the transaction block. Subsequent commands are queued until we run the exec command, which was used to execute all of the commands from the transaction, as well as finish the transaction that was started with the multi command.
In the below example, we are starting the transaction by using the multi command, then the second command is holding a key with string as a value of 10. Then in the third statement, we are not incrementing the value fourth we are incrementing the value by 50, and last we are executing the exec block as follows.
Code:
multi
set set_tran 10
incr set_tran 30
incrby set_tran 50
get set_tran
exec
Output:
Commands included in the transaction are executed sequentially in the order in which they will be queued. Because Redis transactions are atomic, each command in the transaction block is processed. Even when executing redis transactions, an error is returned. In the below example, we can see that in the first example we started the transaction by using multi then we are using set command then we are passing extra parameter in the incr command, then after executing the exec command we can see that it will give the error.
Code:
Multi
set set_tran 10
incr set_tran 30
EXEC
Output:
Redis Transactions Commands and Database
In a redis transaction involving MULTI and EXEC, the client will have the option of executing multiple commands. Basically, the redis transaction database is not like a relational database transaction, which executes partially before being rolled back and committed. We call multi to perform the transaction into Redis, which is followed by the sequence of commands that we intend to execute.
The below example shows redis transaction commands as follows. We are using the multi and exec commands as follows:
Code:
multi>
SET tran_commands 10
GET tran_commands
exec
Output:
Below are the commands of redis transaction:
- Discard – This command will discard all the commands which were issued after the MULTI.
- EXEC – This command is used to execute all the commands which were issued after the execution of MULTI.
- MULTI – This command is used to start the transaction block of redis. By using this command, we are starting a new transaction.
- UNWATCH – By using this command we forget about all watched keys.
- WATCH key – This defines to watch the given keys for determining the execution of block names EXEC and MULTI.
In the below example, we are defining the example of redis transaction commands as follows.
Code:
MULTI
HMSET tran_commands tran_field "val 1"
SET tran_command cmd
exec
Output:
Examples of Redis Transactions
Given below are the examples mentioned:
Example #1
In the below example, we are defining two commands in the transaction.
Code:
multi
SET tran_example 10
GET tran_example
EXEC
Output:
Example #2
In the below example, we are using discard to abort the transaction. We are starting transactions using multi and discarding the same using the discard command.
Code:
multi
incr example
set exp exp
DISCARD
Output:
Example #3
In the below example, we are defining four commands in transactions.
Code:
multi
SET tran_example 50
GET tran_example
SET tran_example2 60
GET tran_example2
exec
Output:
Conclusion
If the redis server crashes or is killed by the administrator, a partial number of operations can be registered. Redis transactions enable us to run multiple commands in a single step. It will be centered on the multiple commands watch, multi, exec, and discard that are used in transactions to make important guarantees.
Recommended Articles
This is a guide to Redis Transactions. Here we discuss the introduction, how to set up transactions in redis? commands and database, examples. You may also have a look at the following articles to learn more –