Difficulty: Advanced
What is the difference between optimistic and pessimistic locking, and how do you decide which one to use?
Both strategies prevent the lost update problem, where two users read the same row and the second write silently overwrites the first. Pessimistic locking locks the row when you read it (SELECT ... FOR UPDATE) so nobody else can change it until you commit. Optimistic locking takes no lock; instead it checks at write time that the row has not changed since you read it, usually with a version column, and fails the update if it has.
Two support agents open ticket 501 at version 3. Agent A changes the status and saves. Agent B, still looking at version 3, changes the priority and saves, overwriting A's status change. Without some form of concurrency control, A's work disappears with no error.
SELECT * FROM seats WHERE id = 7 FOR UPDATE acquires a row lock.COMMIT, which releases the lock.Useful options in PostgreSQL and MySQL 8.0+:
FOR UPDATE NOWAIT fails immediately instead of waiting.FOR UPDATE SKIP LOCKED skips rows already locked, which is the standard way to build a job queue where many workers pull tasks.FOR SHARE allows other readers to also lock for share but blocks writers.version (or updated_at).UPDATE ... SET ..., version = version + 1 WHERE id = ? AND version = ?.ORMs support this directly: JPA and Hibernate use @Version, Entity Framework uses concurrency tokens, and Django needs a manual filter on the version. HTTP APIs expose the same idea with ETag and If-Match headers.
| Factor | Optimistic | Pessimistic |
|---|---|---|
| Contention | Low, conflicts are rare | High, many writers on the same rows |
| Holds locks | No | Yes, until commit |
| Conflict handling | Retry or show error | Waiting, possible deadlocks |
| Long user think time | Works well | Bad, locks held too long |
| Example | Editing a profile or a wiki page | Booking the last seat, debiting an account |
Rule of thumb: if conflicts are rare and retrying is cheap, go optimistic. If conflicts are frequent or a retry is expensive or user-visible (flash sales, ticket booking for a popular show), go pessimistic, and keep the transaction short.
UPDATE stock SET qty = qty - 1 WHERE id = ? AND qty > 0 avoids both strategies.-- Read: remember the version
SELECT id, status, priority, version FROM tickets WHERE id = 501;
-- returns version = 3
-- Write: only succeeds if nobody else changed it
UPDATE tickets
SET priority = 'HIGH', version = version + 1
WHERE id = 501 AND version = 3;
-- App checks affected rows: 0 means conflict, reload and retry
No lock is held between the read and the write. The WHERE clause on version turns the update into a compare-and-swap.
BEGIN;
SELECT id, status FROM seats
WHERE show_id = 9 AND seat_no = 'F12'
FOR UPDATE NOWAIT; -- fail fast if someone else holds it
UPDATE seats SET status = 'BOOKED', user_id = 42
WHERE show_id = 9 AND seat_no = 'F12' AND status = 'AVAILABLE';
COMMIT;
The row is locked from the SELECT until COMMIT, so two users cannot both book F12. NOWAIT returns an error instead of making the second user wait.
Optimistic Locking, Pessimistic Locking, Version Column, SELECT FOR UPDATE, Lost Update