Intermediate LevelSystem Design & Architecture
How do Vector Clocks detect concurrent write conflicts in masterless databases?
β Interview Prompt: Explain Vector Clocks, causality tracking, vector arrays ($V[1..N]$), concurrent write conflicts, and Last-Write-Wins (LWW).
Vector Clocks track causal relationships and detect concurrent write conflicts in masterless distributed databases (Riak, Dynamo):
Conflict Detection:
Each node maintains a vector array of version counters ($V[1..N]$):
β’
If Vector Clock $A$ dominates Vector Clock $B$ ($V_A > V_B$), state $A$ causally succeeds state $B$.
β’
If neither vector dominates ($V_A \parallel V_B$), a concurrent write conflict occurred! The database preserves both sibling values for client or Last-Write-Wins (LWW) resolution.
Code Implementation Example
typescript
type VectorClock = Record<string, number>;
// Check if Clock A causally dominates Clock B
export function isDominating(a: VectorClock, b: VectorClock): boolean {
let greater = false;
for (const node in b) {
if ((a[node] || 0) < (b[node] || 0)) return false;
if ((a[node] || 0) > (b[node] || 0)) greater = true;
}
return greater;
}π‘Comparing Vector Clocks to evaluate causality.
π― Interview Key Takeaway
Use Vector Clocks in masterless distributed databases to detect concurrent write conflicts and track causality.
#System Design#Vector Clocks#Conflict Resolution#Cassandra#Intermediate
Back to all System Design & Architecture Questionsβ