Optimistic Locking
- Definition
- Allow concurrent processes to prepare a change to the same data, but check for consistency before the commit.
How does it work?
Martin Fowler: "Often a business transaction executes across a series of system transactions. Once outside the confines of a single system transaction, we can't depend on our database manager alone to ensure that the business transaction will leave the record data in a consistent state. Data integrity is at risk once two sessions begin to work on the same records and lost updates are quite possible. Also, with one session editing data that another is reading an inconsistent read becomes likely. Optimistic Offline Lock solves this problem by validating that the changes about to be committed by one session don't conflict with the changes of another session. A successful pre-commit validation is, in a sense, obtaining a lock indi-cating it's okay to go ahead with the changes to the record data. So long as the validation and the updates occur within a single system transaction the business transaction will display consistency."
Examples
- Git pre-commit hook
When should you use it?
- When the chance of change conflicts is low or the price of conflicts is low.
Problems
- It a conflict occurs and the changes both processes made can't be reconciled, one change needs to fail.