Locks are essential to maintain data consistency and integrity in multi-user environments. They prevent simultaneous modifications that can lead to data inconsistencies.
What is Pessimistic Lock
Pessimistic locking assumes conflicts will occur and locks the data before any changes are made. It prevents other users from accessing and updating the data until the lock is released.
Pessimistic locking model prevents simultaneous updates to records. As soon as one user starts to update a record, a lock is placed on it. Other users who attempt to update this record are informed that another user has an update in progress. The other users must wait until the first user has finished committing their changes, thereby releasing the record lock. Only then can another user make changes based on the previous user’s changes.
Advantages of Pessimistic
- Prevents Conflicts: An advantage of the pessimistic locking model is that it avoids the issue of conflict resolution by preventing conflicts. Updates are serialized and each subsequent update starts with the committed record changes from the previous user.
- High Reliability: It is useful in situations with a high level of transaction contention due to the locks that are important in maintaining consistency of the data.
Disadvantages of Pessimistic
- Deadlock Risk: So, when dealing with locks, it must be understood that they can lead to deadlocks and thus make programming even more complicated.
- Lower Concurrency: Locks decrease the system’s concurrency since transactions may have to wait in order to obtain the right to use the required locks.
When to use Pessimistic
- Pessimistic locking is a useful approach when subsequent updates can be delayed until a previous update is completed. This usually implies that updates occur in a short time interval.
What is Optimistic Lock
Optimistic locking assumes conflicts are rare. It allows multiple users to access data simultaneously and checks for conflicts when changes are committed. If a conflict is detected, the operation is rolled back.
Optimistic locking model, also referred to as optimistic concurrency control, is a concurrency control method used in relational databases that does not use record locking. Optimistic locking allows multiple users to attempt to update the same record without informing the users that others are also attempting to update the record. The record changes are validated only when the record is committed. If one user successfully updates the record, the other users attempting to commit their concurrent updates are informed that a conflict exists.
Advantages of Pessimistic
- An advantage of the optimistic locking model is that it avoids the overhead of locking a record for the duration of the action. If there are no simultaneous updates, then this model provides fast updates.
Disadvantages of Pessimistic
- Using an optimistic approach for concurrency control can be very expensive as it needs to be rolled back.
When to use Pessimistic
- Optimistic locking is a useful approach when concurrent record updates are expected to be infrequent or the locking overhead is high. In the Rational ClearQuest implementation of optimistic locking, when multiple users edit a record concurrently, after one user’s changes are committed, the other users’ changes are rejected and data conflicts must be saved and manually merged.