Optimistic locking assumes conflicts are rare. It reads without taking a lock, then makes the update conditional on the data being unchanged, usually by checking a version number in the WHERE clause; if another writer got there first, the update affects zero rows and the application retries or reports a conflict. Pessimistic locking assumes conflicts are likely. It locks the rows before changing them, for example with SELECT ... FOR UPDATE, and other writers wait until the lock is released at commit. Optimistic locking is cheaper when contention is low and fits flows with user think time, because nothing is held while the user edits. Pessimistic locking does better when many writers fight over the same few rows, because they queue instead of repeatedly retrying.
Two people try to change the same row at the same moment. There are two classic ways to stop one of them from silently overwriting the other. Optimistic locking takes no lock at all. Both writers read the row at version seven. Each update says: set the new balance and version eight, but only where the version is still seven. The first one lands. The second matches zero rows, so it re-reads and retries. Pessimistic locking locks first. Select for update grabs the row, and every other writer waits in line until that transaction commits. Which one wins depends on contention. When conflicts are rare, optimistic is cheap, and nothing is held while a user is still typing. But on a hot row, optimistic writers keep colliding and retrying, and the work is wasted. A queue of lock waiters makes steady progress instead. And for the last seat in a booking system, one atomic conditional update often beats both: mark it held only if it's still available, and exactly one caller wins.
| Aspect | Optimistic locking | Pessimistic locking |
|---|---|---|
| Assumption | Conflicts are rare | Conflicts are likely |
| When a conflict is handled | At write time: the version check fails | Up front: other writers block |
| Mechanism | Version column, compare-and-set, ETag with If-Match, Redis WATCH | SELECT ... FOR UPDATE, row locks, advisory locks, a distributed lock with a lease |
| Under low contention | Cheap: no lock waits | Pays for locks that were not needed |
| Under high contention | Many failed attempts and wasted work | Writers queue but steadily make progress |
| Deadlock risk | None from the scheme itself | Real; needs consistent lock order or timeouts |
| Long edits by a user | A good fit: nothing is held while they type | Not acceptable: locks would be held for minutes |
| Examples | Wiki edits, profile updates, DynamoDB conditional writes, Kubernetes resourceVersion | Debiting a hot account, a short critical section on inventory |
Use optimistic locking when
Two writers rarely touch the same record at the same time.
Use pessimistic locking when
Many writers hit the same row, and retries would waste more than waiting.
Optimistic: read the row with its version, say 7. Later, run UPDATE accounts SET balance = ?, version = 8 WHERE id = ? AND version = 7. If the update reports one row changed, you won. If it reports zero, someone else changed the row; re-read and decide whether to retry.
Pessimistic: inside a transaction, run SELECT balance FROM accounts WHERE id = ? FOR UPDATE. The row is now locked until commit or rollback. Do the check, run the update, and commit. Concurrent transactions that try to lock the same row wait in line.
For seat or room booking, a single conditional update usually beats both: UPDATE seats SET status = 'held', holder = ? WHERE id = ? AND status = 'available'. It is atomic, holds no lock beyond the statement, and one caller wins. Add a hold with an expiry so abandoned checkouts release the seat.
Across services there is no database row lock to lean on. Distributed locks need leases so a crashed holder does not block forever, and fencing tokens so a holder that paused past its lease cannot overwrite newer work.
This comes up whenever two users can change the same thing: the last seat, the last room, the last unit in a flash sale, or the same wallet balance. Interviewers want to hear which approach you pick, why it fits the contention level, and what the user sees when they lose the race.
“Most edits to a listing rarely conflict, so I would use optimistic locking with a version column and return a conflict if the version changed. The inventory decrement in the flash sale is the opposite case: every request hits the same row, so I would use an atomic conditional decrement, and put a queue in front so the database sees a steady rate rather than a stampede.”
No lock is held. It is a check at write time that the data has not changed since you read it, so it is often called optimistic concurrency control.
Not exactly. MVCC lets readers see a snapshot without blocking writers. Writers in databases such as PostgreSQL still take row locks, and higher isolation levels detect conflicting writes and abort one transaction.
It turns on optimistic locking: the entity carries a version field, every update checks and increments it, and a mismatch raises an exception instead of silently overwriting.
Yes. A common pattern uses optimistic checks for ordinary edits and pessimistic locks or atomic conditional updates on the few hot records where contention is high.
Lost updates, write skew and which level prevents what.
What FOR UPDATE really takes and how waiters queue.
WATCH, MULTI and check-and-set without row locks.
Locks that survive crashes and pauses across machines.
How a database implements pessimistic locking inside.
Seat holds, expiry and contention on popular events.
Extreme contention on a small inventory.
Room holds and double-booking prevention.
Lost updates on balances.
Leases, fencing tokens and failure.
Knowing the trade-off is half of it
Practise saying it under follow-up questions: a timed system design interview with an AI interviewer that probes your choices and grades against a rubric.