Redis – Transactions

  • Post author:
  • Post category:Redis
  • Post comments:0 Comments

Redis transactions allow the execution of a group of commands in a single step. Following are the two properties of Transactions.

  • All commands in a transaction are sequentially executed as a single isolated operation. It is not possible that a request issued by another client is served in the middle of the execution of a Redis transaction.
  • Redis transaction is also atomic. Atomic means either all of the commands or none are processed.

Sample

Redis transaction is initiated by command MULTI and then you need to pass a list of commands that should be executed in the transaction, after which the entire transaction is executed by EXEC command.

redis 127.0.0.1:6379> MULTI 
OK 
List of commands here 
redis 127.0.0.1:6379> EXEC

Example

Following example explains how Redis transaction can be initiated and executed.

redis 127.0.0.1:6379> MULTI 
OK 
redis 127.0.0.1:6379> SET Adglob redis 
QUEUED 
redis 127.0.0.1:6379> GET Adglob 
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 Commands

Following table shows some basic commands related to Redis transactions.

Sr.NoCommand & Description
1DISCARDhttps://adglob.in/blog/redis-transactions-discard-command/
Discards all commands issued after MULTI
2EXEChttps://adglob.in/blog/redis-transactions-exec-command/
Executes all commands issued after MULTI
3MULTIhttps://adglob.in/blog/redis-transactions-multi-command/
Marks the start of a transaction block
4UNWATCHhttps://adglob.in/blog/redis-transactions-unwatch-command/
Forgets about all watched keys
5WATCH key https://adglob.in/blog/redis-transactions-watch-command/
Watches the given keys to determine the execution of the MULTI/EXEC block

Leave a Reply