CSE Roadmap

Specialist tier · about 2 hours · 5 parts

Part 4 of 5 · 2 min read

Optimistic concurrency control

A transaction reads and computes freely, then is validated for conflicts only once, right before it commits.

Optimistic concurrency control

Think of drafting a document privately. Work freely without ever checking anyone else's drafts, then only at the very end check whether anyone else published a conflicting change in the meantime, before publishing the draft as final.

In plain words: optimistic concurrency control runs a transaction through three phases: a read phase, during which it reads data and computes its intended changes without acquiring any locks at all; a validation phase, right before committing, during which the database checks whether any conflicting transaction committed in the meantime that would make this transaction's changes unsafe; and a write phase, during which the transaction's changes are actually applied, but only if validation passed. If validation fails, the transaction is aborted and typically restarted, rather than ever having waited on a lock.

A worked example. A transaction reads a row's balance, computes a new value based on it, and only at commit time checks whether another transaction modified that same balance in the meantime; if so, this transaction is aborted and restarted with fresh data.

The key lines. No new SQL; optimistic concurrency control is an internal protocol, favoring workloads where conflicts are expected to be rare, since restarting an aborted transaction has its own real cost.

Optimistic concurrency control

Optimistic concurrency control lets a transaction read and compute freely with no locks, then validates for conflicts only once, right before commit, aborting and restarting the transaction if a conflict is found.

Example: drafting privately and only checking for conflicting published changes right before publishing, rather than checking continuously, mirrors optimistic concurrency control's single validation phase.

Remember this

Locking prevents conflicts by making transactions wait; timestamp ordering rejects conflicting operations as they happen; optimistic concurrency control defers all checking to one validation phase at the end. All three can guarantee serializability, but they make very different trade-offs about when conflicts are caught.

Quick notes

  • Timestamp ordering assigns each transaction a starting timestamp and rejects operations that would violate the required order.
  • Thomas' write rule ignores, rather than rejects, an outdated write that would never have been visible to any reader.
  • Optimistic concurrency control has a read phase, a validation phase, and a write phase, with no locks held during reading.
  • Validation failure under optimistic concurrency control means the transaction is aborted and typically restarted.
  • Optimistic concurrency control favors workloads where conflicts are expected to be rare, since restarts have a real cost.