Optimistic vs Pessimistic Locking
Compare concurrency control patterns: Row-level exclusive locks (SELECT FOR UPDATE) vs version numbers (OCC / CAS), and SELECT FOR UPDATE SKIP LOCKED for job queues.
01.1. What is Pessimistic Locking?
Pessimistic Locking assumes that data conflicts will happen frequently. It prevents concurrency conflicts by acquiring an exclusive lock on the target record in the database before modifying it, blocking all other transactions until the lock is released:
sql-- Acquires exclusive write lock on row 10 BEGIN; SELECT * FROM inventory WHERE item_id = 10 FOR UPDATE; -- Perform business checks in application code... UPDATE inventory SET stock = stock - 1 WHERE item_id = 10; COMMIT; -- Lock is released upon commit
- Pros: 100% collision prevention; guaranteed transactional safety for high-contention rows.
- Cons: Holding database locks reduces concurrency, can cause lock wait timeouts, and risks deadlocks if locks are acquired in inconsistent order.
Pessimistic Locking (Database Row Locks) vs Optimistic Locking (Version Numbers) π
Pessimistic Locking (Database Row Locks) vs Optimistic Locking (Version Numbers) π
Pessimistic locking locks rows in the database (high contention / long blocking); Optimistic locking uses version numbers without locks, detecting conflicts on write.
Unlock Topic #49: Optimistic vs Pessimistic Locking
You are viewing a preview. The full in-depth engineering deep dive, interactive simulators, architecture flowcharts, and self-assessment quizzes for this topic are available with Pro or Lifetime Access.
Failure modes, high-throughput bottlenecks, and real FAANG implementation decisions.
Interactive system topology diagrams, live parameter simulators, and downloadable SVG charts.
Staff-level multiple-choice quiz questions with instant feedback and answer explanations.
Firebase Google authentication automatically syncs your completed topics and quiz scores.
How clear and staff-actionable was this system breakdown?