MediumExperience level: 3-5 years · 8-15 years1 min read

Optimistic vs Pessimistic Locking

Reviewed by Gurusankar M. · Updated Aug 24, 2026

Asked inAmazonMicrosoftbankingDeloitte
#optimistic locking#pessimistic locking#version#select for update#concurrency
Report issue

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

sql
-- 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

  1. How does JPA @Version detect conflicts?
  2. Why can pessimistic locking deadlock?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.