Optimistic concurrency control

From HandWiki
Short description: Concurrency control method

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] The two main versions of OCC is timestamp OCC and validation OCC. Validation OCC is similar to the timestamp system except that data is recorded about the actions of transactions rather than data about database objects.[3]

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.[4]

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

References

  1. Johnson, Rohit (2003). "Common Data Access Issues". Expert One-on-One J2EE Design and Development. Wrox Press. ISBN 978-0-7645-4385-2. http://learning.infocollections.com/ebook%202/Computer/Programming/Java/Expert_One-on-One_J2EE_Design_and_Development/6266final/LiB0080.html. 
  2. 2.0 2.1 H. T. Kung, J. T. Robinson (1981). "On Optimistic Methods for Concurrency Control". ACM Transactions on Database Systems. https://apps.dtic.mil/dtic/tr/fulltext/u2/a081452.pdf. 
  3. "Validation Based Protocol in DBMS" (in en-US). 2019-06-20. https://www.geeksforgeeks.org/validation-based-protocol-in-dbms/. 
  4. "Editing the Web - Detecting the Lost Update Problem Using Unreserved Checkout". W3C Note. 10 May 1999. http://www.w3.org/1999/04/Editing/. 
  5. Help:Edit conflict
  6. "Bugzilla: FAQ: Administrative Questions". MozillaWiki. 11 April 2012. https://wiki.mozilla.org/Bugzilla:FAQ#Does_Bugzilla_provide_record_locking_when_there_is_simultaneous_access_to_the_same_bug.3F_Does_the_second_person_get_a_notice_that_the_bug_is_in_use_or_how_are_they_notified.3F. 
  7. "Module ActiveRecord::Locking". Rails Framework Documentation. http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html. 
  8. "Object Relational Mapping (GORM)". Grails Framework Documentation. http://grails.org/doc/1.0.x/guide/single.html#5.3.5%20Pessimistic%20and%20Optimistic%20Locking. 
  9. "Transaction Processing". GT.M Programmers Guide UNIX Edition. http://tinco.pair.com/bhaskar/gtm/doc/books/pg/UNIX_manual/ch05s17.html. 
  10. "Tip 19 – How to use Optimistic Concurrency with the Entity Framework". MSDN Blogs. 19 May 2009. http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx. 
    • Most revision control systems support the "merge" model for concurrency, which is OCC.
  11. "Transaction Concurrency - Optimistic Concurrency Control". Mimer Developers - Features. https://developer.mimer.com/article/transaction-concurrency-optimistic-concurrency-control/. 
  12. "The Datastore". What Is Google App Engine?. 27 August 2010. http://code.google.com/appengine/docs/whatisgoogleappengine.html. 
  13. "Updating Parts of Documents". https://lucene.apache.org/solr/guide/6_6/updating-parts-of-documents.html. 
  14. "Elasticsearch - Guide - Index API". Elasticsearch Guide. 22 March 2012. http://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#index-versioning. 
  15. "Couchdb Wiki - Document_revisions". https://wiki.apache.org/couchdb/Document_revisions. 
  16. "Transactions - MonetDB". 16 January 2013. http://www.monetdb.org/Documentation/Manuals/SQLreference/Transactions. 
  17. "Transactions in Redis". http://redis.io/topics/transactions. 
  18. "Working with Items and Attributes - Conditional Writes". https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.ConditionalUpdate. 
  19. "API Overview - Resource Operations". https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#resource-operations-update. 
  20. Yugabyte, Team. "Explicit locking | YugabyteDB Docs" (in en-us). https://docs.yugabyte.com/latest/architecture/transactions/explicit-locking/. 

External links

  • Kung, H. T.; John T. Robinson (June 1981). "On optimistic methods for concurrency control". ACM Transactions on Database Systems 6 (2): 213–226. 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,