Consistency Models and Conflict Resolution Strategies
The Consistency Trade-off
Key-value stores span from strong consistency to eventual consistency. Strong consistency guarantees reads return the latest write, but adds 10-50ms latency for synchronous replication and may refuse writes during network partitions. Eventual consistency serves reads in under 5ms but risks stale reads.
Read and Write Quorums
Tunable consistency uses quorums: R nodes must agree on reads, W nodes must acknowledge writes. With replication factor N, if R + W > N, reads and writes overlap, guaranteeing reads see latest writes. Common configuration: N=3, R=2, W=2 balances consistency and availability. Lower quorums (R=1) return faster but may be stale.
Conflict Resolution: Last Writer Wins
When concurrent writes occur to the same key, the system must choose a winner. Last-writer-wins (LWW) uses timestamps: highest timestamp wins. Simple but can lose data if clocks skew or writes happen within clock resolution. NTP (Network Time Protocol, which synchronizes clocks across networked computers) keeps clocks within milliseconds, but edge cases remain. Applications must tolerate occasional lost updates.
Conflict Resolution: Vector Clocks
Vector clocks track causal relationships between writes by maintaining a counter per node. When two writes have incomparable vectors (neither happened-before the other), the system surfaces them as conflicts for application resolution rather than silently picking a winner. More complex but preserves all data.
CRDTs: Conflict-Free Data Types
CRDTs (Conflict-free Replicated Data Types) are data structures designed to merge automatically without conflicts. Counters, sets, and registers have CRDT variants that converge to consistent state regardless of update order. They eliminate conflict resolution complexity but constrain what data structures you can use.