Sharding — Explained with Examples
Sharding is a database architecture pattern that horizontally partitions data across multiple database instances to improve scalability and performance.
Sharding, also called horizontal partitioning, splits a large dataset across multiple database servers (shards). Each shard holds a subset of the data and operates as an independent database. A shard key determines which shard stores each row.
Why Sharding Matters
Single database servers have finite storage and processing capacity. When a dataset outgrows a single machine, indexing, backup, and query performance degrade. Sharding distributes the load across machines, allowing the system to scale horizontally by adding more shards.
Real-World Analogy
A single filing cabinet can hold only so many files. When it overflows, you don’t buy a taller cabinet — you buy a second cabinet and alphabetically split files: A-M in cabinet 1, N-Z in cabinet 2. Sharding is the same principle applied to databases.
Example: Sharding Strategy
import hashlib
class ShardManager:
def __init__(self, shards: list):
self.shards = shards # list of database connections
def _get_shard(self, user_id: str):
# Consistent hashing to determine shard
hash_val = int(hashlib.md5(user_id.encode()).hexdigest(), 16)
return self.shards[hash_val % len(self.shards)]
def find_user(self, user_id: str):
shard = self._get_shard(user_id)
return shard.query(f"SELECT * FROM users WHERE id = '{user_id}'")
def create_user(self, user_id: str, data: dict):
shard = self._get_shard(user_id)
shard.execute(f"INSERT INTO users (id, name) VALUES ('{user_id}', '{data['name']}')")
# Usage
shards = [Shard("db1"), Shard("db2"), Shard("db3")]
manager = ShardManager(shards)
manager.create_user("user_123", {"name": "Alice"})
user = manager.find_user("user_123")Related Terms
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro