Locking Overview
The need of locking arises whenever there are shared resources in your environment. The resources shared by more than one users can cause data in-consistency. Problems like these give the impetus to the process of locking. Using the locking mechanism, the critical data set becomes fault tolerant resulting in providing the user with atomicity, consistency and isolation.
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 the consistency of data across the entire cache cluster for every update for the same data. Beside this, 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 the balance of $5000. User 1 wants to withdraw $2000 from the account and the 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 & 3000+1000 = 4000).
Now let's consider that both the 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.
Following diagram depicts the scenario visually.
Solution
In order to resolve this problem occurring for shared resources, two approaches can be used.
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.
Second approach is using a mechanism where a bank account is open for use by both users but the bank account version gets updated with respect to 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 of 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, item version updates itself.
See Also
Lock Items Exclusively (Pessimistic Locking)
Lock Items with Cache Item Versioning (Optimistic Locking)
Sync Cache With Database
Search Cache with SQL