Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 April Database Theory: What is Optimistic Locking and Pessimistic Locking?

Database Theory: What is Optimistic Locking and Pessimistic Locking?

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Pessimistic Locking:

This is a standard locking like an Exclusive lock or Shared lock. The reader is blocked by writer and writer is blocked by the reader.

SQL Server Isolation levels like Read Committed, Repeatable Read, Serializable are mostly doing Pessimistic Locking.

It locks the transaction whenever something is going wrong and it puts the transaction into the blocking queue.
Pessimistic locking allows you to access live and committed data, there are no chances for accessing dirty data.

The big OLTP system like banking system or finance system are always preferred to use Pessimistic Locking because data accuracy is required for both reader and writer.

Yes, locking is an extra overhead in a big system, but a few types of the application required for accuracy.

Optimistic Locking:

It is also called as row versioning, and it never blocks any transaction.

The MVCC architecture is most popular now a day and it depends on Optimistic Locking concept.
The RDBMS like PostgreSQL and MySQL InnoDB is fully based on MVCC.
Microsoft SQL Server has also Snapshot Isolation which is one type of Optimistic Locking.

The Reader never blocks the writers and writers never blocks the readers.

Internally, it is managing different row versions, so if the reader is reading one version of data and at the same time writer can also update the same data by creating a new version of that data, Next time readers can read new version of data and old version of data is marked as dead tuple.

There are 70% chance to get last committed version of data while accessing data using Optimistic locking, but it gives fast access because it never creates a dependency between transactions.

Generally, most of the web and mobile applications are fine with the last committed version of data.

Apr 7, 2016Anvesh Patel
Database Theory: What is Timeout, Lock, Block, and Deadlock in a Database System?SQL Server: T-SQL script to generate a DeadLock in a Database
Comments: 5
  1. John
    May 13, 2016 at 6:35 pm

    Awesome write-up. I am a regular visitor of your site and appreciate you taking the time to maintain the excellent site. I’ll be a regular visitor for a long time.

  2. Salli
    July 4, 2017 at 11:38 pm

    ohh great, already doing lots of documentation on DBA task but this is also i enjoyed lot

  3. Frache
    June 23, 2018 at 9:34 am

    explained very easily anvesh, i following you since the beginning.

  4. tibec
    July 14, 2018 at 10:17 am

    nice article and vary easy explanation, but if possible, share some formula to calculate sla for dba work

  5. George
    April 12, 2019 at 8:20 am

    “SQL Server Isolation levels like Read Committed, Repeatable Read, Serializable are mostly doing Pessimistic Locking.” This is not also correct. Only serializable uses pessimistic locking. Read-committed and Repeatable Read use Optimistic locking .

Anvesh Patel
Anvesh Patel

Database Engineer

April 7, 2016 Database TheoryAnvesh Patel, database, database concept, database research and development, database standards, Database Theory, database topic, dbrnd, Optimistic Locking, Pessimistic Locking, RDBMS
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....