High Concurrency, Low Conflict: A Comprehensive Guide to Optimistic Locking
Techniques for Efficiently Handling Concurrent Data Access in Modern Systems
Optimistic locking is a concurrency control mechanism mostly used in software development and database to handle concurrent access of data without locking the resource.
Handles multiple transactions to complete at same time.
Performs Conflict check at the time of update operation.
So now before doing a deep dive into optimistic locking lets first understand the types of locking, so we will get an idea where does this fits correctly.
Types of Locking
Pessimistic Locking
Optimistic Locking
Pessimistic Locking
In this Pessimistic, a transaction locks the resource at the beginning till it gets complete which prevents other transactions to access this resource.
Although, this type of locking seems to be good candidate for locking a resource with a predictable behaviour but there are certain issues with this, listed below are the important ones.
Reduced Concurrency
Performance Overhead
High changes of Deadlocks
It adds complexity in Software development due to complex management of locking , timeouts and etc.
This where optimistic locking comes in and saves.
Optimistic Locking
In optimistic locking, it does not locks the entire resource from the beginning as it assumes that the chances for conflicts are rare so it checks for conflict only at the end of transaction which allows more concurrent access.
one way it checks for conflicts is by maintaining versions of resource.
How Optimistic Locking Works ?
Optimistic locking is a three steps process as it contains
Read phase
It reads the record and stores the version number.
Validation phase
Before update operation, transaction checks if the version number of record is same as read phase.
Write phase
If there is no conflict found, version number is same as read phase then it updates the new value.
Optimistic locking has higher performance comparatively to pessimistic locking as we do not lock resource from beginning.
But Understand, performance of optimistic locking based systems decreases in higher write situations as there is an overhead of validation phase.
It reduces the chances of deadlock as logically it is just a logic check not actual locking of resource.
There are several use cases of using optimistic locking in real life today.
Implementations in web applications
In ecommerce systems, while placing order for an inventory.
Same applies in hotel reservation systems while placing a reservation.
In summary, optimistic locking is best suitable for cases of low data contention , which where simultaneous writes are not much usual.
By this time you might have an idea of what is optimistic locking and where does this fits off ? What is more important is the idea of trade off you see above there are pros and cons of optimistic locking.
So based on the given situation one should know if optimistic will be suitable here or not.
If you really like my content you can subscribe me below.
Youtube Channel - https://www.youtube.com/channel/UCpF3Y8AxzgYZnI8Zcf_G_fg
You can follow me on linkedin here - https://www.linkedin.com/in/suchait-gaurav-944479109/
Github Repo - https://github.com/suchait007