The Desi Architect
Notes / Messaging and Correctness / Optimistic vs Pessimistic Locking
Track 4 · Messaging and Correctness

Optimistic vs Pessimistic Locking

Two requests read the same row, both decide it is safe, both write. Row locks make the second one wait. Version checks make the second one fail and retry. Which one you pick depends on how often people collide and how long you hold the lock.

Guide20 min read
On this page
  1. The last packet of Amul butter
  2. The simple version
  3. Why plain READ COMMITTED does not save you
  4. Optimistic: check the version when you write
  5. Pessimistic: lock, look, then change
  6. Choosing: the same race, two ways
  7. Going deeper: where locks hurt
  8. When the lock lives outside the database
  9. Decision table
  10. Common traps