Optimistic Concurrency Control Explained: How Databases Avoid Locks

Optimistic concurrency control explained in plain terms means understanding how a database can let several transactions work on the same data at once, without locking it, and sort out any conflicts at the end. The central question is how to keep data correct when two people edit the same record at the same time, and the answer is optimism: assume conflicts are rare, let everyone work freely, and check for a clash only when a transaction is ready to commit.

Optimistic concurrency control, usually shortened to OCC, is a method for managing concurrent transactions in a database. Instead of locking data when a transaction reads it, the system lets the transaction work on a private copy, and when it tries to commit, it checks whether any other transaction changed the same data in the meantime. If nothing changed, the commit succeeds; if something did, the transaction is aborted and retried.

The idea is simple: locks are expensive, and in many systems conflicts are rare, so it is cheaper to let transactions run without locks and handle the occasional clash than to make every transaction wait its turn. This article explains what optimistic concurrency control is, how it works, and where it is used.

Key takeaways

  • Optimistic concurrency control lets transactions work without locks.
  • Conflicts are checked at commit time, not when data is read.
  • A version number or timestamp shows whether data has changed.
  • A conflicting transaction is aborted and retried, not blocked.
  • It suits systems where conflicts are rare and reads dominate.

What optimistic concurrency control is

Optimistic concurrency control is one of two broad approaches to keeping concurrent transactions correct. The other is pessimistic concurrency control, which locks data as soon as a transaction touches it, so that no other transaction can change it until the first one finishes. Pessimistic control is safe but slow, since transactions spend much of their time waiting for locks, while optimistic control is fast but must sometimes throw work away.

The method was first described by H. T. Kung and John Robinson in 1981, in a paper that argued locking was the wrong default for many systems. Their insight was that in most databases, transactions rarely touch the same data at the same time, so the cost of preventing conflicts was far higher than the cost of detecting them and retrying the few transactions that clashed.

Optimistic concurrency control is used wherever reads far outnumber writes, or where transactions are short and conflicts are unlikely. It is the default in many modern databases and application frameworks, often under the name optimistic locking, and it is the mechanism behind the version numbers that appear on records in systems from document stores to web applications. Our guide to ACID explains the isolation guarantee that concurrency control protects, and our guide to SQL explains the databases in which these methods are used.

How optimistic concurrency control works

A transaction under optimistic control runs in three phases. In the read phase, it reads the data it needs and works on a private copy, without taking any locks, so other transactions are free to read and write the same data at the same time. In the validation phase, when the transaction is ready to commit, the system checks whether any other transaction changed the data it used. In the write phase, if the check passes, the changes are written to the database; if it fails, the transaction is aborted and must be retried.

The check is usually done with a version number. Every record carries a number that increases each time the record is written, and a transaction remembers the version it read. At commit time, the system compares the remembered version with the current one: if they match, nothing changed, and the commit proceeds; if they differ, another transaction wrote first, and the commit is refused.

Timestamps work the same way. A transaction records the time it started, and the system tracks the last time each record was written. If a record was written after the transaction started, the transaction’s view of it is out of date, and the commit fails, while records untouched since the transaction began pass the check without a problem.

An aborted transaction is not an error; it is a normal part of the design. The application is expected to retry, usually by reading the fresh data and running the transaction again, and since conflicts are rare, most retries succeed on the first or second attempt. The cost of an abort is the wasted work of the failed attempt, which is why optimistic control suits short transactions far better than long ones.

Some systems combine optimism with a fallback. If a transaction fails validation repeatedly, the system may switch it to pessimistic locking for its next attempt, or escalate to a stronger isolation level, so that a transaction caught in a cycle of conflicts can eventually complete instead of retrying forever.

A dark office with two laptops side by side, both showing the same glowing green document, one screen marked with a small check and the other with a retry arrow
A dark office with two laptops side by side, both showing the same glowing green document, one screen marked with a small check and the other with a retry arrow, shows how optimistic concurrency control works: work freely, check at the end, retry if you clash.

Comparing optimistic with pessimistic concurrency control

Optimistic concurrency control is one of two ways to manage concurrent transactions, and each was built with a different emphasis. Pessimistic control locks data early and prevents conflicts entirely, two-phase locking orders transactions by their locks, and multi-version control keeps old copies so readers never wait, but each approach fits a different workload.

The table below sets out the main contrasts between the leading approaches.

Aspect Optimistic control Pessimistic control Two-phase locking
When conflicts are found At commit time Prevented by locks Prevented by locks
Waiting for locks None, no locks taken Common, locks held long Common, until commit
Cost of a conflict Wasted work, then retry Waiting time Waiting time, or deadlock
Deadlocks Impossible, no locks Possible Possible, needs detection
Best for Reads dominate, rare clashes Writes dominate, frequent clashes Traditional databases

The practical result is that optimistic control is the right choice when conflicts are rare and transactions are short, pessimistic control suits heavy write workloads where clashes are common, and two-phase locking is the traditional default in relational databases.

Where optimistic concurrency control is used

Web applications use optimistic locking to protect forms and records. When a user opens a record to edit it, the page carries the record’s version number, and when the user saves, the server checks that the version is unchanged. If another user saved first, the second save is refused with a message, rather than silently overwriting their work.

Document databases use optimistic concurrency control as their default. A document store lets any client read and modify a document, and writes carry the version the client read, so that two clients editing the same document cannot both succeed, and the loser is told to fetch the new version and try again.

Object-relational frameworks build optimistic locking into their models. An entity class carries a version field that the framework checks and increments on every update, so that application code gets the protection automatically, without writing the check by hand in every place a record is saved.

Version control systems use an optimistic idea at a larger scale. A developer works on a local copy of the code for hours or days, and when they push, the system checks whether anyone else changed the same lines in the meantime. If they did, the push is refused and the developer must merge, which is optimistic concurrency control applied to files rather than records.

Collaborative editing tools combine optimistic control with merging. When two people edit the same document, the system accepts both sets of changes and merges them where possible, falling back to a conflict only where the same text was changed in incompatible ways, which keeps the experience smooth while still protecting the data.

Strengths and limits

The greatest strength of optimistic concurrency control is that it removes waiting. Transactions never block on locks, readers never wait for writers, and the system can handle far more concurrent work than one built on locking, which is why it is the default in systems where reads dominate and conflicts are rare.

It is also a strength that optimistic control cannot deadlock. Since no transaction ever holds a lock while waiting for another, the circular waits that cause deadlocks are impossible, and the system never needs deadlock detection or resolution, which simplifies both the database and the application.

The limits are real, though. When conflicts are common, optimistic control wastes work: transactions run to completion and then fail, again and again, and under heavy contention a transaction may never succeed. Long transactions are the worst case, since the longer a transaction runs, the more likely another one is to change its data before it commits.

Optimistic concurrency control checklist

  • Use optimistic control when conflicts are rare and reads dominate.
  • Add a version number to every record that can be edited concurrently.
  • Check the version at commit time, and refuse stale writes.
  • Retry aborted transactions automatically, with a limit.
  • Keep transactions short to reduce the chance of a clash.
  • Show a clear message when a user’s edit conflicts with another’s.
  • Switch to pessimistic locking for transactions that keep failing.
  • Never ignore a version mismatch and write anyway.
  • Test with two clients editing the same record at the same time.

Frequently asked questions

What is optimistic concurrency control in simple terms?

It is a way of letting several transactions work on the same data without locking it, and checking for conflicts only when a transaction is ready to commit. If nothing changed since it started, the commit succeeds; if something did, the transaction is retried.

What is the difference between optimistic and pessimistic locking?

Pessimistic locking locks data as soon as a transaction reads it, preventing conflicts but making other transactions wait. Optimistic control takes no locks, lets everyone work, and detects conflicts at commit time, which is faster when conflicts are rare.

How does a version number detect a conflict?

Every record carries a number that increases on each write, and a transaction remembers the version it read. At commit time, if the current version differs from the remembered one, another transaction changed the record, and the commit is refused.

What happens when a transaction fails validation?

It is aborted, its changes are discarded, and the application is expected to retry, usually by reading the fresh data and running the transaction again. A failed validation is a normal event, not an error, and most retries succeed quickly.

Can optimistic control cause a deadlock?

No, because it takes no locks. A deadlock needs transactions waiting on locks held by each other, and since optimistic transactions never wait for a lock, the cycle that causes a deadlock cannot form.

When should I not use optimistic concurrency control?

When conflicts are frequent, when transactions are long, or when a failed transaction is very expensive to retry. In those cases pessimistic locking, which prevents conflicts rather than detecting them, is usually the better choice.

The bottom line

Optimistic concurrency control explained in one line is this: assume no one will clash, work without locks, and check at the end.

It is the method that lets a database handle far more concurrent work than locking would allow, trading the occasional wasted retry for the freedom never to wait.

This article is general information about how optimistic concurrency control works, not advice about choosing a concurrency method for a particular system.

Latest articles

Scroll to Top