compare-and-swap · shipping · also CAS, Compare-and-set

Compare-and-swap

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.

Related Observatory article: Read on hesela.com

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. [2]

As 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. [5]

etcd 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. [1] [6]

In 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. [8]

Snapshot restore creates a new logical cluster and may move revisions backward. These guards need an explicit recovery policy and do not fence external writes. [7]

Relations

related
Linearizability, Serializability, Conditional write, NVMe Compare and Write, Write skew, Snapshot isolation, SCSI COMPARE AND WRITE, LLAMA, Gryff, Pineapple, Sinfonia, Content-addressed storage

Advantages

  • 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. [1]

Limitations

  • 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. [1]

Citations

  1. etcd v3.6 documentation: Key-Value pair metadata and Transaction. (accessed 2026-10-23)
  2. etcd v3.6 API guarantees: durability, strict serializability and operation completion. (accessed 2026-10-12)
  3. Herlihy. Wait-Free Synchronization. ACM TOPLAS 13(1), 1991, sections 3.1 to 3.3. (accessed 2026-10-23)
  4. etcd v3.6.16, txn.go: applyCompare and compareKV, commit 0b065e61c679ea81f6862bb24c9815f7fc673966. (accessed 2026-11-28)
  5. etcd v3.6 disaster recovery: Revision Difference and Restoring from snapshot. (accessed 2026-11-28)
  6. Hesela: native etcd key-incarnation conditions, captures and reproducibility method. (accessed 2026-11-29)

Further reading

  1. Storage consistency: ordering is not a transaction or a durability promise (accessed 2026-10-12)
  2. Hesela: NVMe conditional updates and complete constructed schedules (accessed 2026-10-21)
Sources & caveats
  • 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.

JSON: /data/terms/compare-and-swap.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. Compare-and-swap. Hesela. 2026-11-29. https://hesela.dev/terms/compare-and-swap/