当前位置:网站首页>Redis design and Implementation (VIII) | transaction
Redis design and Implementation (VIII) | transaction
2022-06-30 08:12:00 【Just look, not like】
List of articles
Business
- summary
Redis adopt
MULTI、EXEC、WATCHWait for the command to implement the transaction(transaction)function . Transactions provide a way to package multiple command requests , then Disposable 、 A mechanism for executing multiple commands sequentially and during transaction execution , The server will not interrupt the transaction and execute the command requests of other clients , It will execute all the commands in the transaction , Then I will process the command requests of other clients .
But everything needs to be met ACID, namely
Atomicity , Uniformity , Isolation and persistence
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set "name" "lhj"
QUEUED
127.0.0.1:6379> get "name"
QUEUED
127.0.0.1:6379> set "py" "sb"
QUEUED
127.0.0.1:6379> get "py"
QUEUED
127.0.0.1:6379> exec
1) OK
2) "lhj"
3) OK
4) "sb"
1. Implementation of transactions
- Three stages of transaction
- The transaction start :MULTI, The transaction ID will be added
- Order to join the team
- Transaction execution :EXIT、EXEC, Remove the identification of the transaction
1.1 Order to join the team
- summary
During the transaction phase , There will be different responses to different commands sent
- If the command sent by the client is
EXEC、DISCARD、WATCH、MULTIOne of the four orders , Then the server executes the command immediately .- On the contrary , If the command sent by the client is
EXEC、DISCARD、WATCH、MULTICommands other than four , Then the server doesn't execute the command immediately , Instead, the command is placed in a transaction queue , And back to the clientQUEUEDreply .
1.2 Transaction queue
- summary
Each client in its own redisClient There are transaction states in the structure , Transaction status , There are transaction queues and queued command counts , Transaction queue with FIFO The way to keep the order to join the team
typedef struet multiState {
// Transaction queue ,FIFO The order
multiCmd *commands;
// Has entered the queue to order the count
int count;
}multistate;
// The structure of the queue
typedef struct multiCmd {
// Parameters
robj **argv;
// The number of arguments
int argc;
// Command pointer
struct redisCommand *cmd;
}multiCmd;
1.3 Perform transactions
- summary
When a client in a transactional state sends
EXECOn command , ThisEXECThe command will be executed immediately by the server . The server will traverse the transaction queue of this client , Execute all commands saved in the queue , Finally, all the results of executing the command are returned to the client .
- Execution process
Create a blank reply queue 、 Read the parameters and the number of parameters and the command to execute , Execute and add the result to the reply queue , Remove the transaction id , Clear client transaction status ( Counters and release queues )
2.WATCH Implementation of commands
- summary
WATCHThe order is aOptimism lock (optimistic locking ), It can beEXECBefore the order is executed , Monitor any number of database keys , And inEXECWhen the order is executed , Check if at least one of the monitored keys has been modified , If so , The server will refuse to execute the transaction , And return a null reply to the client that represents the failure of transaction execution .
watch "xx" "xx" "xx"
- Example
You can see in the T4 After the execution , stay T5 Will refuse to execute the transaction
2.1 Use WATCH Command monitor database key
- summary
16 In a database redisDb There is one in the structure watch_keys Dictionary , The key is the monitored key , The value is a linked list , All clients monitoring the corresponding database key are recorded in the linked list
As you can see from the diagram ,name Key quilt c1 and c2 monitor , Others in the same way
- Be careful
Only when the client uses watch, Will appear in the linked list corresponding to the key
2.2 Trigger of monitoring mechanism
- summary
All commands to modify the database , such as
SET、LPUSH、SADD、ZREM、DEL、FLUSHDBwait , It will call after executionmulti.c/touchWatchReyFunction pairwatched keysDictionary check , Check if any client is monitoring the database key just modified by the command , If any , thattouchwatchKeyFunction will monitor the modified keyAllClient'sREDIS_DIRTY_CASLogo on , Indicates that the transaction security of the client has been compromised .
2.3 Determine whether the transaction is safe
- summary
When a client executes EXEC On command , Will check and monitor the client corresponding to the command execution , His
REDIS_DIRTY_CASIdentify whether to open , Whether it is opened or not will determine whether it is executed
- If it's opened , So at least one key has been modified , Refuse to enforce
- If not, commit the transaction , perform
3. The transaction ACID nature
- summary
In traditional relational databases , Often use ACID Property to test the transaction function Reliability and safety . stay Redis in , Transactions always have
Atomicity (Atomicity)、 Uniformity (Consistency) And isolation ( Isolation ), And when the Redis When running in a particular persistence mode , Transactions also havepersistence (Durability ).
- Why rollback is not supported
Because to guarantee Redis Simple and efficient
3.1 Atomicity (Atomicity)
- summary
Atomicity means , All commands in a transaction are executed as a whole , Either none of them or all of them
stay Redis in , All the commands in the transaction queue are executed , Or none of them
- Execution failure
Execution failure is a command error and the server refuses to execute , As a result, the entire transaction will not be executed
- An error is reported only after the execution ( Rollback is not supported )
Redis The biggest difference between a transaction of and a transaction of a traditional relational database is ,Redis Transaction rollback mechanism is not supported ( rollback ), Even if a command in the transaction queue has an error during execution , The whole business will continue , Until all commands in the transaction queue are executed .
3.2 Uniformity (Consistency)
- summary
A consistent transaction means , If the database is consistent before the transaction is executed , So after the transaction is executed , Whether or not the transaction is executed successfully , The database should still be consistent .“ Agreement ” It means that the data conforms to the definition and requirements of the database itself , Does not contain illegal or invalid error data .Redis Ensure transaction consistency through careful error detection and simple design ,
3.2.1 Team entry mistake
- summary
If a transaction queue command is in progress , The command does not exist or the command format is incorrect Redis Refuse to execute this transaction ; Older versions do not execute wrong commands , The correct one will execute
3.2.2 Perform error
- summary
This kind of mistake can not be found in the team joining mistake , For example, the command format is correct but not standard , It can only be checked during execution ; And executing the wrong command does not interrupt the transaction , Will continue with the rest of the order , And what has been executed will not be affected by the error
3.2.3 Server down
- summary
Anyway, you remember , Whether it is running in memory without persistence or RDB or AOF Will not cause data inconsistency , If you don't have a persistence scheme, the database will become empty after restart, so the data is always consistent
3.3 Isolation, ( Isolation )
- summary
Transaction isolation refers to , Even if there are multiple transactions executing concurrently in the database , Each transaction will not affect each other , And the result of the transaction executed in the concurrent state is exactly the same as that of the transaction executed in the serial state .
because Redis Use a single threaded way to execute transactions ( And the commands in the transaction queue ), And the server guarantees , The transaction will not be interrupted during transaction execution , therefore ,Redis The transactions of are always running in a serial way , And transactions are always isolated .
3.4 persistence
- summary
The durability of a transaction means , When a transaction is completed , The result of executing this transaction has been saved to the permanent storage medium ( For example, hard disk ) Inside the , Even if the server is down after the transaction completes , The results of the execution will not be lost .
because Redis The transactions of are simply wrapped in queues Redis command ,Redis Does not provide any additional persistence capabilities for transactions , therefore Redis The durability of transactions is determined by Redis The persistence pattern used determines :
- If you don't start persistence, then naturally there will be no persistence
- RDB In mode , There's no guarantee , because BGSAVE It is usually set at a time and executed in a child process with asynchrony , There is no guarantee that it will be saved in the hard disk for the first time , So I don't have
- AOF In the pattern , Data loss may occur even if it is synchronized every second , If you return the frequency to the operating system, that is appendfsync The value of the option is no when , Even less persistent
- There is only one case
Regardless of Redis In what mode , Add... At the end of a transaction SAVE Commands always guarantee the durability of transactions :
边栏推荐
- String and underlying character types of go data type
- Wechat applet reports errors using vant web app
- Recurrence relation (difference equation) -- Hanoi problem
- 【NVMe2.0b 14-5】Firmware Download/Commit command
- 牛客小白月賽52
- 【花雕体验】14 行空板pinpong库测试外接传感器模块(之一)
- Dlib library blink
- [tensorflow GPU] building of deep learning environment under windows11
- mysql无法连接内网的数据库
- Tue Jun 28 2022 15:30:29 gmt+0800 (China standard time) date formatting
猜你喜欢

Deep learning -- feature point detection and target detection

Is it difficult to jump job ByteDance? With these skills, you can easily pass

Game 280 problem2: minimum operands to turn an array into an alternating array

安科瑞高等学校校园建筑节能监管系统建设

Deep learning - LSTM

全栈最全性能测试理论-总结
![[flower carving experience] 13 build the platformio ide development environment of esp32c3](/img/32/2c30afe77bf82774479a671ff16898.jpg)
[flower carving experience] 13 build the platformio ide development environment of esp32c3

C # about Net cognition

Deep learning - networks in networks and 1x1 convolution

Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)
随机推荐
C. Fishingprince Plays With Array
Bingbing learning notes: quick sorting
你了解IP协议吗?
Want to change careers, but don't know what to do? This article is written for you who are confused
mysql无法连接内网的数据库
Opencv image
Go 数据类型篇之基本数据类型之间的转化
Game 280 problem2: minimum operands to turn an array into an alternating array
Introduction to opencv (I): image reading and display
ACM. HJ48 从单向链表中删除指定值的节点 ●●
【NVMe2.0b 14-8】Set Features(下篇)
JS中的this指向
MySQL cannot connect to the intranet database
Cesium learning notes (I)
ACM. Hj48 delete the node with the specified value from the one-way linked list ●●
C preliminary chapter learning route
鲸探NFT数字臧品系统开发技术分享
回文子串、回文子序列
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel





