Optimistic vs. Pessimistic Concurrency Control
Explore optimistic and pessimistic concurrency control techniques that help maintain data integrity during simultaneous transactions. Understand how optimistic control uses versioning and retries for low contention, while pessimistic control relies on exclusive locks for high contention environments. Discover the advantages, disadvantages, and typical use cases of each approach.
Optimistic concurrency control
This section covers optimistic concurrency control and its applications.
Introduction
Optimistic concurrency control is a type of concurrency control that assumes conflicts rarely occur. Multiple transactions run concurrently without taking a lock on the shared data resource. Before committing the data resource, however, the transaction verifies no other concurrently running transaction has modified the data during that interval. These are the actions taken during an optimistic concurrency control:
If no transaction has modified the data, the transaction commits persisting the data durably.
If a different transaction modifies the data during that interval, the database rolls back the transaction, and the application can retry the transaction.
Optimistic concurrency control is helpful in environments where data contention and conflicts are very low. When the data contention is low, the transactions can complete without taking an expensive lock on the shared data resource, which leads to higher throughput. But if the data contention is high, retrying transactions multiple times can lead to starvation and significantly impacts performance.
Some practical examples of optimistic concurrency control include collaborative editing and document versioning.
Let’s look ...