Optimistic locking
Detecting conflicting edits with a version number: a save succeeds only if the record has not changed since it was read.
Two people open the same order; both save. Without protection, the second save silently overwrites the first. With a version column, the update says “where id = 7 and version = 3”, and the second save finds version 4 and fails, so the app can show the conflict. Pessimistic locking (SELECT FOR UPDATE) blocks the second writer instead, which suits frequent conflicts at the cost of waiting and deadlock risk.
Related terms
- Aggregate (DDD): A cluster of objects changed as one unit, with a single root that enforces the business rules and marks the boundary of one transaction.
- Idempotency (idempotency key): An operation is idempotent if doing it twice has the same effect as doing it once. Retries become safe.
- Fencing token (distributed lock): A number that grows with each lock grant; the protected resource rejects writes with an older number, so a stale lock holder cannot do damage.
