Lecture
Redis also supports transactions, which allow a group of commands to be
executed in a single step, and other requests' commands cannot
interject into it; at the same time, it guarantees the consistency and
sequential execution of the set of commands, and in case of any problems
it also allows these changes to be rolled back
The following main commands are used to implement transactions in Redis:
MULTI – start recording commands for a transaction.
EXEC – execute the recorded commands.
DISCARD – delete all recorded commands.
WATCH – a command that guarantees a «check-and-set»
(CAS) behavior - the transaction is executed only if
other clients have not modified the value of the variable. Otherwise EXEC will not execute the recorded commands
127.0.0.1:6379> multi
set count 20
127.0.0.1:6379> incr count
QUEUED
127.0.0.1:6379> incr count
> decr count
(integer) 21
(integer) 22
(integer) 21
127.0.0.1:6379>
Now let's try to start a transaction and then abort it.
6379> multi
6379> set foo 0
:6379> incr foo
:6379> discard
127.0.0.1:6379> _
Comments