Pessimistic Locking

Definition
Don't allow concurrent processes to change the same data at the same time.
Optimistic Locking

How does it work?

Martin Fowler: "Pessimistic Offline Lock prevents conflicts by avoiding them altogether. It forces a business transaction to acquire a lock on a piece of data before it starts to use it, so that, most of the time, once you begin a business transaction you can be pretty sure you'll complete it without being bounced by concurrency control."

Examples

  • Database locks
  • Mutexes in code

When should you use it?

  • When change conflicts can't be reconciled and conflicts lead to data loss or data corruption.

Problems

  • It a conflict occurs and the changes both processes made can't be reconciliated, one change needs to fail.

Links