Redis - Transactions


Advertisements

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 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 Commands

Following table shows some basic commands related to Redis transactions.

Sr.No Command & Description
1 DISCARD

Discards all commands issued after MULTI

2 EXEC

Executes all commands issued after MULTI

3 MULTI

Marks the start of a transaction block

4 UNWATCH

Forgets about all watched keys

5 WATCH key [key ...]

Watches the given keys to determine the execution of the MULTI/EXEC block

Advertisements