Skip to the content.

🔄 Versioning (Compare-And-Swap / CAS)

Definition:

Compare-And-Swap (CAS) is a concurrency control technique used in optimistic locking where a value is updated only if it hasn’t changed since it was last read. Often implemented via a version number associated with a record or object.

It’s widely used in databases, caches, and multi-threaded programming.


1️⃣ How CAS Works

  1. Each record has a version number (or timestamp).
  2. Read the value and its version.
  3. Compute new value.
  4. Update the value only if the version matches the version you read.
  5. If the version has changed (someone else updated it), retry or abort.

Pseudocode Example:

// Read current value and version
(oldValue, oldVersion) = read(key)

// Compute new value
newValue = oldValue + 1

// Attempt to update
if (currentVersion(key) == oldVersion) {
    write(key, newValue, oldVersion + 1)
} else {
    // Retry because value changed
}

2️⃣ Advantages


3️⃣ Disadvantages


4️⃣ Use Cases


5️⃣ Analogy

Think of CAS like signing a contract with a version number:


💡 Key takeaway: CAS allows safe concurrent updates without locking, but may require retries under high contention. It’s the core of optimistic concurrency control.