jargon

Comparison

Compare-and-swapvsOptimistic locking

Compare-and-swap

you update the value only if it still equals what you last read, and loop round to try again if somebody beat you to it.

An atomic primitive that sets a value only if it currently matches an expected one. It is the basis of lock-free algorithms and of optimistic concurrency at every layer, from CPU instructions to conditional writes in a key-value store. Under high contention the retry loop can waste more CPU than a lock would have cost.

Full entry →

Optimistic locking

you read the row with its version, write it back only if the version has not changed, and retry the whole thing if it has.

Detecting conflicts at write time by checking that the record has not changed since you read it, usually via a version or timestamp column. There is no lock held, so readers never block and there is no deadlock risk. It works well when conflicts are rare, and degrades badly on hot rows where retries pile up.

Full entry →

Related comparisons