Optimistic concurrency control: Difference between revisions

Content deleted Content added
m presentation
{{unsourced}}, rephrase, formatting, misc
Line 1:
{{unsourced}}
 
In [[computer science]], in the field of [[database]]s, '''optimistic concurrency control''', (OCC) is a [[concurrency control]] method used in [[relational database]]s without using [[Lock (computer science)|lock]]ing. It is commonly referred to as '''optimistic locking''', a misnomer.
 
Optimistic Concurrencyconcurrency Controlcontrol is based on the assumption that most [[database transaction]]s mostly don't conflict with other transactions, and that allowsallowing OCC to be as permissive as possible in allowing transactions to execute.
 
There are three phases in an OCC transaction:
 
1.# '''READRead''': The client reads values from the database, storing them to a private sandbox or cache that the client can then edit.
2.# '''VALIDATIONValidate''': When the client has completed editing of the values in its sandbox or cache, it initiates the storage of the changes back to the database. During validation, an algorithm checks if the changes to the data would conflict with either
 
:#* currently executingalready-committed transactions in the case of ''Forwardbackward Validationvalidation Schemesschemes''., or
2. '''VALIDATION''': When the client has completed editing of the values in its sandbox or cache, it initiates the storage of the changes back to the database. During validation, an algorithm checks if the changes to the data would conflict with either
:#* already-committedcurrently executing transactions in the case of ''Backwardforward Validationvalidation Schemesschemes'', or.
#:If a conflict exists, a Conflictconflict Resolutionresolution Algorithmalgorithm must be used to resolve the conflict somehow (ideally by minimizing the number of changes made by the user) or, as a last resort, the entire transaction can be aborted (resulting in the loss of all changes made by the user).
:* currently executing transactions in the case of ''Forward Validation Schemes''.
3.# '''WRITEWrite''': If there is no possibility of conflict, the transaction commits.
:If a conflict exists, a Conflict Resolution Algorithm must be used to resolve the conflict somehow (ideally by minimizing the number of changes made by the user) or, as a last resort, the entire transaction can be aborted (resulting in the loss of all changes made by the user).
 
3. '''WRITE''': If there is no possibility of conflict, the transaction commits.
 
IfWhen thereconflicts are few conflictsrare, validation can be done efficiently, and leadsleading to betterhigher performancethroughput than other concurrency control methods.{{fact}} UnfortunatelyHowever, if thereconflicts arehappen many conflictsoften, the cost of repeatedly restarting transactions, hurts performance significantly --— other [[non-lock concurrency control]] methods have better performance when there are many conflicts.
 
==Web usage==
 
The [[Stateless_serverStateless server|stateless]] nature of [[HTTP]] makes locking infeasible for web user interfaces.{{fact}} It's 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 expires.
 
OCC is a natural choice. It is simple to implement and avoids unnecessary waiting or silently overwritten records. Typically the [[Form (web)|form]] presented to the user includes 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 activatedinvoked.
 
===Examples===
Line 27:
* [[Bugzilla]] uses OCC; conflicts are called "mid-air collisions". [http://wiki.mozilla.org/Bugzilla:FAQ:Administrative_Questions#Does_Bugzilla_provide_record_locking_when_there_is_simultaneous_access_to_the_same_bug.]
* The [[Ruby on Rails]] framework has an API for OCC. [http://api.rubyonrails.com/classes/ActiveRecord/Locking.html]
* Most [[revision control]] systems support the "merge" model for concurrency, which is OCC.
 
[[Category:concurrency control]]