AlgoViz

Optimistic locking with versions

medium 4 min

Read an object and its version.

Video lesson · Chapter 1 of 5

Two editors open version seven

0:00 / 3:29

Two editors save revision five

1:15Reel

Theory

Read or listen first. About 1 min.

What it is

A conditional save carries both the new content and the revision the editor originally read. The store checks that revision in the same operation that publishes the change.

Two editors start from revision 5

Editor ADocumentEditor BRead v5Read v5Expect v5Saved · v6The comparison and write form one operation.
Both editors load revision 5. A’s conditional save matches the stored revision and advances it to 6.

Why it exists

Conflicting edits can be rare enough that holding a lock during editing would waste time. A failed comparison identifies the point where a fresh read and merge are needed.

Words to know

Expected revision
The version a caller believes is still current when it submits a conditional update.

How it works

Step through the idea, or press Play.

Step 1 of 3

Read revision 5. Two editors start from the same revision.

Understand it

Worked example

Optimistic locking with versions in practice

A and B read revision 5. A publishes revision 6. B’s condition expecting 5 fails. B must reread revision 6 before deciding whether its original edit still makes sense.

Related case

Who gets the last reservation?

A library releases a limited print edition at noon. Thousands see one copy available. A customer can reserve it for two minutes while checking out.

Two readers at the library desk
One copy at the library deskAdaticket ACopy 8availableBowaitingBoth readers see one copy

Compare item, owner, state and deadline.

Sources