
Sharding is the nuclear option of database scaling. You don't shard because you want to; you shard because your primary writer is saturated, vertical scaling hit a physical limit (or AWS bill limit), and read replicas can't solve write throughput bottlenecks.
When you cross that threshold, you trade single-node relational guarantees (ACID transactions, cross-table foreign keys, global unique constraints) for horizontal scalability. This guide covers how modern systems actually shard relational databases, how to choose a sharding key, and how to handle the distributed system failure modes that follow.
Analyze queries for sargability, missing composite indexes & offsets
Analyze SQL queries for performance anti-patterns, wildcards, and slow offsets. Generates optimized queries and DDL composite indexes for PostgreSQL, MySQL, and SQLite.
Before splitting your database into shards, ensure you have exhausted these intermediate steps:
Scaling Stages:
1. Index Optimization & Slow Query Tuning (EXPLAIN ANALYZE)
│ (hit CPU/IOPS limits)
▼
2. Read/Write Splitting (Primary for writes, Replicas for reads)
│ (write throughput saturates primary node)
▼
3. Table Partitioning (PostgreSQL declarative partitioning / MySQL partition)
│ (single machine disk/memory/lock contention saturated)
▼
4. Functional Partitioning (Extract Auth, Billing, Analytics to isolated DBs)
│ (a single domain table like `orders` or `events` exceeds 1 node capacity)
▼
5. Horizontal Sharding (Split identical table schema across N database nodes)
If a single table exceeds 500M+ rows, write operations generate continuous lock contention, and WAL/redo replication lag grows faster than replicas can ingest, you need horizontal sharding.
The Sharding Key determines which database shard holds a given row. Choosing the wrong key leads to hot spots, cross-shard scatter-gather queries, and nightmare migrations.
Rows are routed based on continuous value ranges (e.g., created_at date ranges or ID ranges 1–1,000,000 -> Shard 1, 1,000,001–2,000,000 -> Shard 2).
Pass the sharding key through a cryptographic or fast non-cryptographic hash function (like MurmurHash3 or xxHash) modulo the number of shards, or map onto a consistent hash ring with virtual nodes:
\text{Shard ID} = \text{hash}(\text{sharding\_key}) \pmod N
import mmh3
class ShardRouter:
def __init__(self, shard_count: int):
self.shard_count = shard_count
def get_shard_id(self, entity_id: str) -> int:
# MurmurHash3 ensures uniform distribution across shards
hash_val = mmh3.hash(str(entity_id))
return abs(hash_val) % self.shard_count
router = ShardRouter(shard_count=8)
print(router.get_shard_id("user_98314")) # -> Shard 3
print(router.get_shard_id("user_98315")) # -> Shard 7
WHERE created_at BETWEEN x AND y) must broadcast to all shards (scatter-gather).A central, highly cached lookup service (e.g., Redis + metadata database) maps entity_id to shard_id.
In application-level sharding, your service layer manages connection pools per shard and routes queries based on context:
import asyncpg
from typing import Dict, Any
class ShardedDatabaseManager:
def __init__(self, shard_configs: Dict[int, str]):
self.configs = shard_configs
self.pools: Dict[int, asyncpg.Pool] = {}
async def initialize(self):
for shard_id, dsn in self.configs.items():
self.pools[shard_id] = await asyncpg.create_pool(
dsn, min_size=5, max_size=20, timeout=10.0
)
def get_shard_for_tenant(self, tenant_id: str) -> int:
import hashlib
h = int(hashlib.md5(tenant_id.encode()).hexdigest(), 16)
return h % len(self.pools)
async def execute_query(
self, tenant_id: str, query: str, *args
) -> list[Dict[str, Any]]:
shard_id = self.get_shard_for_tenant(tenant_id)
pool = self.pools[shard_id]
async with pool.acquire() as conn:
records = await conn.fetch(query, *args)
return [dict(r) for r in records]
async def scatter_gather(self, query: str, *args) -> list[Dict[str, Any]]:
"""Broadcast read query to all shards concurrently and aggregate results."""
import asyncio
async def query_shard(shard_id: int, pool: asyncpg.Pool):
async with pool.acquire() as conn:
return await conn.fetch(query, *args)
tasks = [query_shard(sid, pool) for sid, pool in self.pools.items()]
results = await asyncio.gather(*tasks, return_exceptions=False)
flat = []
for r in results:
flat.extend([dict(row) for row in r])
return flat
You can no longer rely on AUTO_INCREMENT or PostgreSQL BIGSERIAL sequences because each shard will produce colliding primary keys.
Solutions:
[Timestamp (41 bits) | Worker ID (10 bits) | Sequence (12 bits)]. Generates millions of sortable unique IDs per second without database locks.Snowflake 64-Bit Structure:
[ 1 bit sign ] [ 41 bits timestamp (ms) ] [ 10 bits machine/shard ID ] [ 12 bits sequence ]
If orders is sharded by tenant_id and products is global, joining them directly in SQL across different physical servers is impossible.
users, orders, and order_items all contain tenant_id as their sharding key, queries restricted to a single tenant_id execute as 100% local joins on that single shard.currencies, countries, plan_tiers) are copied to every shard. Updates are broadcast to all nodes via Change Data Capture (Debezium/Kafka).orders from Shard A, fetch unique product_ids, execute a batch query SELECT * FROM products WHERE id = ANY(...) on the catalog database, and stitch the DTOs together in the backend service.When a business operation touches two different shards (e.g., transferring money from Account on Shard 1 to Account on Shard 2), standard BEGIN ... COMMIT cannot guarantee atomicity.
A transaction coordinator asks all participating shards: "Can you commit?" (Prepare Phase). If all reply YES, coordinator logs commit and issues "Commit!" (Commit Phase).
Replace synchronous distributed locks with asynchronous compensating transactions:
1. [Shard 1] Deduct $100 from Account A -> Write "MoneyDebited" event to local `outbox` table (Atomic local commit)
2. CDC worker reads `outbox` -> Publishes to Kafka
3. Worker reads Kafka -> [Shard 2] Credit $100 to Account B
4. If Shard 2 fails permanently -> Emit "CreditFailed" event -> Compensating transaction on Shard 1 to refund $100 to Account A
Instead of maintaining custom routing layers in your application code, modern architectures often rely on proxy or distributed storage layers:
| Approach | Examples | How It Works | Best For |
|---|---|---|---|
| Transparent Proxy | Vitess (MySQL), Citus (Postgres) | Sits between application and standard MySQL/Postgres. App speaks standard SQL; proxy parses AST and routes shards. | Scaling existing monolithic relational databases without rewriting application SQL. |
| Distributed SQL (Native) | CockroachDB, TiDB, Google Spanner | Storage engine is natively distributed (Raft consensus + LSM trees/Pebble). Shards (ranges) split and rebalance automatically. | Greenfield cloud-native systems needing horizontal writes with standard ACID transactions. |
| App-Level Sharding | Custom Router / Hibernate Shards / SQLAlchemy Routing | Application explicitly selects connection string based on entity ID. | Extreme high throughput where proxy latency overhead (1–2ms) is unacceptable. |
At some point, your 8 shards will run out of capacity and you must expand to 16 shards without taking the platform offline:
A rapidly growing e-commerce platform is experiencing write throughput bottlenecks on its primary database. They've already optimized queries, implemented read replicas, and partitioned large tables. The `orders` table, which is central to their business, is now exceeding 500M rows and causing continuous lock contention. Which sharding strategy would be most appropriate to address the immediate write throughput issue while minimizing the risk of new write hotspots?
Clean AI CLI logs, build cron expressions, decode JWTs, and calculate chmod permissions offline.

A comprehensive guide to modern database seeding strategies using Prisma and TypeScript. We explore idempotency, Faker.js integration, complex relational data handling, and performance optimization for large seed scripts.
Read more
Diagnose and eliminate PostgreSQL table and index bloat. Master autovacuum tuning formulas, pg_repack zero-downtime compaction, and MVCC visibility maps.
Read more
Zero-downtime change data capture (CDC) with PostgreSQL logical replication, Debezium, and Kafka: WAL management, slot lag, and failover resilience.
Read more