{
  "status": "shipping",
  "advantages": [
    {
      "text": "A single guarded transaction both checks and writes, and the store revision advances only once for all requests inside it, so its events share one revision.",
      "citationIds": [
        "etcd-api"
      ]
    }
  ],
  "limitations": [
    {
      "text": "Requests inside one transaction may not share keys between puts and deletes, so a single guarded step cannot both rewrite and remove the same key.",
      "citationIds": [
        "etcd-api"
      ]
    }
  ],
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "updated": "2026-12-05",
  "slug": "compare-and-swap",
  "term": "Compare-and-swap",
  "aliases": [
    "CAS",
    "Compare-and-set"
  ],
  "definition": "Compare-and-swap is an atomic conditional update that replaces a stored value only if it matches an expected value and reports the comparison outcome or observed value.",
  "note": "The comparison and update must be indivisible; separate GET and PUT requests leave a race. Value equality misses A-B-A changes. A timeout can leave completion unknown. This CAS is not content-addressable storage. [etcd-guarantees]\n\nAs a shared-memory object, a CAS register has infinite consensus number. A sentinel-initialized register supports wait-free agreement for any number of processes. Read/write registers have 1; test-and-set, swap and fetch-and-add have 2. Combining those weaker operations cannot implement wait-free CAS for 3 or more processes. [herlihy-waitfree]\n\netcd transactions compare value, version, creation or modification revision with equal, greater, less or not-equal tests, then execute one branch atomically. Modification increments the version; deletion resets it to 0. VERSION can repeat after recreation, even with different bytes. CREATE guards the observed lifetime but allows in-place writes. MOD guards the last modification: use the key's mod_revision, not the response header's store revision. [etcd-api] [etcd-compare-3616]\n\nIn 40 prescribed cases repeated in three fresh etcd 3.6.16 clusters, VERSION accepted recreation, CREATE accepted in-place changes, and MOD rejected both. Unrelated-key writes passed every tested guard. These are controlled observations, not failure rates. [hesela-incarnation]\n\nSnapshot restore creates a new logical cluster and may move revisions backward. These guards need an explicit recovery policy and do not fence external writes. [etcd-recovery]",
  "related": [
    "aba-problem",
    "linearizability",
    "serializability",
    "conditional-write",
    "nvme-compare-and-write",
    "write-skew",
    "snapshot-isolation",
    "scsi-compare-and-write",
    "llama",
    "gryff",
    "pineapple",
    "sinfonia",
    "content-addressed-storage"
  ],
  "citations": [
    {
      "id": "etcd-api",
      "title": "etcd v3.6 documentation: Key-Value pair metadata and Transaction.",
      "url": "https://etcd.io/docs/v3.6/learning/api/",
      "accessed": "2026-10-23"
    },
    {
      "id": "etcd-guarantees",
      "title": "etcd v3.6 API guarantees: durability, strict serializability and operation completion.",
      "url": "https://etcd.io/docs/v3.6/learning/api_guarantees/",
      "accessed": "2026-10-12"
    },
    {
      "id": "hesela-consistency",
      "title": "Storage consistency: ordering is not a transaction or a durability promise",
      "url": "https://hesela.com/analyses/storage-consistency-ordering/",
      "accessed": "2026-10-12"
    },
    {
      "id": "hesela-fused",
      "title": "Hesela: NVMe conditional updates and complete constructed schedules",
      "url": "https://hesela.com/analyses/storage-consistency-ordering/#nvme-fused",
      "accessed": "2026-10-21"
    },
    {
      "id": "herlihy-waitfree",
      "title": "Herlihy. Wait-Free Synchronization. ACM TOPLAS 13(1), 1991, sections 3.1 to 3.3.",
      "url": "https://cs.brown.edu/~mph/Herlihy91/p124-herlihy.pdf",
      "accessed": "2026-10-23"
    },
    {
      "id": "etcd-compare-3616",
      "title": "etcd v3.6.16, txn.go: applyCompare and compareKV, commit 0b065e61c679ea81f6862bb24c9815f7fc673966.",
      "url": "https://github.com/etcd-io/etcd/blob/0b065e61c679ea81f6862bb24c9815f7fc673966/server/etcdserver/txn/txn.go",
      "accessed": "2026-11-28"
    },
    {
      "id": "etcd-recovery",
      "title": "etcd v3.6 disaster recovery: Revision Difference and Restoring from snapshot.",
      "url": "https://etcd.io/docs/v3.6/op-guide/recovery/",
      "accessed": "2026-11-28"
    },
    {
      "id": "hesela-incarnation",
      "title": "Hesela: native etcd key-incarnation conditions, captures and reproducibility method.",
      "url": "https://hesela.dev/research/etcd-key-incarnation/README.md",
      "accessed": "2026-11-29"
    }
  ],
  "provenance": [
    "herlihy-waitfree read 2026-10-23, sections 3.1 to 3.3, for the consensus-number hierarchy, Theorem 5 and Corollary 3.",
    "etcd-api re-read 2026-10-23 for the Compare message, key metadata fields and transaction semantics.",
    "The snapshot-isolation entry links here because its commit rule rejects a transaction when a concurrent committed one wrote the same data, a per-item conflict test akin to a revision compare.",
    "Two companion analyses on ordering and on device-level conditional updates are listed but not used in prose.",
    "2026-11-29: three fresh local etcd 3.6.16 clusters, 40 cases per run. Raw captures, revised verifier and limitations in the linked evidence packet. No recovery or fault-rate experiment."
  ]
}
