Compare locking strategies.
Skip to solutionKEEP THE
mediumSystem Design
What is the difference between optimistic and pessimistic locking?
451 views
01
Understand the problem
locking
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Step 1: Outline use cases and constraints
Gather requirements and scope the problem. Ask questions to clarify use cases and constraints. Discuss assumptions.
Use cases
We'll scope the problem to handle only the following use cases
- User needs to understand
lockingto make architecture decisions
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Optimistic update (version check)
-- Optimistic locking with a version column (compare-and-set).
-- 1. Read the current row + its version:
SELECT id, stock, version FROM products WHERE id = 42; -- version = 7
-- 2. Write only if nobody changed it in the meantime:
UPDATE products
SET stock = stock - 1, version = version + 1
WHERE id = 42 AND version = 7;
-- If 0 rows were updated, someone else won the race -> re-read and retry.05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 31 of 99 decoded in the System Design track. One more won't hurt.