What is the difference between optimistic and pessimistic concurrency?
Imagine having a tool that can automatically detect JPA and Hibernate performance issues. Wouldn’t that be just awesome? Show
Well, Hypersistence Optimizer is that tool! And it works with Spring Boot, Spring Framework, Jakarta EE, Java EE, Quarkus, or Play Framework. So, enjoy spending your time on the things you love rather than fixing performance issues in your production system on a Saturday night! IntroductionIn this article, I’m going to explain what is the difference between optimistic and pessimistic locking, as well as when you should employ one or the other concurrency control strategies. ConflictsAt the Networking course in college, I learned that there are two ways of dealing with conflicts or collisions:
Dealing with conflicts is actually the same even when using a database system. We could allow the conflict to occur, but then we need to detect it upon committing our transaction, and that’s exactly how optimistic locking works. If the cost of retrying is high, we could try to avoid the conflict altogether via locking, which is the principle behind how pessimistic locking works. The Lost Update anomalyLet’s consider the Lost Update anomaly, which can happen on any database running under the Read Committed isolation level: The diagram above illustrates the following situation:
This transaction schedule is not Serializable because it’s neither equivalent to Alice’s reads and writes followed by Bob’s read and writes or Bob executing his transaction first followed by Alice executing her transaction right after. The reads and the writes are interleaves, and that’s why the Lost Update anomaly is generated. Pessimistic LockingPessimistic locking aims to avoid conflicts by using locking. In the diagram above, both Alice and Bob will acquire a read (shared) lock on the Because both Alice and Bob hold the read (shared) lock on the For this reason, Bob’s UPDATE blocks until Alice releases the shared lock she has acquired previously.
Optimistic LockingOptimistic Locking allows a conflict to occur, but it needs to detect it at write time. This can be done using either a physical or a logical clock. However, since logical clocks are superior to physical clocks when it comes to implementing a concurrency control mechanism, we are going to use a The So, when reading the Afterward, when Alice wants to change the Therefore, the method of the UPDATE So, the Lost Update is prevented by rolling back the subsequent transactions that are operating on state data.
Application-level transactionsRelational database systems have emerged in the late ’70s and early ’80s when clients would connect to a mainframe via a terminal. However, nowadays, that’s not the case when using a web browser. So, we no longer execute reads and writes in the context of the same database transaction, and Serializability is no longer sufficient to prevent a Lost Update in a long conversation. For instance, considering we have the following use case: Pessimistic locking would not help us in this case since Alice’s read and the write happen in different HTTP requests and database transactions. So, optimistic locking can help you prevent Lost Updates even when using application-level transactions that incorporate the user-think time as well.
ConclusionBoth pessimistic and optimistic locking are useful techniques. Pessimistic locking is suitable when the cost of retrying a transaction is very high or when contention is so large that many transactions would end up rolling back if optimistic locking were used. On the other hand, optimistic locking works even across multiple database transactions since it doesn’t rely on locking physical records. Follow @vlad_mihalcea Insert details about how the information is going to be processed DOWNLOAD NOW RelatedCategory: Database, Hibernate Tags: anomaly, lost updates, optimistic locking, pessimistic locking, Serializable ← JPA Default Fetch Plan SQL Server deadlock trace flags → 7 Comments on “Optimistic vs. Pessimistic Locking”
|