Practical patterns for building eventually consistent distributed systems.
Eventual consistency allows systems to achieve high availability and performance while ensuring all replicas converge to the same state. This project provides battle-tested patterns and anti-entropy mechanisms.
Version: 0.1.0
- ✅ Read Repair — Fix inconsistencies during reads
- ✅ Anti-Entropy — Periodic reconciliation
- ✅ Conflict Resolution — LWW, vector clocks, CRDTs
- ✅ Causal Consistency — Preserve causality
from eventual_consistency import ReadRepair
repair = ReadRepair(replicas=["node1", "node2", "node3"])
# Read with repair
value = repair.read(key="user:123")
# Automatically repairs stale replicas in backgrounddef read_with_repair(key):
# Read from multiple replicas
values = [replica.get(key) for replica in replicas]
# Pick latest version
latest = max(values, key=lambda v: v.timestamp)
# Repair stale replicas asynchronously
for replica, value in zip(replicas, values):
if value.timestamp < latest.timestamp:
repair_replica(replica, key, latest)
return latestfrom anti_entropy import MerkleSync
sync = MerkleSync()
# Periodic reconciliation
while True:
for peer in peers:
diffs = sync.compare_trees(peer)
sync.repair_differences(peer, diffs)
time.sleep(300) # Every 5 minutes# Last-Write-Wins
def resolve_lww(v1, v2):
return v1 if v1.timestamp > v2.timestamp else v2
# Vector Clock
def resolve_vector_clock(v1, v2):
if v1.vector_clock.happens_before(v2.vector_clock):
return v2
elif v2.vector_clock.happens_before(v1.vector_clock):
return v1
else:
# Concurrent - application-specific resolution
return merge_concurrent(v1, v2)- Strong consistency: All reads see latest write
- Eventual consistency: All replicas converge eventually
- Causal consistency: Causally related operations ordered
- Read-your-writes: See your own writes
- Shopping carts
- Social media feeds
- Collaborative editing
- Distributed caches
pip install git+https://github.com/navinBRuas/_DistributedSystems#subdirectory=eventual-consistency-patternsSee the sections above and examples/ for usage patterns.
No mandatory configuration. Optional settings are documented in the package code and examples.
0.1.0 (see VERSION.md)
See CHANGELOG.md.
MIT License. See repo root LICENSE.