jonnify·persistence /echo-persistence/persistence-concepts/the-occ-commit

Module 3 · Dive 3.2

The OCC commit.

Reads were free because the log never changes. Writes are the hard part: two writers can both want the next LSN. The local engine resolves it optimistically — each stages a commit against the head it read, and the append is a compare-and-swap on the head. One wins; the other is told {:conflict, head} and retries. Run it.

§1

Two writers, one head

interactive
Optimistic commit. Both writers read head = 5 and stage LSN 6. Step forward: the append is a compare-and-swap on the head — only one swap from 5 to 6 succeeds.
shared log · head is the last LSN head = 5 Writer 1 read head: 5 stage commit → — reading… Writer 2 read head: 5 stage commit → — reading… compare-and-swap: append succeeds only if head is still what you read.
step 0 · both read the head
step 0 · Both writers read head = 5. Each will stage a commit as LSN 6 — but only one compare-and-swap from 5→6 can win.
§2

Why optimistic, not locked

graft.design.md

A lock would make every writer wait even when there is no contention, which is most of the time. Optimistic concurrency assumes success and only pays on collision: stage freely, and let the single atomic swap on the head decide. The loser hasn’t corrupted anything — it simply re-reads the new head and tries again, which is cheap because reads are free. This is the local serialization, inside one process. The next dive is the same “one writer wins” idea one layer out, where the writers are different nodes and the referee is the object store.

§3

References & sources