How to choose between strong and eventual consistency
· Category: System Design
Short answer
Strong consistency guarantees that all readers see the latest write, while eventual consistency allows temporary divergence for higher availability and performance.
Steps
- Analyze business requirements to determine tolerance for stale reads.
- Choose strong consistency for financial transactions, inventory, and critical configuration.
- Choose eventual consistency for social feeds, analytics, and recommendation rankings.
- Implement read-replicas with replication lag monitoring for hybrid approaches.
- Communicate consistency guarantees clearly to application developers.
Tips
- Use quorum-based systems like Paxos or Raft when strong consistency is required.
- Design idempotent operations so retries are safe under uncertainty.
- Expose replication lag metrics to help clients decide when to force strong reads.
- Accept that strong consistency across wide areas incurs latency penalties.
Common issues
- Developers assuming strong consistency in eventually consistent stores.
- Stale reads causing confusing user experiences in rapidly changing data.
- Conflict resolution complexity in multi-master replication.
- Performance collapse when forcing cross-region strong consistency.
Example
# Consistent hashing for service discovery
import hashlib
def get_node(key, nodes):
hash_val = int(hashlib.md5(key.encode()).hexdigest(), 16)
return nodes[hash_val % len(nodes)]
node = get_node('user-123', ['node-a', 'node-b', 'node-c'])
This snippet implements consistent hashing to distribute keys across nodes, a foundational technique in scalable distributed systems.