Conflict-Free Replicated Data Types (CRDTs)
What you'll learn: How specially designed data structures automatically merge concurrent updates from multiple replicas without conflicts or coordination.
The Core Idea
CRDTs are mathematically-designed data structures that guarantee convergence—meaning all replicas eventually reach the same state—without requiring coordination between nodes. Unlike Last-Write-Wins or version vectors that detect conflicts, CRDTs prevent conflicts entirely through their merge logic.
Think of CRDTs like a shared grocery list where multiple family members can add items simultaneously. Instead of fighting over whose addition "wins," the final list simply contains everything everyone added. The data structure's rules ensure everyone ends up with the identical complete list, no matter what order updates arrive.
How CRDTs Work
CRDTs rely on commutative and associative operations—operations where order doesn't matter. There are two main types:
State-based CRDTs (CvRDTs): Replicas exchange their entire state and merge using a special merge function that's commutative, associative, and idempotent.
Operation-based CRDTs (CmRDTs): Replicas broadcast operations that commute—applying them in any order yields the same result.
Common CRDT examples include:
- G-Counter (grow-only counter): Each replica maintains its own count; totals are summed
- PN-Counter (positive-negative counter): Separate increment and decrement counters
- G-Set (grow-only set): Items can only be added, never removed
- OR-Set (observed-remove set): Tracks unique tags to handle concurrent add/remove correctly
The Tradeoff
CRDTs eliminate conflicts at the cost of restricting what operations are possible. Not every data structure has a conflict-free design. They work beautifully for collaborative editing, shopping carts, and distributed counters.
Key Takeaway: CRDTs use mathematical properties to guarantee automatic convergence across replicas without coordination, eliminating conflicts by design rather than detecting and resolving them.