Dataoptimistic lockingcompare-and-setexpected versionversion columnetag

Optimistic concurrency control

Definition

Optimistic concurrency control attaches a version to each record and applies a write only if the version is still the one the writer read; otherwise the write fails and the caller retries or reports a conflict.

In practice

It prevents lost updates without holding locks across user think time or network calls, which suits web forms, APIs with If-Match ETags and event stores that append with an expected stream version. Conflicts become visible errors instead of silent overwrites.

Under heavy contention on the same record, most attempts fail and retries waste work, so pessimistic locking or a queue is better for hot rows. The retry must re-read and re-decide, not just resend the stale write, and the version check has to be atomic with the write.

Example

UPDATE docs SET body = :b, version = 8 WHERE id = 5 AND version = 7 affects 0 rows if another editor saved first, and the client shows a conflict instead of overwriting their change.