Redis transactions let a set of commands execute in a single step. There are two properties in the transaction, which are described below:
- All commands in a transaction are executed sequentially as a single, independent operation. This is not possible, and requests made to another client are held during the execution of the Redis transaction.
- Redis transactions are also atomic. An atom means that either all commands are executed, or none are processed.
Example
The Redis transaction is started by the instruction MULTI, and then the transaction is passed, and the entire transaction executes after executing the command, executing a list of commands.
Redis 127.0.0.1:6379> MULTI
Ok
List of commands here
Redis 127.0.0.1:6379> EXEC
Example
Here's an example of how Redis's bookstore is started and executed.
Redis 127.0.0.1:6379> MULTI
Ok
Redis 127.0.0.1:6379> Set Tutorial Redis
QUEUED
Redis 127.0.0.1:6379> GET Tutorial
QUEUED
Redis 127.0.0.1:6379> INCR Visitors
QUEUED
Redis 127.0.0.1:6379> EXEC
1) OK
2) "Redis"
3) (integer) 1
Redis Transaction Instructions
As shown in the following table, there are some basic commands related to Redis transactions:
S.N. |
Commands & Instructions |
1 |
DISCARD After issuing the command MULTI discard all |
2 |
Exec MULTI All commands are issued after the execution line |
3 |
MULTI Mark the beginning of a transaction block |
4 |
Unwatch Cancel all the corresponding focus keys |
5 |
WATCH key [Key ...] Focus on the given item to determine the execution of the multi/exec block |
Redis QuickStart-Redis transactions (13)