Redis transaction
Redis transaction execute multiple commands at once, and with two important guarantees:
- A transaction is a separate quarantine action: All commands are serialized transaction executed sequentially. During the execution of the transaction will not be sent by the client to request a command interrupted.
- A transaction is an atomic operation: commands in the transaction are either all executed or all not executed.
A transaction from start to execution through the following three stages:
- Begin the transaction.
- Command into the team.
- The enforcement branch.
Examples
The following is an example of a transaction, it first started a business withMULTI, then multiple commands into teams to the transaction, triggered by last EXECcommand transaction, together with all the commands in a transaction:
redis 127.0.0.1:6379> MULTI OK redis 127.0.0.1:6379> SET book-name "Mastering C++ in 21 days" QUEUED redis 127.0.0.1:6379> GET book-name QUEUED redis 127.0.0.1:6379> SADD tag "C++" "Programming" "Mastering Series" QUEUED redis 127.0.0.1:6379> SMEMBERS tag QUEUED redis 127.0.0.1:6379> EXEC 1) OK 2) "Mastering C++ in 21 days" 3) (integer) 3 4) 1) "Mastering Series" 2) "C++" 3) "Programming"
Redis transaction command
The following table lists the commands for redis matters:
No. | Command and description |
---|---|
1 | DISCARD Cancel the transaction, give up all the commands within a transaction block. |
2 | EXEC Run all transactions within the block. |
3 | MULTI It marks the start of a transaction block. |
4 | UNWATCH Cancel WATCH command to monitor all of the key. |
5 | WATCH key [key ...] Monitor one (or more) key, if this (or these) key was altered other commands before the transaction is executed, the transaction will be interrupted. |