Live on Product Hunt Intraface launched today. Less steering, more vacations. See the launch
Start for free

What is optimistic concurrency control?

Optimistic concurrency control lets many writers work on the same record without locking it. Each one reads the current version number, makes its change, and asks the store to write it only if the version is still the same. If someone else wrote first, the write is refused and the writer re-reads and retries.

Part of Glossary: the terms behind MCP, coding agents and shared pages, defined plainly.

How it works

Two agents write from version 7; the server compares and swaps, so the first becomes version 8 and the second is told to re-read.Ana's agentread version 7Ben's agentread version 7check, then writein one stepthe serverthe boardversion 7, then 81. write, if still v72. write, if still v71. v7 matches: now v82. v8 now: re-read, retry
  1. Read the record and remember its version: a number, a timestamp or an ETag. Nothing is locked.

  2. Make your change locally, however long it takes. Other writers do the same from the same version.

  3. Write with a condition: apply only if the stored version still equals the one you read. The check and the write are one atomic step (compare and swap).

  4. If the condition holds, the write lands and the version advances. If not, you get a refusal, such as HTTP 412 or a stale-version error.

  5. On a refusal, re-read, redo your change on the new version and try again. When collisions are rare this costs almost nothing.

Source: Kung and Robinson, On optimistic methods for concurrency control (ACM TODS 6:2, 1981)

Optimistic versus pessimistic locking

Pessimistic control takes a lock before reading, holds it while you work, and releases it when you write. Nobody else can touch the record in between, so there is never a collision, and also never any overlap: every writer waits for the one before. Hold a lock across a slow step, or crash while holding it, and everyone waits for nothing.

Optimistic control takes no lock. Kung and Robinson's 1981 paper described a transaction as a read phase, a validation phase and a write phase: do the work on a private copy, then check that nothing you read has changed, then write. The bet is that collisions are rare, so paying for them when they happen is cheaper than paying to prevent them every time. "Optimistic locking" is the common name in ORMs and databases, but notice the irony: nothing is locked.

Example: two agents publishing one page

Two people's coding agents pull a shared board at version 7 and spend ten minutes editing. Both publish. The server processes publishes one at a time. The first says "I started from 7": the head is 7, so it lands and the board is now version 8. The second also says "I started from 7", but the head is 8.

What happens next depends on how fine the check is. Intraface checks per file: if the second publish changed different files from version 8, it is merged on top and becomes version 9; if it changed the same file, it comes back as needs_rebase, naming the file and who changed it, and the agent pulls version 8 and redoes its edit. A plain database row does the simpler thing: UPDATE ... WHERE version = 7 touches zero rows, and the application retries.

HTTP has the same idea built in. A client sends If-Match with the ETag it last saw; RFC 9110 says the server must not perform the request if the condition is false, and may answer 412 Precondition Failed, to prevent what it calls the lost update problem.

When to use it

Use it when writers rarely collide and you cannot or will not hold locks: web and API requests that are stateless by nature, documents edited by people who pull, think and publish, records where a retry is cheap. Avoid it when contention is high. When conflicts are frequent, the cost of repeatedly restarting work hurts badly, and a lock or a queue serves better.

The one detail that matters is the atomicity of the check-and-write. The comparison and the update must be a single step, which is what compare and swap means: compare the stored value with the one you expect, and swap in the new one only if they match, as one atomic operation that reports whether it succeeded. If the check and the write are two separate calls, a second writer can slip between them and you have the lost update you were trying to prevent.

Common mistakes

  • Comparing content instead of a version. Two writers can set a field back to its old value, so "the value is what I read" does not mean nothing happened. Use a version number that only increases.
  • Retrying blindly. On a refusal, re-read first and re-apply your intent to the new state. Replaying the same bytes just overwrites the other person's work with an extra step.
  • Checking the whole record when a part would do. Coarse checks make unrelated edits collide. Per-field or per-file checks let non-overlapping changes through.

FAQ

Is optimistic locking actually a lock?
No. Nothing is locked; the name stuck because it solves the same problem locks solve. Each writer records the version it read, and the store refuses a write whose version is stale. The writer, not the store, handles the retry.
When does optimistic concurrency control perform badly?
When many writers hit the same record at once. Each refused write is work thrown away and redone, and under heavy contention the retries can outnumber the successes. A queue or a lock is the better tool there.
What is the difference between compare and swap and optimistic concurrency control?
Compare and swap is the single atomic step: write the new value only if the current value is the one you expected, and report whether it happened. Optimistic concurrency control is the larger pattern built on it: read, work without a lock, attempt a conditional write, retry on refusal.

Stop sending HTMLover Slack.

Create an account in a minute, connect your coding agent, and make your first board.