Senior LevelSoftware Engineering Principles
What are Conflict-Free Replicated Data Types (CRDTs) and how do they enable eventual consistency without central locks?
❓ Interview Prompt: Explain CRDTs (Operation-based vs State-based), LWW-Element-Set, PN-Counters, collaborative editing (Figma/Google Docs), and peer-to-peer merging.
Conflict-Free Replicated Data Types (CRDTs) are data structures that can be replicated across multiple nodes in a distributed network and merged concurrently without central coordination or lock conflicts:
Core Properties:
•
Mathematical Commutativity, Associativity, & Idempotency: Merging updates in any order produces the exact same final state everywhere!
•
Types: State-based (CvRDT) and Operation-based (CmRDT).
•
Applications: Real-time collaborative document editing (Figma, Notion) and peer-to-peer offline mobile sync.
Code Implementation Example
typescript
// State-based PN-Counter CRDT
class PNCounter {
private P: Record<string, number> = {}; // Increments
private N: Record<string, number> = {}; // Decrements
value(): number {
const pos = Object.values(this.P).reduce((a, b) => a + b, 0);
const neg = Object.values(this.N).reduce((a, b) => a + b, 0);
return pos - neg;
}
// Merge function: Take maximum count per node (Commutative & Idempotent!)
merge(other: PNCounter) {
for (const k in other.P) this.P[k] = Math.max(this.P[k] || 0, other.P[k]);
for (const k in other.N) this.N[k] = Math.max(this.N[k] || 0, other.N[k]);
}
}💡Implementing a State-based PN-Counter CRDT in TypeScript.
🎯 Interview Key Takeaway
Use CRDTs to achieve conflict-free eventual consistency in collaborative real-time and offline peer-to-peer applications.
#CRDT#Distributed Systems#Collaborative Editing#Senior
Back to all Software Engineering Principles Questions→