Optimistic vs Pessimistic Locking in Cache
The need for Locking arises whenever there are shared resources in your environment. Resources shared by more than one user can cause data inconsistency. Problems like these can be best handled by Locking. Using the Locking mechanism, the critical data set becomes fault-tolerant resulting in providing the user with atomicity, consistency, and isolation.
Note
This feature is also available in NCache Professional.
NCache provides you with an efficient Locking mechanism for data synchronization and integrity within the cache store being updated by different parallel clients. NCache internal Locking guarantees data consistency across the entire cache cluster for every update of the same data. Additionally, NCache provides you with API to lock specific cache data, so that only a single thread or application can update or read it.
When to Use Locking
Problem Statement
Consider a scenario where there is a unique bank account used by two account holders. The account holds a balance of $5000. User 1 wants to withdraw $2000 from the account and User 2 wants to deposit $1000 to the account. Considering the given conditions, the account balance after performing these operations must be $4000 (5000-2000 = 3000 and 3000+1000 = 4000).
Now let's consider that both users want to perform this operation on the account at the same time and since there is no proper handling for the scenario, the following things happen:
User 1 checks the account balance and it shows $5000. He withdraws $2000 and the total balance becomes $3000.
User 2 checks the account balance and it shows $5000. He deposits $1000 and the total balance becomes $6000.
The following diagram depicts the scenario visually.
Solution
To resolve this shared resource problem, you can use two approaches.
The first approach is Locking the bank account for any further operation on it when one user is already performing any sort of operation on it.
The second approach is using a mechanism where a bank account is open for use by both users but the bank account version gets updated concerning the current time on every operation performed. This way the new version will be available for the user operations.
NCache provides both mechanisms of Locking known as Pessimistic and Optimistic Locking. They are further explained in successive chapters.
Types: Optimistic vs Pessimistic Locking
There are two types of Locking available with NCache:
Pessimistic Locking (Exclusive Locking)
Pessimistic Locking is a mechanism where an item is exclusively locked using aLockHandle
due to which the item remains inaccessible for other users while locked.Optimistic Locking (Cache Item versioning)
Optimistic Locking is a mechanism where item versioning is used to lock an item. Using this mechanism, an item is open for use by another user, but on every operation performed on an item, the item version updates itself.
See Also
.NET: Alachisoft.NCache.Runtime.Caching namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Node.js: Cache class.
Python: ncache.runtime.caching class.