Lets learn "Different types of database locks"
In this tutorial, we'll learn about different types of locks in the database. Basically, there are two types of locks.
- Pessimistic Lock
- Optimistic Lock
Pessimistic Lock
Pessimistic is a simple mutex lock. In the pessimistic lock, the clients wait for the lock to be acquired before doing the DB update. So the DB operation will never fail, unlike optimistic lock.
<acquire lock>
perform DB write......
<release lock>
Usecase
Pessimistic locks are used on RDBMS.
Optimistic Lock
Optimistic Locking is a strategy where you read a record and note its version/hash/checksum and while updating the record, check if the version/hash/checksum is the same before updating the record. Record update fails otherwise if version/hash/checksum is changed.
Let's consider an example to understand an optimistic lock using the above DB table data.
- Suppose there are two clients A and B who wants to update the row with ID 3 at the same time.
- Say Client A wants to modify the data value to "Locks by A".
- And Client B wants to modify the data value to "Locks by B".
- When the requests reach the DB level, clients A and B will read the row as 3/Locks/v1.
- The write by client A will reach DB in the form of 3/Locks by A/v1 which means update row with ID 3 in DB with data "Locks by A" if the version is v1.
- The write by client B will reach DB in the form of 3/Locks by B/v1 which means update row with ID 3 in DB with data "Locks by B" if the version is v1.
- After updating the row, the version will be changed to v2.
- Now since the write requests are reached at DB at the same time from client A and B, only one of the write will be successful and the other one will be failed.
- And the one which will be successful is non-deterministic.
- Suppose client B will be able to write successfully. Now the row with ID 3 contains data "Locks by B" and version v2.
- Now with this request from a client A will fails because the version has changed now to v2 and client A's request 3/Locks by A/v1 says only update if the version is v1.
- client A will retry the request and this time it reads data as 3/Locks by B/v2 and it will update the data as 3/Locks by A/v2 and the version is changed to v3.
Usecase
Let's learn about the use-cases where the optimistic lock is required and pessimistic lock won't work there.
- In any content updation site, for example in Wikipedia, anyone can update any article, If we use a pessimistic lock then no one can edit an article once it is picked by someone else. But its not a good user experience because editing an article can take a few days, we don't wanna block others for this much time. We can solve this problem using optimistic locks. With optimistic lock, anyone can pick a particular article to edit it, but once they try to submit the edited changes, if someone else has already made changes in that article, we can fail the submit and show the conflicts so user can retry submitted the edited changes after resolving the conflicts.
Performance difference between Pessimistic and Optimistic lock
Pessimistic lock works great with RDBMS and low scale but in NoSQL DB, there are lot of concurrent requests, blocking other requests because of one request doesn't work great to optimistic lock is preferred in such case.
General rule is:
Few conflicts - Optimistic lock
More conflicts - Pesssimistic lock
The reason why Optmistic lock won't work well with more conflicts is because most of the requests will fail only and thus we'll be wasting the CPU cycles. Hence waiting for the resource to get free sounds better if there are More conflicts.
HOPE YOU LIKE THIS TUTORIAL. FEEL FREE TO COMMENT BELOW IF YOU HAVE ANY DOUBTS. AND STAY TUNED FOR MORE TUTORIALS :)
Comments
Post a Comment