Optimistic Locking
Definition
Optimistic locking is a concurrency control strategy that assumes multiple transactions can complete without affecting each other. Instead of locking a record, it checks for conflicts at the time of commit. If a conflict is detected, the transaction is rolled back.
Why It Matters
Optimistic locking allows for much higher concurrency than pessimistic locking, as it doesn't lock resources. It is well-suited for environments where conflicts are rare.
Contextual Example
Two users load the same product page to edit its description. User A saves their changes. When User B tries to save their changes, the system detects that the data has been modified since they loaded it (using a version number or timestamp). User B's update fails, and they are asked to reload the data and try again.
Common Misunderstandings
- This strategy is often implemented by adding a `version` column to the database table.
- It is "optimistic" because it hopes that conflicts will not happen.