How to Replicate Data Across All Shards in Python
Mocks a global table that replicates a key-value pair to every shard, ensuring reads return the same value from any shard.
Python code
35 linesfrom dataclasses import dataclass
from typing import Dict, List
@dataclass
class Shard:
id: str
data: Dict[str, int]
class GlobalTable:
def __init__(self, shards: List[Shard]):
self._shards = {s.id: s for s in shards}
def set_value(self, key: str, value: int) -> None:
"""Replicate a key-value pair to all shards."""
for shard in self._shards.values():
shard.data[key] = value
def get_value(self, key: str) -> int:
"""Return the value from the first shard (same on all)."""
first = next(iter(self._shards.values()))
return first.data[key]
if __name__ == "__main__":
shard_a = Shard(id="shard-a", data={"counter": 0})
shard_b = Shard(id="shard-b", data={"counter": 10})
table = GlobalTable([shard_a, shard_b])
table.set_value("counter", 42)
print(shard_a.data["counter"])
print(shard_b.data["counter"])
print(table.get_value("counter"))
Output
42
42
42
How it works
The GlobalTable class stores shards in a dictionary keyed by shard ID, and set_value iterates over all shards to write the same key-value pair to each one. The get_value method returns from the first shard, which is safe because replication guarantees consistency. The @dataclass decorator auto-generates __init__ methods for the Shard class, keeping the code concise. This pattern mirrors real-world data replication where writes fan out to all replicas for read consistency.
Common mistakes
- Assuming shards are always in the same order when using `next(iter(...))` — use a consistent ordering strategy if needed
- Forgetting that mutations via `set_value` are synchronous and block until all shards are updated
- Ignoring failure handling when a shard write fails or is temporarily unavailable
Variations
- Use a `for shard in self._shards.values()` loop with error logging for partial failures instead of silently succeeding
- Store shards in a list and maintain a separate index for faster first-shard access
Real-world use cases
- Testing distributed database logic where every read replica must return the same value after a write.
- Simulating leaderless replication in a classroom or interview environment before implementing with actual databases.
- Building a lightweight in-memory mock for integration tests that need multi-node consistency without external dependencies.
Sponsored
More from Database scaling & optimization
- Approximate Count with HyperLogLog in Python medium
- B-Tree Insert and In-Order Traversal in Python hard
- Broadcast a Small Reference Table in Python easy
- Build a Full Text Search Index in Python medium
- Build a Partial Index Mock in Python for Database Filtering easy
- Composite index leftmost prefix in Python medium
Keep learning
Related tutorials and quizzes for this topic.