Optimistic concurrency control explained

Optimistic concurrency control (OCC), also known as optimistic locking, is a non-locking 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 in 1979 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:

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.

Examples

See also

External links

Notes and References

  1. Book: Johnson, Rohit . Expert One-on-One J2EE Design and Development . Wrox Press . 2003 . 978-0-7645-4385-2 . Common Data Access Issues . http://learning.infocollections.com/ebook%202/Computer/Programming/Java/Expert_One-on-One_J2EE_Design_and_Development/6266final/LiB0080.html . https://web.archive.org/web/20111008203709/http://learning.infocollections.com/ebook%202/Computer/Programming/Java/Expert_One-on-One_J2EE_Design_and_Development/6266final/LiB0080.html . 8 October 2011.
  2. News: On Optimistic Methods for Concurrency Control . J. T. Robinson . H. T. Kung . ACM Transactions on Database Systems . 1981 . https://web.archive.org/web/20190831230313/https://apps.dtic.mil/dtic/tr/fulltext/u2/a081452.pdf. live. August 31, 2019.
  3. Web site: Editing the Web - Detecting the Lost Update Problem Using Unreserved Checkout . W3C Note . 10 May 1999.
  4. [w:Help:Edit conflict|Help:Edit conflict]
  5. Web site: Bugzilla: FAQ: Administrative Questions . MozillaWiki . 11 April 2012.
  6. Web site: Module ActiveRecord::Locking . Rails Framework Documentation.
  7. Web site: Object Relational Mapping (GORM) . Grails Framework Documentation . dead . https://web.archive.org/web/20140815173309/http://grails.org/doc/1.0.x/guide/single.html#5.3.5%20Pessimistic%20and%20Optimistic%20Locking . 2014-08-15 .
  8. Web site: Transaction Processing . GT.M Programmers Guide UNIX Edition.
  9. Web site: Handling Concurrency Conflicts . Entity Framework documentation hub . 5 July 2023.
  10. Web site: Transaction Concurrency - Optimistic Concurrency Control . Mimer Developers - Features . 22 Dec 2023.
  11. Web site: The Datastore . What Is Google App Engine? . 27 August 2010.
  12. Web site: Updating Parts of Documents. 2018-06-28.
  13. Web site: Optimistic concurrency control . Elastic . 2024-02-05.
  14. Web site: Technical Overview . Apache CouchDB Documentation . 2024-02-06.
  15. Web site: Transactions - MonetDB . 16 January 2013.
  16. Web site: Transactions in Redis .
  17. Web site: Working with Items and Attributes - Conditional Writes. 2 November 2020.
  18. Web site: API Overview - Resource Operations. 3 November 2020.
  19. Web site: Yugabyte. Team. Explicit locking YugabyteDB Docs. 2022-01-04. docs.yugabyte.com. en-us.