Full answer
The shared goal
Several replicas apply edits concurrently and must end in the same state (convergence) while keeping each user's intent.
Operational transformation (OT)
An operation such as "insert 'X' at position 5" depends on the document it was made on. If another user deleted position 2 at the same time, applying the insert unchanged would put X in the wrong place. OT defines a transform function T(op1, op2) that adjusts op1 to apply after op2 (the insert moves to position 4).
In practice a central server orders all operations per document. Each client sends operations with the version it was based on; the server transforms them against everything accepted since and broadcasts the result. Clients transform their own pending operations against incoming ones.
- Strengths: small operations, compact documents, history is a simple log; proven in large editors.
- Weaknesses: transform functions are tricky to get right for every pair of operation types; peer-to-peer OT without a central order is hard.
CRDTs
A CRDT (conflict-free replicated data type) guarantees convergence by construction. State-based CRDTs merge whole states with a join that is commutative, associative and idempotent; operation-based CRDTs send operations that commute.
The simplest example is a grow-only counter:
class GCounter:
"""Grow-only counter CRDT: each replica increments its own slot; merge takes the max."""
def __init__(self, replica):
self.replica, self.slots = replica, {}
def increment(self, n=1):
self.slots[self.replica] = self.slots.get(self.replica, 0) + n
def merge(self, other):
for r, v in other.slots.items():
self.slots[r] = max(self.slots.get(r, 0), v)
def value(self):
return sum(self.slots.values())
if __name__ == "__main__":
a, b, c = GCounter("a"), GCounter("b"), GCounter("c")
a.increment(3)
b.increment(2)
c.increment(5)
a.merge(b); b.merge(c); c.merge(a); a.merge(c); b.merge(a) # any order, repeated merges
assert a.value() == b.value() == c.value() == 10
a.merge(a) # idempotent
assert a.value() == 10
print("ok: all replicas converge to", a.value())Save as gcounter.py and run python3 gcounter.py (Python 3.13.5).
For text, sequence CRDTs (RGA, Yjs, Automerge) give each character a unique ID and a position defined relative to its neighbours' IDs, so inserts and deletes can be applied in any order. Deleted characters often remain as tombstones.
- Strengths: no central server needed; works offline for days and merges later; replicas can sync peer to peer.
- Weaknesses: metadata per character and tombstones increase memory; some intentions (move a paragraph, rich-text formatting spans) need careful design; garbage collection of tombstones needs coordination.
Choosing
- Central server, always online, very large documents: OT (or a server-ordered CRDT).
- Offline-first mobile apps, local-first software, peer-to-peer sync: CRDTs.
- Simple shared state such as counters, sets, flags: CRDTs are easy and safe.
Both guarantee convergence, not that the result is what users wanted; two people retyping the same word still produce a duplicated word.
By level
Fresher: Explain that two people editing at once can conflict, and that both methods make every copy end up the same. A fresher should explain the position shift example.
Mid: Describe OT with a central server and CRDTs with unique IDs and commutative merges. Use the counter example to show convergence.
Senior: Discuss tombstones and metadata cost, rich text, garbage collection, server-ordered CRDTs and correctness of transform functions. Expect a clear decision framework.
Key points
- OT transforms concurrent operations, usually with a central order
- CRDT merges are commutative, associative and idempotent
- Sequence CRDTs give each character a unique ID
- CRDTs suit offline and peer-to-peer; OT suits server-centred editors
- Convergence does not guarantee the merged text matches intent
Common mistakes
- Thinking last-write-wins is a CRDT solution for text
- Assuming OT works peer to peer without extra machinery
- Ignoring tombstone growth in CRDTs
- Claiming either method removes all semantic conflicts
Follow-ups
- How do you garbage-collect CRDT tombstones?
- How would you represent bold text ranges in a CRDT?
- How does a server-ordered CRDT combine both ideas?
Tips
- Give the insert and delete position example
- Show a tiny CRDT such as a counter
- Tie the choice to online versus offline use
Evaluation rubric
- Correct explanation of OT transforms
- Correct CRDT properties
- Balanced trade-offs
- Clear selection guidance