🔄 Conflict-free Replicated Data Types (CRDTs)

Distributed data structures with guaranteed convergence

Overview

CRDTs are data structures designed for distributed systems where replicas update independently without coordination. They guarantee Strong Eventual Consistency (SEC): any two replicas that have received the same set of updates will be in the same state, regardless of message ordering, duplication, or delays.

This is achieved through merge operations satisfying three algebraic properties: commutativity, associativity, and idempotency — forming a join-semilattice.

Concepts Demonstrated

Implemented CRDTs

TypeDescriptionUse Case
GCounterGrow-only counterPage views, likes
PNCounterIncrement/decrement counterStock levels, votes
GSetGrow-only setTag collections, seen-message IDs
TwoPhaseSetAdd-once, remove-once setUser ban lists
LWWRegisterLast-writer-wins registerUser profile fields
ORSetObserved-remove setShopping carts, collaborative editing
MVRegisterMulti-value registerConflict-preserving fields

How It Works

(* Each replica maintains local state *)
(* Updates are applied locally — always succeed *)
(* Replicas periodically exchange state and merge *)

(* G-Counter example: *)
let c = GCounter.create ()
  |> GCounter.increment "nodeA" 3
  |> GCounter.increment "nodeB" 5
(* GCounter.value c = 8 *)

(* Merge two diverged replicas: *)
let merged = GCounter.merge replica1 replica2
(* Takes pointwise maximum → convergence guaranteed *)

(* Key insight: merge is commutative, associative, idempotent *)
(* merge(A,B) = merge(B,A)                    — order doesn't matter *)
(* merge(merge(A,B),C) = merge(A,merge(B,C))  — grouping doesn't matter *)
(* merge(A,A) = A                              — duplicates are harmless *)

CRDTs vs STM

Compare with Software Transactional Memory:

CRDTs trade consistency strength for availability (AP in CAP theorem). STM trades availability for consistency (CP).

Key Takeaways