当前位置:网站首页>Does redis transaction support acid?
Does redis transaction support acid?
2022-06-25 20:00:00 【Code byte】
Tencent interviewer :「 Do you understand the database transaction mechanism ?」
「 Inner monologue : small token of kindly feelings , No ACID Well , Think about it in a second , I interviewed a technical expert , It's not such a simple question 」
Cheng Xuyuan :「balabala…… Extremely confident and calm .」
Tencent interviewer :「Redis You know what's going on ? Its transaction mechanism can realize ACID Attribute ?」
Cheng Xuyuan :「 Scratch your head , This …… That's true. lua Scripts can implement transactions …」
Tencent interviewer :「 well , Go back and wait for the announcement .」
Margo , I learned from you 《Redis series 》 Got a lot offer, I didn't expect to lose in the end 「Redis How to implement transactions ?」 On this issue .
Let's analyze step by step :
- What is business ACID?
- Redis How to implement transactions ?
- Redis What properties can a transaction implement ?
- Lua Script implementation .
What is business ACID
Ghost blowing lamp 《 Yunnan insect valley 》 There is a saying by the school captain in touch with gold 「 When combined, there will be , You will die 」, In order to find Chen Zhu, the three of them have a clear division of labor 、 Work together to advance and retreat To succeed .
Business (Transaction) Is the concurrency control unit , A sequence of operations , These operations are either performed , Either not .
「 It is an indivisible work unit 」.
When the transaction is executing , Special attribute guarantee will be provided :
Atomicity (Atomicity): Multiple operations of a transaction must be completed , Or not at all (ps:MySQL What does atomicity depend on ? Welcome to comment in the message area );
Uniformity (Consistency): After the execution of the transaction , The database integrity constraint is not broken , The sequence of transaction execution is legal data status .
Database integrity constraints include, but are not limited to :
- Entity integrity ( The primary key of a row exists and is unique );
- Column integrity ( Such as the type of the field 、 size 、 The length should meet the requirements )
- Foreign key constraints ;
- User-defined integrity ( Before and after the transfer , The sum of the two balances should remain the same ).
Isolation, (Isolation): Operations within a transaction are isolated from other transactions , Transactions that execute concurrently cannot interfere with each other .
What we pay attention to is the interaction between different affairs , Strict isolation corresponds to serialization in the isolation level (Serializable).
persistence (Durability): Once the transaction is committed , All changes will be permanently saved to the database , Even if the system crashes, the data will not be lost after restart .
Margo , I understand ACID After the specific requirements of ,Redis How to implement the transaction mechanism ?
Redis How to implement transactions
MULTI、EXEC、DISCARD and WATCH The order is Redis The basis for implementing transactions .
Redis The execution of a transaction consists of three steps :
- Open transaction ;
- Order to join the team ;
- Execute a transaction or discard ;
Open a transaction explicitly
Client pass MULTI
The command explicitly means to start a transaction , Subsequent commands will be queued , It doesn't actually execute .
Order to join the team
The client sends a series of instructions to be executed in the transaction to the server .
It should be noted that , Although the instruction is sent to the server , however Redis The instance just temporarily stores this series of instructions in a command queue , Not immediately .
Execute a transaction or discard
The client sends a command to the server to commit or discard the transaction , Give Way Redis Execute the specific instruction sent in step 2 or clear the queue command , Give up execution .
Redis Just call EXEC when , Queue command execution can be scheduled .
Can also pass DISCARD Discard the command saved in the queue in the second step .
Redis Business case
Execute our sample code through the online debugging website :https://try.redis.io
Normal execution
adopt MULTI
and EXEC
Execute a transaction process :
# Open transaction > MULTIOK# Start defining a list of instructions > SET “ official account : Code byte ” " fans 100 ten thousand "QUEUED> SET "order" "30"QUEUED> SET " Number of articles " 666QUEUED> GET " Number of articles "QUEUED# Actually execute the transaction > EXEC1) OK2) OK3) OK4) "666"
We see that the return result of each read-write instruction is QUEUED
, Thank you. All operations are temporarily stored in the command queue , Not actually implemented yet .
When executed EXEC
command , You can see the response data of each instruction .
Give up the business
adopt MULTI
and DISCARD
Discard queue command :
# Number of initialization orders > SET "order:mobile" 100OK# Open transaction > MULTIOK# Order - 1> DECR "order:mobile"QUEUED# Discard the drop column command > DISCARDOK# The data has not been modified > GET "order:mobile""100"
Margo ,Redis Your business can guarantee ACID Characteristics ?
That's a good question , Let's analyze .
Redis The transaction satisfies ACID?
Redis Transactions can execute more than one command at a time , And there are three important guarantees :
- Batch instructions are executing EXEC The command will be put into the queue for temporary storage ;
- received EXEC Enter transaction execution after command , The execution of any command in the transaction failed , The rest of the orders are still being carried out ;
- During transaction execution , Commands submitted by other clients will not be inserted into the current command execution sequence .
Atomicity
Margo , If an error occurs during the execution of the transaction , Is atomic performance guaranteed ?
During the transaction , Two command errors may be encountered :
- In execution
EXEC
Before the command , The instruction sent itself is wrong . as follows :- Wrong number of parameters ;
- Wrong command name , Used a command that does not exist ;
- Out of memory (Redis Case use
maxmemory
Instruction to configure memory limits ).
- In execution
EXEC
After the command , Orders may fail . for example , The data types of the command and operation do not match ( Yes String type Of value Yes List The list of operations ); - In the execution of a transaction
EXEC
On command . Redis The instance failed, resulting in transaction execution failure .
EXEC An error is reported before execution
When ordered to join the team ,Redis will Report an error and record the error .
here , We You can continue to submit commands .
Wait until it's done EXEC
After the command ,Redis will Refuse to execute all submitted command operations , Return the result of transaction failure .
thus , All commands in the transaction will no longer be executed , It guarantees atomicity .
The following is the command queue error , Examples of transaction failures :
# Open transaction > MULTIOK# Send the first operation in the transaction , however Redis The command is not supported , Return error message 127.0.0.1:6379> PUT order 6(error) ERR unknown command `PUT`, with args beginning with: `order`, `6`,# Send the second operation in the transaction , This operation is the correct command ,Redis Join the order > DECR b:stockQUEUED# Actually execute the transaction , But there was an error in the previous command , therefore Redis Refuse to enforce > EXEC(error) EXECABORT Transaction discarded because of previous errors.
EXEC Error reported after execution
When a transaction operation is queued , The data types of the command and operation do not match , but Redis The instance does not check for errors .
however , The execution of the EXEC After the order ,Redis Actually execute these instructions , You're going to report a mistake .
Knock on the blackboard :Redis Although an error will be reported for the wrong instruction , But the transaction will still execute the correct command , At this time, the atomicity of the transaction cannot be guaranteed !
Margo , Why? Redis Rollback is not supported ?
Actually ,Redis There is no rollback mechanism in . although Redis Provides DISCARD command .
however , This command can only be used to actively abandon transaction execution , Empty the temporary command queue , No rollback effect .
EXEC Execution time , failure
If Redis Open the AOF journal , that , Only part of the transaction operations will be recorded to AOF In the log .
We need to use redis-check-aof Tool check AOF Log files , This tool can remove incomplete transaction operations from AOF Remove from file .
thus , We use AOF After restoring the instance , Transaction operations will no longer be performed , So that atomicity .
Simple summary :
- When you order to join the team, you report an error , Will abandon the transaction execution , Guaranteed atomicity ;
- No error was reported when ordering to join the team , The actual execution times an error , There is no guarantee of atomicity ;
- EXEC Instance failure during command execution , If it's on AOF journal , You can guarantee atomicity .
Uniformity
Consistency will be affected by wrong commands 、 The impact of the occurrence time of the instance failure , According to the command, the occurrence time of the two dimensions of the instance fault , It can be analyzed in three cases .
EXEC Before execution , Team entry error reporting
The transaction will be abandoned , So consistency can be guaranteed .
EXEC After execution , The actual execution times an error
If there is an error, it will not be executed , Correct instructions can be executed normally , Consistency can guarantee .
EXEC Execution time , Instance failure
After an instance fails, it will be restarted , This is related to the way of data recovery , We need to check whether the instance is enabled RDB or AOF Let's discuss the situation .
If we don't turn on RDB or AOF, that , After instance failure and restart , There's no data , The database is consistent .
If we use RDB snapshot , because RDB Snapshots do not execute when a transaction is executed .
therefore , The results of the transaction command operation will not be saved to RDB In the snapshot , Use RDB When restoring a snapshot , The data in the database is also consistent .
If we use AOF journal , The transaction operation has not been recorded yet AOF When the log , The instance fails , that , Use AOF The database data recovered by log is consistent .
If only part of the operation is recorded AOF journal , We can use redis-check-aof Clear the completed operations in the transaction , The database is also consistent after recovery .
Isolation,
Transaction execution can be divided into orders to join the queue (EXEC Before the order is executed ) And the actual execution of the command (EXEC After the execution of the command ) Two phases .
Therefore, during concurrent execution, we analyze these two stages in two cases :
- Concurrent operations in
EXEC
To execute before an order , Isolation needs to passWATCH
Mechanism guarantees ; - Concurrent operations in
EXEC
After the command , Isolation can guarantee .
Margo , What is? WATCH Mechanism ?
Let's focus on the first case : A business EXEC When the command has not been executed , The command operation of the transaction is temporarily stored in the command queue .
here , If there are other concurrent operations , alike key Be modified , It depends on whether the transaction uses WATCH
Mechanism .
WATCH The function of the mechanism is : Before the transaction is executed , Monitor the value change of one or more keys , When a transaction calls EXEC When the order is executed ,WATCH The mechanism will first check whether the monitored key has been modified by other clients .
If it changes , Just give up the transaction execution , Avoid breaking the isolation of transactions .
meanwhile , The client can execute the transaction again , here , If there are no concurrent operations to modify transaction data , The transaction can be executed normally , Isolation is also guaranteed .
No, WATCH
without WATCH Mechanism , stay EXEC Read and write data by concurrent operations before command execution .
When executed EXEC When , The data to be manipulated within the transaction has changed ,Redis There is no separation between transactions .
Concurrent operations in EXEC Then receive and execute
As for the second case , because Redis Is to execute commands with a single thread , and ,EXEC After the execution of the command ,Redis It ensures that all commands in the command queue are executed first, and then the subsequent instructions are executed .
therefore , under these circumstances , Concurrent operations do not break transaction isolation .
persistence
If Redis Not used RDB or AOF, Then the persistence property of the transaction cannot be guaranteed .
If Redis Used RDB Pattern , that , After a transaction is executed , And next time RDB Before the snapshot is executed , If an instance goes down , Data loss , In this case , The data modified by transactions cannot be guaranteed to be persistent .
If Redis Adopted AOF Pattern , because AOF Three configuration options for mode no、everysec and always There will be data loss .
therefore , The persistence property of transactions is still not guaranteed .
No matter Redis What persistence pattern to use , The persistence property of transactions is not guaranteed .
summary
- Redis With a certain degree of atomicity , But rollback is not supported .
- Redis Do not have ACID The concept of consistency in .( Or say Redis Ignore this in design )
- Redis It's isolated .
- Redis There is no guarantee of persistence .
Redis The transaction mechanism can ensure consistency and isolation , But there is no guarantee of persistence .
however , because Redis Itself is an in memory database , Persistence is not a required property , We are more concerned about atomicity 、 Consistency and isolation .
The case of atomicity is more complicated , When the syntax of the command used in the transaction is incorrect , Atomicity is not guaranteed , In other cases , Transactions can be executed atomically .
Good article recommends
Redis Persistent article :AOF And RDB How to ensure high availability of data
Redis High availability : Data consistency synchronization principle of master-slave architecture
Redis High availability : The principle of sentry group
Redis High availability :Cluster The cluster theory
Redis Actual combat : Skillfully use Bitmap Achieve billion level massive data statistics
Redis Actual combat : adopt Geo The type implements that people near meet the goddess
边栏推荐
- Connecting PHP to MySQL instances in the lamp environment of alicloud's liunx system
- PAT B1053
- 2.15(Multiple of 3 Or 5)
- VMware failed to prompt to lock this profile exclusively
- Use of serialize() and serializearray() methods for form data serialization
- PostgreSQL change table owner
- Verification code native JS canvas
- 2.6 finding the sum of the first n terms of factorial sequence
- PAT B1051
- New features of php7
猜你喜欢
<C>. array
Applet password input box
Principles of MySQL clustered index and non clustered index
ECS 7-day practical training camp (Advanced route) -- day04 -- build a portal using ECs and polardb
2.3 partial sum of square and reciprocal sequences
Record Baidu search optimization thinking analysis
Arduino ide + esp8266+mqtt subscribe to publish temperature and humidity information
Number of wechat applet custom input boxes
PostgreSQL user role permissions
Can GoogleSEO only do content without external chain? (e6zzseo)
随机推荐
6、 Configuration resolution of hikariconfig
六、HikariConfig的配置解析
Please do not call Page constructor in files
ECS 7-day practical training camp (Advanced route) -- day03 -- ecs+slb load balancing practice
PAT B1096
PAT B1057
RPM package installation command
2020-12-09 laravel . Env file loading mechanism process
PostgreSQL division considerations
Randomly generate 100 non repeating numbers between 1 and 150 and put them in the array
<C>. Branch and loop statements
Is it safe to open a new bond account
<C>. function
Arduino : No such file or directory
The functions in the applet page are better than those in app JS first execution solution
Convert word to PDF through libreoffice
Ali vision AI training camp-day01
Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
200 OK (from memory cache) and 200 OK (from disk cache)
PAT B1086