Optimistic Locking

Reject stale writes with a version precondition.

Problem

Concurrent editors can silently overwrite one another.

When to use it

  • Conflicts are uncommon
  • Users can merge or retry

When not to use it

  • A hot counter has constant contention
  • The invariant spans many changing rows

Minimal example

UPDATE tasks SET ..., version=8 WHERE id=? AND version=7; zero rows means 412 Precondition Failed.

Tradeoffs

  • Clients need conflict UX
  • Retries must recompute from fresh state