Senior LevelSystem Design & Architecture
How do State-Based and Operation-Based CRDTs achieve Strong Eventual Consistency?
β Interview Prompt: Explain CRDTs (Conflict-Free Replicated Data Types), State-based (CvRDT) vs Operation-based (CmRDT), join-semilattices, and LWW-Element-Set.
Conflict-Free Replicated Data Types (CRDTs) achieve Strong Eventual Consistency (SEC) across distributed replicas without central locks or consensus rounds:
Mathematical Guarantees:
1
State-Based CRDTs (CvRDT): Replicas send full states. Merges use a Join-Semilattice operator that is Commutative ($A \lor B = B \lor A$), Associative, and Idempotent ($A \lor A = A$).
2
Operation-Based CRDTs (CmRDT): Replicas stream concurrent edit operations. Requires causal delivery guarantees.
Code Implementation Example
typescript
// State-based LWW (Last-Write-Wins) Element Set CRDT
class LWWRegister<T> {
constructor(public value: T, public timestamp: number) {}
// Merge operator: Commutative, Associative, Idempotent
merge(other: LWWRegister<T>): LWWRegister<T> {
return other.timestamp > this.timestamp ? other : this;
}
}π‘Implementing a Last-Write-Wins (LWW) Register CRDT.
π― Interview Key Takeaway
Use CRDTs with commutative merge operators to achieve strong eventual consistency across distributed replicas.
#System Design#CRDT#Eventual Consistency#Mathematics#Senior
Back to all System Design & Architecture Questionsβ