Kalpana Kalpana (Editor)

Optimistic concurrency control

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

Optimistic concurrency control (OCC) 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. Optimistic concurrency control was first proposed by H.T. Kung.

Contents

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; it is commonly thought that other concurrency control methods have better performance under these conditions. However, locking-based ("pessimistic") methods also can deliver poor performance because locking can drastically limit effective concurrency even when deadlocks are avoided.

OCC phases

More specifically, OCC transactions involve these phases:

  • 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 TOCTTOU 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'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 times out.

    HTTP does provide a form of built-in OCC: The GET method returns an ETag for a resource and subsequent PUTs use the ETag value in the If-Match headers; while the first PUT will succeed, the second will not, as the value in If-Match is based on the first version of the resource.

    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 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 invoked.

    Examples

  • MediaWiki's edit pages use OCC.
  • Bugzilla uses OCC; edit conflicts are called "mid-air collisions".
  • The Ruby on Rails framework has an API for OCC.
  • The Grails framework uses OCC in its default conventions.
  • The GT.M database engine uses OCC for managing transactions (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.
  • Mimer SQL is a DBMS that only implements optimistic concurrency control.
  • Google App Engine data store uses OCC.
  • The Elasticsearch search engine supports OCC via the version attribute.
  • CouchDB implements OCC through document revisions.
  • The MonetDB column-oriented database management system's transaction management scheme is based on OCC.
  • Most implementations of software transactional memory use optimistic locking.
  • Redis provides OCC through WATCH command.
  • MySQL implements OCC in Group Replication configuration
  • References

    Optimistic concurrency control Wikipedia