---
title: "What is the difference between optimistic locking and pessimistic locking?"  
description: "What is the difference between optimistic locking and pessimistic locking?"  
author: "Anubhav Sharma"  
published: 2026-05-14  
updated: 2026-06-05  
canonical: https://answers.mindstick.com/qa/116616/what-is-the-difference-between-optimistic-locking-and-pessimistic-locking  
category: "database"  
tags: ["database", "sql server"]  
reading_time: 1 minute  

---

# What is the difference between optimistic locking and pessimistic locking?

## Answers

### Answer by Ravi Vishwakarma

**Optimistic Locking** assumes conflicts are rare. [Multiple users](https://answers.mindstick.com/qa/96212/how-to-use-whatsapp-business-with-multiple-users) can read and update the same data concurrently. Before saving, the system checks whether the data has been modified by [someone else](https://yourviews.mindstick.com/story/3453/why-do-we-yawn-when-we-see-someone-else-yawning) (usually using a version number or [timestamp](https://www.mindstick.com/interview/626/what-is-the-physical-storage-length-of-each-of-the-following-db2-data-types-date-time-timestamp)). If a [conflict](https://yourviews.mindstick.com/view/81957/history-behind-azerbaijan-vs-armenia-conflict) is detected, the update is rejected and must be retried.

**Pessimistic Locking** assumes conflicts are likely. When a user accesses data for modification, the system locks it immediately, [preventing](https://www.mindstick.com/news/2244/issue-preventing-users-from-accessing-facebook-s-social-networking-platforms-has-been-resolved) others from modifying it until the lock is released.

### Example

- **Optimistic Locking:** Two users edit the same record. The first saves successfully; the second gets a version conflict error.
- **Pessimistic Locking:** The first user locks the record while editing, so the second user must wait until the lock is released.

### Key Difference

- **Optimistic Locking:** [Better performance](https://www.mindstick.com/forum/156505/where-do-i-put-my-javascript-code-in-my-file-for-better-performance) and [concurrency](https://www.mindstick.com/interview/23464/what-is-concurrency), but may require retries.
- **Pessimistic Locking:** Prevents conflicts upfront, but can reduce concurrency and cause blocking.

> **One-line [interview](https://yourviews.mindstick.com/view/83067/how-to-prepare-for-the-star-method-of-interview) answer:**\
> *"Optimistic locking detects conflicts at commit time using version checks, while pessimistic locking prevents conflicts by locking the resource before updates occur."*


---

Original Source: https://answers.mindstick.com/qa/116616/what-is-the-difference-between-optimistic-locking-and-pessimistic-locking

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
