Optimistic vs Pessimistic Locking
Reviewed by Gurusankar M. · Updated Aug 24, 2026
Short Answer
Optimistic: no lock; a version/timestamp column is checked on UPDATE and the txn retries if it changed (great for low-contention, high-read). Pessimistic: SELECT ... FOR UPDATE locks the row up front (best for high-contention hotspots), at the cost of blocking and deadlock risk.
On this page
Concept Made Simple
Coffee Chat Question
“Optimistic vs pessimistic locking — which do you use for concurrent updates?”
Remember It Faster
Mind Map Answer
- Optimistic
- version check on write, retry on conflict
- Pessimistic
- FOR UPDATE locks the row now
- Pick
- low contention → optimistic; hot row → pessimistic
Learn by Doing
Hands-on Keyboard
-- optimistic
UPDATE account SET balance = ?, version = version + 1
WHERE id = ? AND version = ?; -- 0 rows updated → conflict, retry
-- pessimistic
SELECT balance FROM account WHERE id = ? FOR UPDATE;Think Beyond the Expected
What If?
A single 'hot' inventory row gets thousands of concurrent decrements — optimistic or pessimistic?
On a hot row, optimistic locking causes a storm of version conflicts and retries (livelock-ish). Pessimistic FOR UPDATE (or an atomic UPDATE ... SET qty = qty - 1 WHERE qty > 0) serializes access cleanly — better for high contention.
Real World
JPA's @Version implements optimistic locking — the default for most entities; pessimistic FOR UPDATE is reserved for genuine hotspots like inventory/seat booking where conflicts are frequent.
Interviewer's Expectation
Keywords they're listening for:
- version-check + retry
- FOR UPDATE locks
- contention drives the choice
- @Version
- atomic UPDATE alternative
Common Mistakes
- Optimistic locking on a high-contention hot row
- Pessimistic locks held across long/external calls
- No retry handling for optimistic conflicts
Best Practices
- Optimistic for low-contention; pessimistic for hotspots
- Keep pessimistic locks short
- Prefer atomic conditional UPDATEs where possible
Follow-up Questions
- How does JPA @Version detect conflicts?
- Why can pessimistic locking deadlock?
- When is a single atomic UPDATE better than either?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Transactions (SQL) Interview question: "Optimistic vs pessimistic locking — which do you use for concurrent updates?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair