Backend Mystery · Card #46
What is the difference between optimistic and pessimistic locking, and when should you use each?
**Optimistic locking** is a strategy that:
- Assumes conflicts are rare and don’t happen that often.
- Allows for concurrent data access.
- Checks if there are conflicts before committing.
- It’s best used in high-read, low-write scenarios.
**Pessimistic locking**, on the other hand, is a strategy that:
- Assumes conflicts to be very common.
- Locks data and prevents concurrent access.
- Holds these locks for the duration of a transaction.
- It’s best suited for high-write scenarios or when data integrity is critical.
databases