If Redis You need to execute a set of commands , To keep the data consistent , Continuity , You need to use transactions （Transaction）, Here are some simple examples , sketch Redis Business related content , For learning and sharing only , If there are shortcomings , Also please correct me .
Redis Business involves command
- DISCARD： Cancel the business , And DISCARD Is with the MULTI Pairs appear , It can't be used alone .
- MULTI： Mark the beginning of a transaction block .
- EXEC： Execute commands in all transactions in sequence .
- WATCH： Monitor one or more key.
- UNWATCH： Cancel surveillance .
Transaction basic operations
Basic business MULTI Start , With EXEC end , In the middle is a group of Redis command , As shown below ：
1 127.0.0.1:6379> MULTI 2 OK 3 127.0.0.1:6379> SET NAME HSIANG 4 QUEUED 5 127.0.0.1:6379> SET AGE 20 6 QUEUED 7 127.0.0.1:6379> SET SEX MALE 8 QUEUED 9 127.0.0.1:6379> SET ADDR SHENZHEN 10 QUEUED 11 127.0.0.1:6379> EXEC 12 1) OK 13 2) OK 14 3) OK 15 4) OK 16 127.0.0.1:6379>
Cancel the business
Cancel the business , Then restore the data to the state before the transaction , With MULTI Start , With DISCARD ending , As shown below ：
1 127.0.0.1:6379> GET AGE 2 "20" 3 127.0.0.1:6379> MULTI 4 OK 5 127.0.0.1:6379> INCR AGE 6 QUEUED 7 127.0.0.1:6379> INCR AGE 8 QUEUED 9 127.0.0.1:6379> INCR AGE 10 QUEUED 11 127.0.0.1:6379> DISCARD 12 OK 13 127.0.0.1:6379> GET AGE 14 "20"
Transaction partial execution
Redis Transaction , If you want to execute a set of commands , There are no grammatical mistakes , But there is a data type error , It's going on EXEC When , Other commands are executed successfully , Error data execution failed , That's partial success . As shown below ：
1 127.0.0.1:6379> MULTI 2 OK 3 127.0.0.1:6379> INCR AGE 4 QUEUED 5 127.0.0.1:6379> INCR ADDR 6 QUEUED 7 127.0.0.1:6379> INCR AGE 8 QUEUED 9 127.0.0.1:6379> EXEC 10 1) (integer) 21 11 2) (error) ERR value is not an integer or out of range 12 3) (integer) 22 13 127.0.0.1:6379> GET AGE 14 "22"
If it's to be done Redis command , There are grammatical errors , It's going on EXEC When you roll back , As shown below ：
1 127.0.0.1:6379> MULTI 2 OK 3 127.0.0.1:6379> INCR AGE 4 QUEUED 5 127.0.0.1:6379> SET ADDR 6 (error) ERR wrong number of arguments for 'set' command 7 127.0.0.1:6379> INCR AGE 8 QUEUED 9 127.0.0.1:6379> EXEC 10 (error) EXECABORT Transaction discarded because of previous errors. 11 127.0.0.1:6379> GET AGE 12 "22"
Lock is divided into optimistic lock , Pessimistic locking , Here are the introduction ：
- Pessimistic locking (Pessimistic Lock)： As its name suggests , With strong exclusivity and exclusivity . It refers to the data being exposed to the outside world ( Includes other current transactions of the system , And transactions from external systems ) The revision was conservative .
- Optimism lock (Optimistic Locking): It's a relative pessimistic lock , Optimistic locks assume that the data will not cause conflicts in general , So when the data is submitted for update , The data conflict will be formally detected or not , If there is a conflict , Then return the error information to the user , Let the user decide how to do it . Optimistic lock is suitable for scenarios with many read operations , This can improve the throughput of the program .
When monitoring key The value of changed before the transaction , The transaction is not executed . Only after the monitoring is cancelled , The transaction is executed , As shown below ：
1 127.0.0.1:6379> WATCH balance 2 OK 3 127.0.0.1:6379> set balance 300 4 OK 5 127.0.0.1:6379> MULTI 6 OK 7 127.0.0.1:6379> INCRBY balance 10 8 QUEUED 9 127.0.0.1:6379> DECRBY debt 10 10 QUEUED 11 127.0.0.1:6379> EXEC 12 (nil) 13 127.0.0.1:6379> GET balance 14 "300" 15 127.0.0.1:6379> UNWATCH 16 OK
Redis The nature of transactions
- Separate isolation operation ： All commands in the transaction , Will be serialized , Execute in order . The transaction is in progress , Will not be interrupted by other command requests .
- There is no concept of isolation level ： Commands in the queue , Before submission , None of them are actually executed , There is no internal transaction to view the updates in the transaction .
- There is no guarantee of atomicity ：redis In the same business , If a command fails , Other commands will be executed , No rollback （ Unlike relational databases ）.
Early onset Baidicheng
author ： Li Bai （ Tang Dynasty ）
Leaving at dawn the White King crowned with rainbow cloud , I have sailed a thousand miles through Three Georges in a day .
With monkeys' sad adieus the riverbanks are loud , My boat has left ten thought mountains far away .