Xin Xing talks about Redis transactions and Xin Xing talks about Redis transactions
Redis also provides support for transactions. In Redis, we commonly use four commands: multi, exec, discard, and watch. The multi command is used to start a transaction. All the commands after the statement are considered as operations within the transaction, while exec is to commit a transaction, and discard is to roll back a transaction.
The following is a detailed introduction to some commands:
Multi ---- mark the start of a transaction. Subsequent commands are stored in the command queue until exec is executed. Its return value is always OK.
Exec ---- execute all the commands in the command queue in a transaction, and restore the current connection status to normal, that is, the so-called non-transaction status. If the watch command is executed in a transaction, only when the keys monitored by watch are not modified can the exec command execute all the commands in the transaction queue, otherwise, exec will discard all commands in the current transaction. It returns the results of each command in the transaction atomically. If watch is used in the transaction, once the transaction is abandoned, exec returns the response of null-multi-bulk.
Discard -- rolls back all commands in the transaction queue and restores the current connection status to normal, which is not a transaction status. If the watch command is used, it will unwatch all keys. Always Return OK.
Watch --- Before executing the multi command, you can specify the keys to be monitored. If the monitored keys are modified before exec is executed, then exec will discard all the commands in the transaction queue. It always returns OK.
Unwatch ---- cancel the specified monitored keys in the current transaction. If the exec or discard command is executed, you do not need to manually execute this command. At this time, all monitored keys will be automatically canceled. It always returns OK.
In Redis transactions, the watch command can be used to provide the CAS function. The so-called CAS is the Check And Set function. Assume that we use the watch command to monitor multiple keys before the transaction is executed. If the value of any key changes after we add the watch command, the transaction executed by the exec command will be abandoned, at the same time, a Null multi-bulk response is returned to notify the caller that the transaction failed.
First, let's look at a piece of code:
Val = get key1
Val = val + 1
Set key1 val
If multiple clients execute the above Code at the same time, there may be a common problem in multithreading-competing mode contention. For example, both clients read the original value of key1. If the value is 18, after executing the above Code, it is possible that both clients read 18 when reading data, the result of the final program is 19, instead of 20. we can avoid this problem by using the following command:
Watch key1
Val = get key1
Val = val + 1
Multi
Set key1 val
Exec
In the above Code, the watch key1 command is in front, that is, the monitoring rebuilding is performed when the value of key1 is obtained. Note that we enclose the set in the transaction here, if the value has been modified, the Operation will fail because of the existence of watch. Therefore, we can determine whether val has been modified by checking the return value, then we can choose whether to re-execute the command.