Blog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2025 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Databases
Databaseshard

Optimistic vs pessimistic locking — what’s the difference?

Tags
#locking#optimistic#pessimistic#concurrency
Back to categoryPractice quiz

Answer

Optimistic locking assumes conflicts are rare: you update with a version/timestamp check and retry on conflict. Pessimistic locking locks rows upfront (e.g., `SELECT ... FOR UPDATE`) to prevent others from changing them.

SELECT *
FROM accounts
WHERE id = 1
FOR UPDATE;

Related questions

Databases
What is a deadlock in a database and how do you reduce it?
#deadlock#locking#transactions
Databases
What is an isolation level (and why do we care)?
#transactions#isolation#concurrency
PostgreSQL
MVCC in Postgres: why don’t readers block writers?
#postgres#mvcc#concurrency
Java
HashMap vs ConcurrentHashMap: when should you use each?
#java#collections#concurrency
Java
`synchronized` vs `ReentrantLock`: what are the differences?
#java#concurrency#locks
MongoDB
MongoDB transaction write conflicts: why do they happen and how should you handle them?
#mongo#transactions#concurrency