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.
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
- etcd v3.6 documentation: Key-Value pair metadata and Transaction.
- etcd v3.6 API guarantees: durability, strict serializability and operation completion.
- Herlihy. Wait-Free Synchronization. ACM TOPLAS 13(1), 1991, sections 3.1 to 3.3.
- etcd v3.6.16, txn.go: applyCompare and compareKV, commit 0b065e61c679ea81f6862bb24c9815f7fc673966.
- etcd v3.6 disaster recovery: Revision Difference and Restoring from snapshot.
- Hesela: native etcd key-incarnation conditions, captures and reproducibility method.
Further reading
- Storage consistency: ordering is not a transaction or a durability promise
- Hesela: NVMe conditional updates and complete constructed schedules
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.