The difference and application scenarios of pessimistic locking and optimistic locking

The difference and application scenarios of pessimistic locking and optimistic locking

https://zm8.sm-tc.cn/?src=l4uLj8XQ0J2TkJjRnIybkdGRmovQpryXmpG5mpGY0J6Ni5ack5rQm5qLnpaTjNDIys%2FPzMbOzg%3D%3D&uid=377d4216269bf3d8d359b1c664998fb5&hid=7eacc5eabee7f2317120a91afe7cb86b&pos=2&cid=9&time=1503321001497&from=click&restype=1&pagetype=0000000002000408&bu=web&query=%E4%B9%90%E8%A7%82%E9%94%81%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF&mode=&v=1&uc_param_str=dnntnwvepffrgibijbprsvdsdichei

 

Summary:

In the actual production environment, if the amount of concurrency is not large and dirty reading is not allowed, pessimistic locking can be used to solve the concurrency problem; but if the concurrency of the system is very large, pessimistic locking will bring very big performance problems, so we have to choose A method for optimistic locking.

 

Both types of locks have their own advantages and disadvantages, and one cannot be considered better than the other. For example, optimistic locks are suitable for cases where there are few writes , that is, when conflicts really rarely occur, which can save the overhead of locks , plus increase the overall throughput of the system. However , if conflicts occur frequently, the upper-layer application will continue to retry, which will reduce performance , so it is more appropriate to use pessimistic locks in this case.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326216362&siteId=291194637