Optimistic locking and pessimistic locking are mainly used to solve concurrency problems, and they are relatively low-level concurrency problems.
scenario
Concurrency problems occur when multiple users work on the same resource. For example, a user likes an article. Our business processing is generally as follows:
steps | operation | Database Statement Examples |
---|---|---|
1 | Query this article | select id, praise_points from t_article where id = 1 |
2 | willThe user ID andThe article ID Is written to the likes table |
insert into t_praise_link (id, user_id, article_id) values (…) |
Other threads | Updated the number of likes on the post | … |
3 | Update the number of likes on a post | update t_article set praise_points = ? where id = 1 |
At this time, it is not locked, and in high concurrency, there will be a situation where the number of likes recorded in the article table is less than the actual number of likes. Let’s use locking to solve this concurrency problem.
Pessimistic locking
Always assume the worst case, so every time [select] is always locked, do not allow other threads to modify. Row lock, table lock, shared lock, exclusive lock and Synchronized in Java all belong to the category of pessimistic lock.
The realization of database lock
The lock type | Implementation example |
---|---|
A Shared lock | select id, praise_points from t_article where id = 1 lock in share mode |
Exclusive lock | select id, praise_points from t_article where id = 1 for update |
Exclusive lock | InnoDB engine, update,insert,delete by default automatic exclusive lock |
Apply pessimistic locking
It is important to first analyze whether a shared lock should be used (which allows other transactions to add shared lock reads as well, but does not allow other transactions to modify or add exclusive locks) or exclusive locks. First of all, let’s look at the business scenario at this time. The data we lock and the data we modify are article tables. In this case, it is not appropriate to use the shared lock, which is prone to deadlock. The reason is: shared locks, transactions are added, all can read. Changes are unique and must wait for the previous transaction commit.
steps | operation | Database Statement Examples |
---|---|---|
begin | Start the transaction | |
1 | Query this article (with exclusive lock) | select id, praise_points from t_article where id = 1 for update |
2 | willThe user ID andThe article ID Is written to the likes table |
insert into t_praise_link (id, user_id, article_id) values (…) |
3 | Update the number of likes on a post | update t_article set praise_points = ? where id = 1 |
end | End of the transaction |
Optimistic locking
When updating, I will judge whether others have updated the data.
Apply optimistic locking
This is typically implemented using a version number mechanism or CAS algorithm (potential ABA problem). The most commonly used is the version number mechanism, mainly because it is relatively simple to implement, commonly used ORM have a perfect implementation mechanism.
steps | operation | Database Statement Examples |
---|---|---|
begin | Start the transaction | |
1 | Query this article (with exclusive lock) | select id, praise_points, version from t_article where id = 1 |
2 | willThe user ID andThe article ID Is written to the likes table |
insert into t_praise_link (id, user_id, article_id) values (…) |
3 | Update the number of likes on a post | update t_article set praise_points = ? where id = 1 and version = 1 |
end | End of the transaction |
conclusion
This article is based on database level simply introduces the concept of optimistic locking and pessimistic locking, but in the development of life, the types of lock is very much, such as biased locking, lock, etc., lightweight, heavy lock, lock clearance for different concurrency issues, actually the solution is different, but still have some of the giants of the experience for reference.
- Pessimistic locks are suitable for scenarios where you write more and read less;
- Optimistic locking works for scenarios where you write less and read more;
- Alibaba’s suggestion: If the probability of conflict per visit is less than
20%
, recommended useOptimistic locking
, otherwise usePessimistic locking
.Optimistic locking
The number of retries must not be less than3
Times; - Control the scope of the lock and reduce the scope of the lock object, such as row locking.