How do optimistic concurrency control techniques differ from other concurrency control techniques?

Concurrency control is the management of contention for data resources. A concurrency control scheme is considered pessimistic when it locks a given resource early in the data access transaction and does not release it until the transaction is closed. A concurrency control scheme is considered optimistic when locks are acquired and released over a short period of time at the end of a transaction.

The objective of optimistic concurrency is to minimize the time that a given resource is unavailable for use by other transactions. This is especially important with long-running transactions, which under a pessimistic scheme would lock up a resource for unacceptably long periods of time.

Under an optimistic scheme, locks are obtained immediately before a read operation and released immediately after. Update locks are obtained immediately before an update operation and held until the end of the transaction.

To enable optimistic concurrency, this product uses an overqualified update scheme to test if the underlying data source has been updated by another transaction since the beginning of the current transaction. With this scheme, the columns marked for update and their original values are added explicitly through a WHERE clause in the UPDATE statement so that the statement fails if the underlying column values have been changed. As a result, this scheme can provide column-level concurrency control; pessimistic schemes can control concurrency at the row level only.

Optimistic schemes typically perform this type of test only at the end of a transaction. If the underlying columns have not been updated since the beginning of the transaction, pending updates to container-managed persistence fields are committed and the locks are released. If locks cannot be acquired or if some other transaction has updated the columns since the beginning of the current transaction, the transaction is rolled back: All work performed within the transaction is lost.

Pessimistic and optimistic concurrency schemes require different transaction isolation levels. Enterprise beans that participate in the same transaction and require different concurrency control schemes cannot operate on the same underlying data connection.

Best practices: Whether to use optimistic concurrency depends on the type of transaction. Transactions with a high penalty for failure might be better managed with a pessimistic scheme. A high-penalty transaction is one for which recovery is risky or resource-intensive. For low-penalty transactions, it is often worth the risk of failure to gain efficiency through the use of an optimistic scheme. In general, optimistic concurrency is more efficient when update collisions are expected to be infrequent; pessimistic concurrency is more efficient when update collisions are expected to occur often.

Get full access to Database Systems: Concepts, Design and Applications and 60K+ other titles, with free 10-day trial of O'Reilly.

There's also live online events, interactive content, certification prep materials, and more.

Optimistic concurrency control (OCC), also known as optimistic locking, is a concurrency control method applied to transactional systems such as relational database management systems and software transactional memory. OCC assumes that multiple transactions can frequently complete without interfering with each other. While running, transactions use data resources without acquiring locks on those resources. Before committing, each transaction verifies that no other transaction has modified the data it has read. If the check reveals conflicting modifications, the committing transaction rolls back and can be restarted.[1] Optimistic concurrency control was first proposed by H. T. Kung and John T. Robinson.[2]

OCC is generally used in environments with low data contention. When conflicts are rare, transactions can complete without the expense of managing locks and without having transactions wait for other transactions' locks to clear, leading to higher throughput than other concurrency control methods. However, if contention for data resources is frequent, the cost of repeatedly restarting transactions hurts performance significantly, in which case other concurrency control methods may be better suited. However, locking-based ("pessimistic") methods also can deliver poor performance because locking can drastically limit effective concurrency even when deadlocks are avoided.

Phases of optimistic concurrency control

Optimistic concurrency control transactions involve these phases:[2]

  • Begin: Record a timestamp marking the transaction's beginning.
  • Modify: Read database values, and tentatively write changes.
  • Validate: Check whether other transactions have modified data that this transaction has used (read or written). This includes transactions that completed after this transaction's start time, and optionally, transactions that are still active at validation time.
  • Commit/Rollback: If there is no conflict, make all changes take effect. If there is a conflict, resolve it, typically by aborting the transaction, although other resolution schemes are possible. Care must be taken to avoid a time-of-check to time-of-use bug, particularly if this phase and the previous one are not performed as a single atomic operation.

Web usage

The stateless nature of HTTP makes locking infeasible for web user interfaces. It is common for a user to start editing a record, then leave without following a "cancel" or "logout" link. If locking is used, other users who attempt to edit the same record must wait until the first user's lock times out.

HTTP does provide a form of built-in OCC. The response to an initial GET request can include an ETag for subsequent PUT requests to use in the If-Match header. Any PUT requests with an out-of-date ETag in the If-Match header can then be rejected.[3]

Some database management systems offer OCC natively, without requiring special application code. For others, the application can implement an OCC layer outside of the database, and avoid waiting or silently overwriting records. In such cases, the form may include a hidden field with the record's original content, a timestamp, a sequence number, or an opaque token. On submit, this is compared against the database. If it differs, the conflict resolution algorithm is invoked.


  • MediaWiki's edit pages use OCC.[4]
  • Bugzilla uses OCC; edit conflicts are called "mid-air collisions".[5]
  • The Ruby on Rails framework has an API for OCC.[6]
  • The Grails framework uses OCC in its default conventions.[7]
  • The GT.M database engine uses OCC for managing transactions[8] (even single updates are treated as mini-transactions).
  • Microsoft's Entity Framework (including Code-First) has built-in support for OCC based on a binary timestamp value.[9]
  • Mimer SQL is a DBMS that only implements optimistic concurrency control.[10]
  • Google App Engine data store uses OCC.[11]
  • The Apache Solr search engine supports OCC via the _version_ field.[12]
  • The Elasticsearch search engine supports OCC via the version attribute.[13]
  • CouchDB implements OCC through document revisions.[14]
  • The MonetDB column-oriented database management system's transaction management scheme is based on OCC.[15]
  • Most implementations of software transactional memory use OCC.[citation needed]
  • Redis provides OCC through WATCH command.[16]
  • Firebird uses Multi-generational architecture as an implementation of OCC for data management.[citation needed]
  • DynamoDB uses conditional update as an implementation of OCC.[17]
  • Kubernetes uses OCC when updating resources.[18]
  • YugabyteDB is a cloud-native database that primarily uses OCC.[19]

See also

  • Server Message Block#Opportunistic locking


  1. ^ Johnson, Rohit (2003). "Common Data Access Issues". Expert One-on-One J2EE Design and Development. Wrox Press. ISBN 978-0-7645-4385-2. Archived from the original on 8 October 2011.
  2. ^ a b H. T. Kung, J. T. Robinson (1981). "On Optimistic Methods for Concurrency Control" (PDF). ACM Transactions on Database Systems. Archived (PDF) from the original on August 31, 2019.
  3. ^ "Editing the Web - Detecting the Lost Update Problem Using Unreserved Checkout". W3C Note. 10 May 1999.
  4. ^ Help:Edit conflict
  5. ^ "Bugzilla: FAQ: Administrative Questions". MozillaWiki. 11 April 2012.
  6. ^ "Module ActiveRecord::Locking". Rails Framework Documentation.
  7. ^ "Object Relational Mapping (GORM)". Grails Framework Documentation. Archived from the original on 2014-08-15.
  8. ^ "Transaction Processing". GT.M Programmers Guide UNIX Edition.
  9. ^ "Tip 19 – How to use Optimistic Concurrency with the Entity Framework". MSDN Blogs. 19 May 2009.
    • Most revision control systems support the "merge" model for concurrency, which is OCC.
  10. ^ "Transaction Concurrency - Optimistic Concurrency Control". Mimer Developers - Features. 26 February 2010. Archived from the original on 21 March 2013. Retrieved 6 May 2013.
  11. ^ "The Datastore". What Is Google App Engine?. 27 August 2010.
  12. ^ "Updating Parts of Documents". Retrieved 2018-06-28.
  13. ^ "Elasticsearch - Guide - Index API". Elasticsearch Guide. 22 March 2012.
  14. ^ "Couchdb Wiki - Document_revisions". Archived from the original on 4 February 2017.
  15. ^ "Transactions - MonetDB". 16 January 2013.
  16. ^ "Transactions in Redis".
  17. ^ "Working with Items and Attributes - Conditional Writes". Retrieved 2 November 2020.
  18. ^ "API Overview - Resource Operations". Retrieved 3 November 2020.
  19. ^ Yugabyte, Team. "Explicit locking | YugabyteDB Docs". Retrieved 2022-01-04.

  • Kung, H. T.; John T. Robinson (June 1981). "On optimistic methods for concurrency control". ACM Transactions on Database Systems. 6 (2): 213–226. CiteSeerX doi:10.1145/319566.319567.
  • Enterprise JavaBeans, 3.0, By Bill Burke, Richard Monson-Haefel, Chapter 16. Transactions, Section 16.3.5. Optimistic Locking, Publisher: O'Reilly, Pub Date: May 16, 2006,Print ISBN 0-596-00978-X,
  • Hollmann, Andreas (May 2009). "Multi-Isolation: Virtues and Limitations" (PDF). Multi-Isolation (what is between pessimistic and optimistic locking). 01069 Gutzkovstr. 30/F301.2, Dresden: Happy-Guys Software GbR. p. 8. Retrieved 2013-05-16.{{cite conference}}: CS1 maint: location (link)[permanent dead link]

Retrieved from ""