[Turn] teach you the difference between optimistic locking and pessimistic locking in one minute

Teach you the difference between optimistic locking and pessimistic locking in one minute

Pessimistic Lock, as the name suggests, is very pessimistic. Every time I go to get the data, I think that others will modify it, so every time I get the data, it will be locked, so that others will block until it gets the data. to the lock. Many such lock mechanisms are used in traditional relational databases , such as row locks, table locks, read locks, write locks, etc., all of which are locked before operations are performed.

 

Optimistic Lock, as the name suggests, is very optimistic. Every time I go to get data, I think that others will not modify it, so it will not be locked, but when it is updated, it will judge whether others have updated it during this period. Data, you can use mechanisms such as version numbers. Optimistic locks are suitable for multi-read application types, which can improve throughput. For example, if the database provides a mechanism similar to write_condition, it is actually an optimistic lock.

 

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 the case 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.

 

Original: http://blog.csdn.net/hongchangfirst/article/details/26004335

Guess you like

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