Optimistic vs. Pessimistic Concurrency Control
Learn to differentiate between optimistic and pessimistic concurrency control.
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 at an illustration that shows how optimistic concurrency control works.
Get hands-on with 1400+ tech skills courses.