当前位置:网站首页>Redis' optimistic lock and pessimistic lock for solving transaction conflicts

Redis' optimistic lock and pessimistic lock for solving transaction conflicts

2022-07-04 15:36:00 BugMaker-shen

One 、Redis The problem of transaction conflict

Example :
for instance ,3 Individuals have your account : Do you have 10000 element
A person asked to reduce the amount 8000
A person asked to reduce the amount 5000
A person asked to reduce the amount 1000

 Insert picture description here

Two 、 Pessimistic locking

 Insert picture description here
Pessimistic locking (Pessimistic Lock), seeing the name of a thing one thinks of its function , Is very pessimistic , Every time I go to get the data, I think others will modify it , therefore I lock it every time I get the data ( No other transaction operation is allowed after locking ), So that if people want to take this data, they will block Until it gets the lock . There are many lock mechanisms used in traditional relational databases , For example, line locks. , Table lock, etc. , Read the lock , Write locks, etc. , It's all locked before the operation

The disadvantage is low efficiency , Serial operation only

3、 ... and 、 Pessimistic locking

 Insert picture description here

Optimism lock (Optimistic Lock), seeing the name of a thing one thinks of its function , Is very optimistic , Every time I go to get the data, I think other people won't modify it , So it won't lock , So everyone can get the data , But in the process of updating, we will judge whether other people have updated this data during this period , You can use mechanisms like version numbers .

When reading data, it is unobstructed , When updating data, you need to match the version number of the read data with the version number of the data in the database , If it is consistent, it can be modified , Write back to the database after modification ; If the data is updated , It is inconsistent with the version number of the data in the database , You are not allowed to update , You need to read the data of the new version number to continue the operation

Optimistic lock is suitable for multi read applications , This can improve throughput .Redis It's using this check-and-set The mechanism implements the .

Four 、 Optimistic lock use

In execution multi Before starting a transaction , Execute first watch key1 [key2], You can watch one ( Or more ) key , If before the transaction is executed ( Or these ) key Altered by other orders , Then the business will be interrupted

 Insert picture description here

unwatch Cancel WATCH Command to all key Surveillance : If in execution WATCH After the command ,EXEC Order or DISCARD If the order is executed first , Then there's no need to execute UNWATCH 了

watch The monitoring function is realized through optimistic locking

Both terminals monitor balance And open the business
 Insert picture description here
 Insert picture description here
terminal 1 Yes balance Add 10
 Insert picture description here
terminal 1 Yes balance Add 20
 Insert picture description here
terminal 1 exec Successful execution of transaction
 Insert picture description here

terminal 2 exec Failed to execute transaction

 Insert picture description here
analysis : Optimistic lock leads to terminal 2 Transaction execution failed ;2 All terminals get balance This data , All monitor it , First, add 10, Then the version number is updated , The second terminal performs addition 20, Judge the read balance Version number and database balance Version number , It's different , You can't perform the operation

5、 ... and 、Redis Three characteristics of transaction

  • Separate isolation operation : All commands in the transaction are serialized 、 To execute in order . Transaction is in the process of execution , Will not be interrupted by command requests from other clients

  • There is no concept of isolation level : After opening the transaction , No, exec Before submitting, the command is only stored in the queue , Will not actually be implemented

  • There is no guarantee of atomicity : If a command fails in a transaction , Subsequent orders will still be executed , No rollback ( This and MySQL The atomicity of is different )

原网站

版权声明
本文为[BugMaker-shen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041330113125.html