Redis Transactions: Basic and advance

Redis transaction is used to facilitates customers to execute team of instructions in a single step.

There are two properties of execution:

All commands in a transaction are sequentially done as a single isolated operation. You can’t issue a request by any other client served in the middle of the execution of a Redis transaction. Redis transaction is additionally atomic. Atomic ability either all of the commands or none are processed.

Sample

In Redis, transaction is initiated via the use of “MULTI” command and then you need to omit a listing of instructions that need to be completed in the transaction, after which the whole transaction is achieved with the aid of “EXEC” command.

Example

Let’s take an instance to see how Redis transaction can be initiated and executed.

Example

redis 127.0.0.1:6379> MULTI  
OK  
redis 127.0.0.1:6379> EXEC  
(empty list or set)  
redis 127.0.0.1:6379> MULTI  
OK  
redis 127.0.0.1:6379> SET javatpoint redis  
QUEUED  
redis 127.0.0.1:6379> GET javatpoint  
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 is a listing of some simple instructions of Redis transaction.

Index Command Description
1 DISCARD It is used to discard all commands issued after MULTI
2 EXEC It is used to execute all commands issued after MULTI
3 MULTI It is used to mark the start of a transaction block
4 UNWATCH It is used to forget about all watched keys
5 WATCH key [key …] It is used to watche the given keys to determine the execution of the MULTI/EXEC block

Next TopicRedis Scripting