Optimistic lock and pessimistic lock

Source: Internet
Author: User

Lock ( locking )

< Span style= "" >    this concept in our learning multi-threaded time has been contacted, in fact, the lock and multi-threaded inside the processing of the concurrency of the lock is a truth, are violent to the resources for their own all. The purpose of this is to use a mechanism to ensure that some data is not modified by the outside world during an operation, in this case the so-called " lock "
pessimistic Lock (  pessimistic locking  ) dependent data mechanism
      pessimistic lock, As the name of the database, he is pessimistic about the database, he felt that every operation of his procedures are likely to produce concurrency. It refers to the conservative attitude to the data being modified by the outside world (including other transactions currently in the system, as well as transactions from external systems), so that the data is locked during the entire data processing process. Pessimistic lock implementation, often rely on the database provided by the lock mechanism (also only the database layer provides a lock mechanism to truly guarantee the exclusivity of data access, otherwise, even in this system to implement the locking mechanism, there is no guarantee that the external system will not modify the data).
A typical pessimistic lock call that relies on the database (plus forupdate):


Select  *  from Account wherename= "Erica" Forupdate

this one SQL statement is locked. Account all eligible search criteria in the table ( name= " Erica " ) is recorded. These records cannot be modified by the outside world before the transaction commits (the locks in the transaction are freed during transaction commits).


Optimistic Lock (optimistic Locking)

Relative pessimistic lock, the optimistic locking mechanism adopts a more relaxed locking mechanism. Pessimistic locking relies on the lock mechanism of the database in most cases, to ensure the maximum degree of exclusivity of the operation. But it comes with a lot of overhead for database performance, especially for long transactions, which are often unsustainable. The optimistic locking mechanism solves this problem to some extent. Optimistic locking, mostly based on the data version (versions) recording mechanism implementation. What is a data version? is to add a version identity to the data, which is typically done by adding a "version" field to the database table in the version solution based on the database table .


such as a financial system, when an operator reads the user's data and modifies it on the basis of the read-out user data (such as changing the user account balance), a pessimistic locking mechanism means that the entire operation

Approached (the entire process of reading data from the operator, starting the modification until the result of the change is submitted, even including the time the operator is going to cook the coffee), the database record is always locked, and you can imagine

Hundreds of thousands of concurrent, such a situation will lead to what consequences. The optimistic locking mechanism solves this problem to some extent. Optimistic locking, mostly based on the data version (versions) recording mechanism implementation. What is a data version? is to add a version identity to the data, which is typically done by adding a "version" field to the database table in the version solution based on the database table.

When the data is read, the version number is read together, and then the version number is added one after the update. At this point, the version data of the submitted data is compared to the current version of the database table corresponding to the record, if the submitted data

The version number is newer than the current version number of the database table, otherwise it is considered to be outdated data. For the example above to modify user account information,

Suppose there is a version field in the Account information table in the database, the current value is 1, and the Current Account balance field (balance) is $ $.

1. Operator a reads it out (version=1) and deducts $ ($100-$50) from his account balance.

2. During operator A's operation, operator B also reads this user information (version=1) and deducts $ $100-$20 from its account balance.

3, operator a completed the modification work, the data version number plus one (version=2), together with account deduction after the balance (BALANCE=$50), submitted to the database update, at this time due to the submission of data version large

The database records the current version, the data is updated, and the database record version is updated to 2.

4, operator B completed the operation, but also the version number plus one (version=2) attempt to submit data to the database (balance=$80), but at this time than the database record version found that operator B submitted The data version number is 2, the current version of the database record is also 2, and the "commit version must be greater than The current version to perform an update ", so the submission of operator B is dismissed.

This avoids the possibility of operator B overwriting operator A's results with the results of old version=1-based data modifications.

As can be seen from the above example, the optimistic locking mechanism avoids the database lock-up overhead in a long transaction (both operator A and operator B do not locking the database data), greatly improving the system under large concurrency

Overall performance.

It should be noted that the optimistic locking mechanism is often based on the data storage logic in the system, so there are some limitations, as in the above example, because the optimistic locking mechanism is implemented in our system, from the external system users

The balance update operation is not controlled by our system and may cause dirty data to be updated in the database. In the system design phase, we should take full account of the possibility of these situations, and make appropriate adjustments (such as

The optimistic locking policy is implemented in the database stored procedure, and only the data update path based on this stored procedure is opened externally, rather than the database table being exposed directly to the public.


Optimistic lock and pessimistic lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.