Why CRDTs exist
Last-write-wins and ad-hoc merges lose updates under concurrent writes. CRDTs (Conflict-free Replicated Data Types) guarantee that replicas converge to the same state without coordination, as long as all operations eventually reach every replica.
Two families:
| Family | Idea | Examples |
|---|---|---|
| State-based (CvRDT) | Merge full states with a commutative, associative, idempotent function | G-counter, OR-set, LWW-register |
| Operation-based (CmRDT) | Broadcast operations; apply in any order if ops commute | RGA text, op-based counters |
The math requirement (interview level)
A state-based CRDT needs merge ∪ such that:
- Commutative — merge(A,B) = merge(B,A)
- Associative — merge(merge(A,B),C) = merge(A,merge(B,C))
- Idempotent — merge(A,A) = A
Then replicas converge regardless of delivery order.
Common types to name
- G-counter / PN-counter — increment-only or increment/decrement counters
- OR-set — add wins over remove (observed-remove set)
- LWW-register — timestamped last-write-wins per register (not global wall clock chaos)
- RGA / Yjs-style sequences — collaborative text with unique op IDs
CRDT vs coordination
| Approach | Pros | Cons |
|---|---|---|
| CRDT | High availability, partition tolerant merges | Metadata overhead, limited data models, tombstone GC |
| Strong consensus (Raft) | Simple mental model for linearizable state | Unavailable under partition if minority |
Use CRDTs when availability + merge correctness beats single global order — collaborative editors, presence, counters, shopping carts with commutative adds.
Link to Topic 5
Version vectors detect concurrent writes; CRDTs define how to merge them without a central resolver.
Link to Databases track
Document DBs often use LWW at the field level (Dynamo-style). CRDTs are the principled alternative when LWW loses data you care about.
Interview answer template
"We'd use an OR-set for the wishlist so concurrent add/remove from two devices converges. For account balance we'd not use a CRDT — that needs linearizable transactions."
Further Reading
- Shapiro et al. — A comprehensive study of Convergent and Commutative Replicated Data Types (CRDT survey, skim §1–3)Reference45m
- DDIA — Ch. 5: §5.4.2 (version vectors) and Ch. 11: §11.3.3 (stream joins preview); Kleppmann's CRDT blog cross-refBook25m
- Martin Kleppmann — CRDTs and the Quest for Available Data TypesArticle20m
Hands-On Tasks (Optional)
Low-setup exercises — browser visualizers, paper drills, or optional Docker. No autograding; the goal is interview fluency.
- Pick a CRDT for collaborative editing20m
A Google-Docs-style cursor position and text body must merge without a central server during partition. Name one state-based and one op-based approach, and what invariant each preserves.