Skip to content

Lesson 7 of 9 · 25m

Conflict Resolution: Last-Write-Wins, OT, and CRDTs

What happens when two clients edit the same state, how to choose a merge strategy, and why collaboration is a data-model decision.

Realtime collaboration is not primarily a transport problem. The transport only delivers bad decisions faster. The hard question is: when two actors change the same object concurrently, what is the resulting state supposed to mean?

Start with the shape of the data

Some state is naturally independent per user: cursor position, selection, typing indicators. Last-write-wins per user is correct there because users do not share ownership. Some state is a shared scalar, such as a title or checkbox. Some state is ordered text, where concurrent inserts must merge without destroying either edit.

cursor position       -> last write wins per user
document title        -> versioned scalar or operational transform
small shared counter  -> atomic server mutation / CRDT counter
rich text document    -> OT or CRDT
chat message list     -> append-only log with server ordering

Choosing one strategy for everything is how teams end up building a collaborative text editor when all they needed was a form lock.

Last-write-wins needs versions, not hope

UPDATE documents
SET title = $1, version = version + 1, updated_at = now()
WHERE id = $2 AND version = $3;

If zero rows update, someone else won. Return a conflict and let the client merge or rebase. Blindly overwriting because the websocket message arrived later is not a strategy; it is data loss with low latency.

For low-value fields, silent last-write-wins can be acceptable if you say so. For user-entered text, it is usually unacceptable.

Operational Transform and CRDTs solve different organizational problems

Operational Transform transforms concurrent operations against each other so all replicas converge. It powered classic collaborative editors and often assumes a central sequencing authority. CRDTs design operations or states so they commute; replicas can merge in any order and converge.

// Yjs-style conceptual flow, not a full implementation
const update1 = Y.encodeStateAsUpdate(docA);
const update2 = Y.encodeStateAsUpdate(docB);
Y.applyUpdate(docA, update2);
Y.applyUpdate(docB, update1);
// docA and docB converge even if updates arrive in different orders

CRDTs are not free magic. Metadata overhead, tombstones, snapshot compaction, and schema migrations become real engineering work. OT is not simpler either; transformations are subtle and implementation bugs corrupt documents. Use a proven library when the domain fits it.

Server authority simplifies correctness

For most applications, make the server sequence mutations. Clients send intent, server assigns order, then broadcasts the committed result. This is slower than peer-to-peer optimism but vastly easier to audit, authorize, and recover.

Optimistic UI can still hide latency. The trick is to keep pending operations explicit, then reconcile them when authoritative events arrive. If the server rejects an edit, rollback must restore the prior state, not merely toast an error while leaving corrupted UI.

Design undo before you need it

Realtime systems magnify mistakes because they propagate instantly. Append-only logs, operation history, snapshots, and periodic compaction are the difference between recovery and archaeology. Even if users never see an undo button, operators will need one.

Field notes

  • Do not use a CRDT because it is fashionable. Use it because concurrent edits are central to the product.
  • Every mutation needs actor, timestamp, causality or sequence information, and a stable ID.
  • Authorization must be checked on the operation, not just connection establishment. Users lose access while sockets remain open.
  • Test conflicts with real network delay. Two localhost tabs are not a concurrency test.

Was this lesson helpful?

Sign in to rate