Skip to main content
Definition Website Computer and Technology

Optimistic Concurrency Control

Subject: Computer and Technology noun /ˌɑːp.tɪˈmɪs.tɪk kənˈkɜːr.ən.si kənˈtroʊl/ 1 definition

  1. Definition (noun)

    Entry #311 · Updated

    Optimistic Concurrency Control (OCC) is a concurrency control strategy used in databases, distributed systems, and shared-memory environments that allows multiple transactions to read and modify data simultaneously without acquiring up-front locks. Rather than blocking competing operations, each transaction executes a read phase against a local workspace or snapshot, enters a validation phase at commit time to confirm that no other committed transaction altered the records it depends on, and proceeds to a write phase only if validation succeeds. When validation detects a conflict—via mismatched version counters, timestamps, entity tags, or intersecting read-write sets—the transaction aborts, discards its buffered changes, and typically retries against fresh state. OCC's core purpose is to eliminate lock acquisition overhead, blocking, and deadlocks in read-heavy or low-contention workloads, though its throughput degrades under high write contention as frequent rollbacks and retries waste work.

    Common Forms

    • Version-Based Validation: Attaches a monotonically increasing version number or timestamp to each record and issues conditional writes—such as UPDATE ... WHERE id = ? AND version = ?—that succeed only if the stored version still matches the value originally read.

    • ETag and Conditional HTTP Requests: Pairs HTTP ETag response headers with If-Match request preconditions in RESTful APIs and object stores so stale concurrent PUT or PATCH requests fail with 412 Precondition Failed rather than overwriting newer state.

    • Read-Write Set (Kung-Robinson) Validation: Records the sets of data items read and written during a transaction's execution phase and verifies at commit time that those sets do not conflict with transactions committed since the execution began.

    • Compare-and-Swap (CAS) Primitives: Uses atomic CPU or lock-free storage operations that compare a target memory location or key against an expected value and install a new value only when unchanged, retrying in a loop on mismatch.

    Example

    An inventory microservice backed by PostgreSQL reads SKU A-402 (quantity = 5, version = 12) and attempts to reserve one unit with UPDATE inventory SET quantity = 4, version = 13 WHERE sku = 'A-402' AND version = 12. Because a parallel checkout worker committed version = 13 milliseconds earlier, the query modifies zero rows, signaling a conflict that prompts the service to reload the updated row (quantity = 4, version = 13) and retry the decrement.