Transaction¶
Redis transaction begin with the MULTI command and end with EXEC or DISCARD command.
After the MULTI command, every non transactional command will be pushed on the queue.
But if any illegal command is pushed, an error will be reported. Then the transaction cannot be executed due to the error.
Warning
Transactions in Redis do not follow the all or nothing principle.
It can ensures all commands in the queue will be executed in sequence and that there will never be any command to jump in line.
So it has no ROLLBACK operation.
Commands¶
MULTI-
Marks the start of a transaction block.
After MULTI command, commands for each non transactional operation are pushed on the queue.
EXEC/DISCARD- Marks the end of a transaction block And EXECUTE or DISCARD the commands in the queue.
WATCH-
WATCH key [key ...]Marks the given keys to be watched.
UNWATCH-
Unmarks all the previously watched keys for a transaction.
If you call EXEC or DISCARD, there’s no need to manually call UNWATCH.
Simple example¶
| Execute transaction | |
|---|---|
| Discard transaction | |
|---|---|
| Do not atomic | |
|---|---|
| An illegal command | |
|---|---|
Watch and Unwatch¶
In the Redis transaction, the Lock operation is implemented through the WATCH command.
Before execute the MULTI command, you can use the WATCH key [key ...] to watch all keys that will be used in the following transaction.
And if other client modify any keys you watched before you execute EXEC command, your transaction will be not executed and return (nil).
UNWATCH command will be executed after EXEC or DISCARD command automatically. You can also execute it manually before WATCH command.
It was not watch the keys, so the result was unexpected.
| Client 1 | Client 2 |
|---|---|
> MULTI |
|
| OK | |
(TX)> MGET a b |
|
| QUEUED | |
(TX)> DECRBY a 100 |
|
| QUEUED | |
(TX)> INCRBY b 100 |
|
| QUEUED | |
(TX)> MGET a b |
|
| QUEUED | |
> GET a |
|
| "100" | |
> DECRBY a 50 |
|
| (integer) 50 | |
(TX)> EXEC |
|
| 1) 1) "50" | |
| -- 2) "5" | |
| 2) (integer) -50 | |
| 3) (integer) 105 | |
| 4) 1) "-50" | |
| -- 2) "105" |
| Client 1 | Client 2 |
|---|---|
> WATCH a b |
|
| OK | |
> MULTI |
|
| OK | |
(TX)> MGET a b |
|
| QUEUED | |
(TX)> DECRBY a 100 |
|
| QUEUED | |
(TX)> INCRBY b 100 |
|
| QUEUED | |
(TX)> MGET a b |
|
| QUEUED | |
> GET a |
|
| "100" | |
> DECRBY a 50 |
|
| (integer) 50 | |
(TX)> EXEC |
|
| (nil) | |
> MGET a b |
|
| 1) "50" | |
| 2) "5" |
