System Design (50 Qs)•Part 2: Real-World High-Scale Architecture Problems (11 - 25)
Design a Distributed Key-Value Store (Dynamo-Style Vector Clocks & Gossip Protocol).
In Simple Words: Design an Amazon Dynamo-style highly available, partition-tolerant distributed key-value store with eventual consistency and decentralized masterless architecture.
Key Technical Pillars:
- Decentralized Membership: Nodes monitor cluster health via Gossip Protocol (epidemic information dissemination).
- Partitioning: Consistent Hashing ring with virtual nodes.
- Data Versioning: Vector Clocks to detect concurrent write conflicts.
- Read Repair & Anti-Entropy: Inconsistencies detected during read operations are repaired asynchronously in the background.
typescript
// Vector Clock Conflict Detection Logic
export type VectorClock = Record<string, number>;
export function compareVectorClocks(v1: VectorClock, v2: VectorClock): 'EQUAL' | 'BEFORE' | 'AFTER' | 'CONCURRENT' {
let v1Bigger = false;
let v2Bigger = false;
const allNodes = new Set([...Object.keys(v1), ...Object.keys(v2)]);
for (const node of allNodes) {
const c1 = v1[node] || 0;
const c2 = v2[node] || 0;
if (c1 > c2) v1Bigger = true;
if (c2 > c1) v2Bigger = true;
}
if (v1Bigger && !v2Bigger) return 'AFTER';
if (!v1Bigger && v2Bigger) return 'BEFORE';
if (!v1Bigger && !v2Bigger) return 'EQUAL';
return 'CONCURRENT'; // Conflict! Requires client or LWW resolution
}