QA
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→

More Software Engineering Principles Interview Questions

Beginner LevelSoftware Engineering Principles

Explain the 5 SOLID Principles of Object-Oriented Software Design with practical refactoring examples.

Beginner LevelSoftware Engineering Principles

What are DRY (Don't Repeat Yourself), KISS (Keep It Simple, Stupid), and YAGNI (You Aren't Gonna Need It)?

Beginner LevelSoftware Engineering Principles

Why is Composition Over Inheritance a fundamental object-oriented design principle?