当前位置:网站首页>Please, don't use pessimistic locks in high concurrency scenarios!

Please, don't use pessimistic locks in high concurrency scenarios!

2022-06-21 13:29:00 Hollis Chuang

△Hollis, One right Coding People with unique pursuit △

fc1e235c831a7b5b3ae85e837c564c7e.png

This is a Hollis Of the  394  Original sharing

author l Hollis

source l Hollis(ID:hollischuang)

f169779b32adf15e652dab8f03406b9a.png

Hollis In the limited time discount for your new book , An in-depth explanation Java Basic dry goods notes !

We know , Optimistic lock and pessimistic lock are the main technical means of concurrency control , Usually used in database management .

however , Optimism lock 、 Pessimistic locks are not like row level locks 、 Shared lock and other concepts are also real locks . In fact, they are just concepts defined by people , It can be thought of as an idea .  In fact, it is not only the concept of optimistic lock and pessimistic lock in relational database system , image memcache、hibernate、tair There are similar concepts .

For different business scenarios , Different concurrency control methods should be chosen . therefore , Don't take optimistic concurrency control and pessimistic concurrency control in a narrow sense as DBMS The concept of , Don't put them and the lock mechanism provided in the data ( Row lock 、 Table locks 、 Exclusive lock 、 Shared lock ) Confuse STH with STH else . Actually , stay DBMS in , Pessimistic lock is realized by using the lock mechanism provided by the database itself .

There are many introductions about optimistic lock and pessimistic lock on the Internet , I also have articles before (《 In depth understanding of optimistic lock and pessimistic lock 》) Specially introduced , Here, for the convenience of understanding , Just make a simple summary .

Pessimistic lock and optimistic lock

Pessimistic locking , As its name suggests , It refers to the pessimistic attitude towards the data being modified by the outside world , therefore , In the whole data processing process , You need to lock the data first , Operate after obtaining the lock .

stay MySQL in , You can use exclusive locks to implement pessimistic locks , It mainly uses select ... for update grammar .

Use pessimistic locks , Need to be closed mysql Auto-commit properties for the database :set autocommit=0;

Then in the transaction , Lock the data with the following statement :

select status from t_goods where id=1 for update

above , In the face of id = 1 Before the modification of the records , Through the first for update The way to lock , And then make changes . This is a typical pessimistic lock strategy .

If the above inventory modification code is concurrent , At the same time, only one thread can start a transaction and get id=1 Lock of , Other transactions must wait until this transaction is committed . This ensures that the current data is not modified by other transactions .

Relative to pessimistic locks , The optimistic locking assumption assumes that data in general will not cause collisions , So when the data is submitted for update , The data conflict will be formally detected or not , If there is a conflict , Returns the user's error message , Let the user decide how to do it .

The implementation of optimistic locking does not use the locking mechanism provided by the database . The general way to implement optimistic locking is to record the data version , As below SQL:

update t_goods     
 set status=2,version=version+1    
 where id=#{id} and version=#{version}

Difference between the two

above , We learned about the idea of optimistic lock and pessimistic lock, and realized it , Discuss their differences .

First , The locking time is different , Pessimistic locking is locking at the beginning of a transaction , After you get the lock, you can perform business operations . Optimistic locks, on the other hand, do not control concurrency until they are updated , So it is the first business operation .

secondly , Pessimistic lock is mainly realized by means of exclusive lock of database , The exclusive lock is essentially a blocking lock .  If the concurrency is large and there are many conflicts , This will cause many threads to be blocked by locks , The result of the request RT Elongated , And it will occupy a lot of database links .

by comparison , Optimistic locks do not cause congestion , But the problem it brings is that if the concurrency conflict is high , Then there will be many failures , The business code is required to handle such failures .

The third point , That's it Optimistic lock is called lock , But he didn't add an extra lock , It's through CAS To achieve , So he is more efficient , Pessimistic locks need to be locked by using the database locking mechanism , This will bring some additional cost .

And finally , It's also more important , That's it Pessimistic lock because of the action of locking , So it will lead to deadlock .

Do not use pessimistic locks for high concurrency

I strongly recommend that you , Give priority to optimistic locks , Especially, the concurrency is high , And there are many conflicts .

Because we mentioned earlier , Pessimistic locks have an additional cost 、 And it may cause deadlock . But these are not the most important .

most important of all , Pessimistic lock is essentially a blocking lock , In the case of high concurrency , There will be many threads blocked , These blocked threads will occupy the database links . Therefore, the concurrency of your system will be very low , In addition, the response time of these blocked threads will be very long , Extremely affecting the user experience , There will be a lot more slow SQL.

One extra sentence , stay MySQL 8.0 in , Has supported select ... for update nowait, You can turn a blocking lock into a non blocking lock . Some problems caused by pessimistic lock blocking can be solved to some extent , However, the additional cost of locking and the problem of deadlock still exist .

therefore , In high concurrency scenarios , It is recommended to use optimistic lock , In especial MySQL 5.x In the version of the , Because it's not supported nowait, Once the pessimistic lock is used , It will greatly reduce the concurrency of your system .

c7ed21c1336dc12128ad8e859fb6bfd8.png

End

My new book 《 In depth understanding of Java The core technology 》 It's on the market , After listing, it has been ranked in Jingdong best seller list for several times , At present 6 In the discount , If you want to start, don't miss it ~ Long press the QR code to buy ~

76d2c1b2742809aec24facb0b86fbbeb.png

Long press to scan code and enjoy 6 A discount

 Previous recommendation 

f23d3e109b4fae8b9381e36318552bcb.png

It's a hot topic : a monthly salary 2~3W The Manon of , How to spend the day ?


72fe6d390deec9be4e390b997daa1470.png

It's still in use SimpleDateFormat Format the time ? Be careful of project collapse !


0a1120dbf36fdae98666e6ae85f58b54.png

Start a new company , How to get familiar with code quickly ?

If you enjoyed this article ,

Please hold on to the QR code , Focus on  Hollis.

b00054fae8c9ee37681e6db0200b137b.png

Forward to circle of friends , It was my greatest support .

Order one   Looking at  

Like is a feeling

Looking is a kind of support

原网站

版权声明
本文为[Hollis Chuang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211045586589.html