StackPractices
advanced By Mathias Paulenko

Complete Guide to Database Sharding

Master database sharding: shard keys, partitioning strategies, consistent hashing, cross-shard queries, resharding, Vitess and Citus.

Introduction

Sharding breaks a database into smaller pieces called shards, each living on its own server and holding a slice of the data. By the end you’ll know how to pick a shard key, route queries, reshard without downtime, and choose between Vitess, Citus, and a natively-sharded database.

When to Shard

Shard when:
  - Data does not fit on a single machine (disk or memory)
  - Tables pass ~1TB and backups no longer fit the window
  - Writes exceed ~5k/sec on the biggest instance you can buy
  - Query latency increases as data grows
  - Maintenance (index rebuilds, schema changes) takes hours
  - You need geographic data distribution

Do NOT shard when:
  - Data fits on a single machine with headroom
  - You can scale vertically (bigger server, more RAM, SSD)
  - Application logic cannot handle cross-shard complexity
  - You need ACID transactions across shards

Sharding adds:
  - Operational complexity (more servers to manage)
  - Application complexity (routing queries to shards)
  - Cross-shard query limitations
  - Resharding difficulty when the shard key changes

Rule: shard last, after vertical scaling, read replicas, and caching.

Partition First: Single-Node Partitioning

Sharding is horizontal partitioning spread across servers. Two cheaper forms of partitioning live inside a single database, and they solve real problems before you ever need a second node. Try them first — see the Complete Guide to SQL Query Optimization for the full checklist.

Vertical Partitioning

Split the columns of a wide table by access pattern: hot columns stay in the base table, cold columns move to a side table.

-- Before: single wide table
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50),
    email VARCHAR(100),
    profile_json JSONB,
    avatar_url VARCHAR(500),
    preferences_json JSONB,
    created_at TIMESTAMP
);

-- After: frequently accessed columns in users, rarely accessed in user_profiles
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50),
    email VARCHAR(100),
    created_at TIMESTAMP
);

CREATE TABLE user_profiles (
    user_id INT PRIMARY KEY REFERENCES users(id),
    profile_json JSONB,
    avatar_url VARCHAR(500),
    preferences_json JSONB
);

Use it when some columns are read 100x more often than others — it cuts I/O on hot queries without adding servers.

Declarative Range Partitioning (PostgreSQL)

Keep rows in one table logically, stored as separate physical partitions on the same server.

CREATE TABLE events (
    id BIGSERIAL,
    event_type VARCHAR(50),
    created_at TIMESTAMP NOT NULL,
    data JSONB
) PARTITION BY RANGE (created_at);

CREATE TABLE events_2024_01 PARTITION OF events
    FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');

CREATE TABLE events_2024_02 PARTITION OF events
    FOR VALUES FROM ('2024-02-01') TO ('2024-03-01');

Benefits: partition pruning (queries scan only the relevant partitions), cheap archival (DETACH PARTITION drops an old month instantly), and faster VACUUM/ANALYZE per partition.

Partitioning Strategies

Range-Based Sharding

Range-based sharding assigns data to shards based on value ranges of the shard key.

Shard 1: user_id 1 - 1,000,000
Shard 2: user_id 1,000,001 - 2,000,000
Shard 3: user_id 2,000,001 - 3,000,000
def get_shard(user_id: int) -> int:
    if user_id <= 1_000_000:
        return 1
    elif user_id <= 2_000_000:
        return 2
    else:
        return 3

# Pros: range queries are efficient (scan one shard)
# Cons: hot spots — recent users go to the last shard
#       uneven distribution if keys are not uniform

Hash-Based Sharding

Hash-based sharding applies a hash function to the shard key to determine the shard.

import hashlib

def get_shard(user_id: str, num_shards: int = 4) -> int:
    hash_value = int(hashlib.md5(user_id.encode()).hexdigest(), 16)
    return hash_value % num_shards

# Pros: even distribution, no hot spots
# Cons: range queries require scanning all shards
#       resharding is expensive (all data must be redistributed)

# Example distribution
users = ["alice", "bob", "charlie", "diana", "eve", "frank"]
for user in users:
    print(f"{user}: shard {get_shard(user)}")
# alice: shard 2
# bob: shard 0
# charlie: shard 3
# diana: shard 1
# eve: shard 2
# frank: shard 0

Directory-Based Sharding

A lookup table maps shard keys to shards. A dedicated service manages the mapping.

import redis

# Shard directory stored in Redis
r = redis.Redis(host="localhost", port=6379)

# Initialize shard mapping
shard_mapping = {
    "us-east": "shard1.example.com",
    "us-west": "shard2.example.com",
    "eu-central": "shard3.example.com",
    "asia-pacific": "shard4.example.com",
}

for region, host in shard_mapping.items():
    r.hset("shard_directory", region, host)

def get_shard_connection(region: str) -> str:
    host = r.hget("shard_directory", region)
    if not host:
        raise ValueError(f"Unknown region: {region}")
    return host.decode()

# Pros: flexible — can move data between shards without changing the hash function
# Cons: lookup adds latency, directory is a single point of failure
#       requires a highly available directory service

List-Based Sharding

Map discrete values of the shard key to specific shards — the explicit form of directory sharding.

Shard 1: region IN ('us-east', 'us-west')
Shard 2: region IN ('eu-west', 'eu-central')
Shard 3: tenant_tier = 'enterprise'

Best for compliance-driven placement (EU data stays in the EU) or isolating premium tenants on dedicated hardware. The cost is manual maintenance: every reassignment is a directory update plus a data move.

Consistent Hashing

Consistent hashing minimizes data movement when adding or removing shards.

import hashlib
import bisect

class ConsistentHashRing:
    def __init__(self, virtual_nodes: int = 150):
        self.virtual_nodes = virtual_nodes
        self.ring: list[tuple[int, str]] = []
        self.sorted_keys: list[int] = []
    
    def _hash(self, key: str) -> int:
        return int(hashlib.md5(key.encode()).hexdigest(), 16)
    
    def add_node(self, node: str) -> None:
        for i in range(self.virtual_nodes):
            hash_val = self._hash(f"{node}:{i}")
            bisect.insort(self.sorted_keys, hash_val)
            self.ring.insert(
                bisect.bisect_left(self.sorted_keys, hash_val),
                (hash_val, node)
            )
    
    def remove_node(self, node: str) -> None:
        self.ring = [(h, n) for h, n in self.ring if n != node]
        self.sorted_keys = [h for h, n in self.ring]
    
    def get_node(self, key: str) -> str:
        if not self.sorted_keys:
            raise ValueError("No nodes in ring")
        hash_val = self._hash(key)
        idx = bisect.bisect_right(self.sorted_keys, hash_val)
        if idx == len(self.sorted_keys):
            idx = 0
        return self.ring[idx][1]

# Usage
ring = ConsistentHashRing(virtual_nodes=150)
ring.add_node("shard1.example.com")
ring.add_node("shard2.example.com")
ring.add_node("shard3.example.com")

print(ring.get_node("user:12345"))  # shard2.example.com
print(ring.get_node("user:67890"))  # shard1.example.com

# Adding a node only moves a fraction of keys
ring.add_node("shard4.example.com")
# Only ~25% of keys move to the new node
Consistent hashing benefits:
  - Adding a node: only K/N keys move (K = total keys, N = nodes)
  - Removing a node: only K/N keys move
  - Virtual nodes improve distribution uniformity
  - No need to rehash all keys when topology changes

Used by: Cassandra, DynamoDB, Redis Cluster, Memcached clients

Shard Key Selection

Good shard key properties:
  - High cardinality — many distinct values for even distribution
  - Low frequency — no single value dominates (avoids hot spots)
  - Non-monotonic — does not always increase (avoids all new data on one shard)
  - Immutable — never changes; changing a key means moving the row
  - Query-relevant — most queries include the shard key (avoids scatter)

Bad shard keys:
  - Auto-increment ID — monotonic, all new data goes to the last shard
  - Timestamp — recent data concentrated on one shard
  - Low cardinality field (e.g., country with 3 values) — uneven distribution
  - Field not in queries — every query scans all shards

Good shard keys:
  - User ID (UUID) — high cardinality, included in most queries
  - Hash of (user_id + timestamp) — non-monotonic, high cardinality
  - Composite key (user_id, created_at) — supports range queries per user
# Example: choosing a shard key for a multi-tenant app
# Bad: shard by tenant_id — one large tenant overwhelms a shard
# Good: shard by (tenant_id, user_id) — distributes within a tenant

def get_shard_key(tenant_id: str, user_id: str) -> str:
    return f"{tenant_id}:{user_id}"

# Most queries include tenant_id and user_id
# SELECT * FROM orders WHERE tenant_id = 'acme' AND user_id = 'u123'
# Routes to a single shard

Common shard keys by workload:

WorkloadShard keyWhy it works
Multi-tenant SaaStenant_idNatural isolation per customer
Social platformuser_idA user’s data is read together
E-commercecustomer_idOrders co-located with the buyer
Time-series(device_id, timestamp)Composite key spreads recent writes
Gamingplayer_idSession and inventory stay local

The Hotspot Problem

Even with hash sharding, a single key can dominate writes:

Shard 1: 45% of writes (celebrity user with 10M followers)
Shard 2: 15% of writes
Shard 3: 20% of writes
Shard 4: 20% of writes

Mitigations:

  • Sub-sharding — split the hot key further, for example by post_id inside the celebrity’s data.
  • Write splitting — fan out writes for the hot key across several shards and aggregate at read time.
  • Cache layer — absorb hot-key reads in Redis so the shard only serves writes. See caching strategies.

Cross-Shard Queries

-- Single-shard query (efficient — includes shard key)
SELECT * FROM orders WHERE user_id = 123 AND created_at > '2026-01-01';

-- Cross-shard query (scatter-gather — query all shards)
SELECT * FROM orders WHERE status = 'pending';
-- Router sends query to all shards, merges results

-- Cross-shard aggregation (expensive)
SELECT COUNT(*) FROM orders WHERE created_at > '2026-01-01';
-- Each shard counts, router sums the counts

-- Cross-shard join (very expensive — avoid)
SELECT u.name, o.total
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE o.status = 'shipped';
-- Requires fetching data from multiple shards and joining in memory
# Scatter-gather pattern in application code
import concurrent.futures

def scatter_gather(query: str, shards: list[str]) -> list[dict]:
    results = []
    
    with concurrent.futures.ThreadPoolExecutor(max_workers=len(shards)) as executor:
        futures = {
            executor.submit(execute_on_shard, shard, query): shard
            for shard in shards
        }
        
        for future in concurrent.futures.as_completed(futures):
            shard = futures[future]
            try:
                shard_results = future.result()
                results.extend(shard_results)
            except Exception as e:
                print(f"Error on {shard}: {e}")
    
    return results

def execute_on_shard(shard: str, query: str) -> list[dict]:
    # Connect to shard and execute query
    conn = connect_to_shard(shard)
    return conn.execute(query).fetchall()

Queries Without the Shard Key

When a query can’t carry the shard key, scatter-gather is the fallback — but three workarounds cover most cases:

ProblemWorkaroundCost
Lookup by a secondary field (email, slug)Index table mapping the field to the shard keyExtra table to keep in sync
Uniqueness across shardsUUID or Snowflake IDs instead of auto-incrementNo ordering guarantees
Global aggregates and analyticsPre-aggregate per shard or ship to a warehouseStale numbers, extra system

This is also why sharded schemas lean on denormalization: every join you move into application code is a join the router doesn’t have to fake.

Resharding

When to reshard:
  - Data grows beyond current shard capacity
  - Hot shard (uneven distribution)
  - Adding or removing shards

Resharding strategies:
  1. Dual-write: write to both old and new shards, backfill, then switch reads
  2. Capture-change: stream changes from old shards to new, then switch
  3. Offline: stop writes, migrate data, restart (downtime)

Dual-write process:
  1. Add new shards alongside old ones
  2. Write to both old and new (dual-write)
  3. Backfill existing data from old to new
  4. Verify data consistency
  5. Switch reads to new shards
  6. Stop writes to old shards
  7. Decommission old shards

Managed tools do this online: Vitess MoveTables and citus.rebalance_table_shards() stream data between shards without a write freeze. For small datasets, snapshot-and-replay during a maintenance window is the cheap version.

# Dual-write example
import hashlib

class DualWriteRouter:
    def __init__(self, old_shards, new_shards):
        self.old_shards = old_shards
        self.new_shards = new_shards
        self.read_from = "old"  # Switch to "new" after verification
    
    def write(self, shard_key: str, data: dict):
        # Write to both old and new
        old_shard = self.get_shard(shard_key, self.old_shards)
        new_shard = self.get_shard(shard_key, self.new_shards)
        
        old_shard.insert(data)
        new_shard.insert(data)
    
    def read(self, shard_key: str, query: str):
        if self.read_from == "old":
            return self.get_shard(shard_key, self.old_shards).query(query)
        else:
            return self.get_shard(shard_key, self.new_shards).query(query)
    
    def get_shard(self, key: str, shards: list) -> object:
        # hash() varies between processes — use a stable hash instead
        idx = int(hashlib.md5(str(key).encode()).hexdigest(), 16) % len(shards)
        return shards[idx]

Vitess

Vitess is a database clustering system for horizontal scaling of MySQL.

# vttablet configuration
tablet:
  keyspace: commerce
  shard: 0
  tablet_alias: zone1-0000000100

db:
  host: localhost
  port: 3306
  user: vt_app
  password: vt_password
  dbname: vt_commerce
-- Vitess uses VSchema for cross-shard queries
-- VSchema defines how tables are sharded

-- Sharded table (by user_id)
CREATE TABLE orders (
  id BIGINT PRIMARY KEY,
  user_id BIGINT NOT NULL,
  total DECIMAL(10,2),
  created_at TIMESTAMP
);

-- VSchema: orders is sharded by user_id (vindex)
-- "hash" vindex uses consistent hashing

-- Unsharded table (lookup table)
CREATE TABLE products (
  id BIGINT PRIMARY KEY,
  name VARCHAR(255),
  price DECIMAL(10,2)
);

-- VSchema: products is unsharded (in a single shard)

-- Cross-shard query (Vitess handles scatter-gather)
SELECT * FROM orders WHERE total > 100;
-- Vitess routes to all shards and merges results

Citus

Citus is a PostgreSQL extension that distributes data across a cluster of worker nodes.

-- Install Citus extension
CREATE EXTENSION citus;

-- Create distributed table (sharded by user_id)
SELECT create_distributed_table('orders', 'user_id');

-- Citus automatically shards the table
-- Default: 32 shards, hash-based distribution

-- Query with shard key (routes to single shard)
SELECT * FROM orders WHERE user_id = 123;

-- Cross-shard query (scatter-gather)
SELECT count(*), status FROM orders GROUP BY status;

-- Reference table (replicated to all nodes)
SELECT create_reference_table('products');

-- Join distributed table with reference table
SELECT o.id, p.name, o.total
FROM orders o
JOIN products p ON o.product_id = p.id
WHERE o.user_id = 123;
-- Reference table is local on each node, join is efficient

-- Colocated tables (same shard key, same shards)
SELECT create_distributed_table('order_items', 'user_id');
-- orders and order_items are colocated by user_id
-- Joins on user_id are single-shard and efficient

SELECT o.id, oi.product_id, oi.quantity
FROM orders o
JOIN order_items oi ON o.id = oi.order_id AND o.user_id = oi.user_id
WHERE o.user_id = 123;

Beyond Vitess and Citus

Vitess and Citus bolt sharding onto databases that lack it. If you’re choosing a database today, engines with native sharding skip the router entirely:

EngineModelWhen it fits
MongoDBNative sharding built inDocument model, flexible schema
CockroachDBPostgreSQL-compatibleGlobal distribution, strong consistency
TiDBMySQL-compatibleHTAP — transactions and analytics together
YugabyteDBPostgreSQL/CQL-compatibleCloud-native multi-region
Managed services (Aurora, Spanner, Cosmos DB)Auto-shardingYou want zero sharding operations

The trade-off: native engines shard transparently but constrain your data model, consistency guarantees, or budget. Vitess and Citus let you keep your current database and SQL dialect.

A cheaper split worth naming before any of this: functional sharding — separate databases per domain (a users DB, an orders DB). It scales services without a router, but only until one domain outgrows its node.

Worked Example: Multi-Tenant Sharding for a SaaS

Take a SaaS with 2,000 tenants at roughly 500GB each, sharded by tenant_id through a directory. The directory lives in a small metadata database; the router caches it so lookups don’t add a round-trip per query.

Request flow in a directory-sharded SaaS: the app asks the shard router, the router reads the shard_directory metadata and forwards the query to the tenant's regional shard.
CREATE TABLE shard_directory (
    tenant_id INT PRIMARY KEY,
    shard_id INT NOT NULL,
    region VARCHAR(20)
);

-- Route EU tenants to EU shards, US tenants to US shards
SELECT shard_id FROM shard_directory WHERE tenant_id = 1002;
class ShardRouter {
  constructor() {
    this.shards = new Map();
  }

  async loadDirectory() {
    const rows = await this.metaDb.query(
      "SELECT tenant_id, shard_id FROM shard_directory");
    for (const r of rows) {
      this.shards.set(r.tenant_id, this.pools[r.shard_id]);
    }
  }

  getShard(tenantId) {
    const pool = this.shards.get(tenantId);
    if (!pool) throw new Error("Tenant not found");
    return pool;
  }

  async query(tenantId, sql, params) {
    return this.getShard(tenantId).query(sql, params);
  }
}

// Middleware extracts tenant_id from the JWT
app.use((req, res, next) => {
  req.db = shardRouter.getShard(req.user.tenantId);
  next();
});

Rebalancing a Hot Shard

Shard 1 hits 80% disk. The plan: create Shard 4, migrate selected tenants, update the directory, reload routing, then delete the copied data from the old shard.

async function migrateTenant(tenantId, from, to, newShardId) {
  const tables = ["users", "orders", "invoices"];

  for (const t of tables) {
    const rows = await from.query(
      `SELECT * FROM ${t} WHERE tenant_id = $1`, [tenantId]);
    for (const row of rows) {
      const cols = Object.keys(row);
      const ph = cols.map((_, i) => `$${i + 1}`).join(",");
      await to.query(
        `INSERT INTO ${t} (${cols.join(",")}) VALUES (${ph})`,
        Object.values(row));
    }
  }

  await metaDb.query(
    "UPDATE shard_directory SET shard_id = $1 WHERE tenant_id = $2",
    [newShardId, tenantId]);

  for (const t of tables) {
    await from.query(`DELETE FROM ${t} WHERE tenant_id = $1`, [tenantId]);
  }
}

Monitoring signals worth alerting on:

MetricAlert threshold
Disk per shard> 75%
Queries/sec deviation> 30% from average
Tenants per shard> 500
Migration lag> 5 min

Lessons from this setup:

  • Directory-based sharding gives full control over placement.
  • Rebalancing is the biggest operational cost — automate it before you need it.
  • Keep transactions inside one shard (one tenant = one shard).
  • Cache the directory in the router; a lookup per query adds latency.
  • Very large tenants may outgrow the shared model and need a dedicated shard.

Best Practices

  • Start with directory-based routing — it turns rebalancing into a config change instead of a re-hash of every key.
  • Fewer, larger shards beat many small ones — every shard is a server to monitor, back up, and patch.
  • Design for the rebalancing event — it will happen. Keep the dual-write path and rollback runbook ready before you need them.
  • Keep transactions inside one shard — where you can’t, use sagas or the outbox pattern instead of two-phase commit.
  • Replicate small lookup tables (countries, currencies, product catalogs) to every shard as reference tables.
  • Test distribution with production-like data — a 1,000-row fixture hides the hot spot that 100 million rows creates.

Common Mistakes

  • Sharding too early. Vertical scaling, read replicas, and caching solve most growth problems with a fraction of the operational cost.
  • A mutable or monotonic shard key. Auto-increment IDs and timestamps pile every new write onto one shard, and a key that changes forces a row move.
  • Ignoring cross-shard query patterns. Queries that ran fine on one node become scatter-gather fan-outs — audit the hot queries before picking a key.
  • No rebalancing plan. Uneven shards recreate the exact hot spot you built sharding to avoid.
  • Assuming ACID survives. Multi-shard transactions need sagas, two-phase commit, or a schema redesign — they don’t come free.

See Also

Frequently Asked Questions

What is the difference between sharding and partitioning?

Partitioning divides a table within a single database into smaller pieces. Partitioning can be horizontal (by rows) or vertical (by columns). Sharding spreads the data over separate database servers. Sharding is horizontal partitioning across machines. Partitioning stays on one server — it improves query performance and manageability. Sharding adds network hops and distributed-query complexity. Use partitioning first; shard only when one server can't handle the data.

How do I choose a shard key?

Choose a shard key with high cardinality (many distinct values), even distribution (no hot spots), and that appears in most queries (avoids scatter-gather). Avoid monotonic keys like auto-increment IDs or timestamps — they concentrate new data on one shard. Avoid low-cardinality fields like status or country. Good choices: UUIDs, composite keys like (tenant_id, user_id), or hashes of natural keys. Test with real data distribution before committing — changing the shard key later requires resharding.

What is consistent hashing and why does it matter?

Consistent hashing maps both data keys and server nodes to the same hash ring. When a node is added or removed, only the keys on that node's portion of the ring move. This minimizes data redistribution — typically K/N keys move where K is total keys and N is node count. Without consistent hashing, adding a node with modulo hashing requires redistributing all keys. Consistent hashing is used by Cassandra, DynamoDB, Redis Cluster, and most distributed caches.

Can I do ACID transactions across shards?

Most sharded databases don't support cross-shard ACID transactions. Each shard is independent — a transaction on one shard can't lock rows on another. Workarounds: use two-phase commit (slow and complex), saga pattern (compensating transactions), or design your schema so transactions stay within a single shard. If your app needs cross-shard transactions, question whether sharding is even the right call — a single beefy server may serve you better.

When should I use Vitess or Citus?

Reach for Vitess when your app runs on MySQL and needs horizontal scaling. Vitess gives you connection pooling, query routing, and online schema migrations; YouTube, Slack, and GitHub run it. Reach for Citus when your app runs on PostgreSQL and needs horizontal scaling. Citus extends PostgreSQL with distributed tables, reference tables, and colocated joins — it's an extension, so you keep full SQL compatibility. Both tools handle sharding transparently — your application sees a single database.

How do I handle joins across shards?

Cross-shard joins are expensive — the router must fetch data from every matching shard and join in memory. Avoid them by: (1) colocating tables with the same shard key on the same shards, (2) using reference tables (replicated to all shards), (3) denormalizing data to avoid joins, or (4) doing joins in application code after fetching from shards. Citus handles colocated joins efficiently — if two tables share the same shard key, joins on that key are single-shard. Vitess supports VSchema for similar optimization.

How many shards should I start with?

Start small: 4–8 shards covers most first deployments. Every shard is another node to monitor, back up, and rebalance, so oversharding on day one just buys operational cost you don't need yet. Vitess and Citus both split shards later — a small cluster with a rebalancing plan scales further than a big cluster without one.

Should I shard or switch to a distributed database?

If your application already runs on MySQL or PostgreSQL, Citus or Vitess shards it without a rewrite. If you're starting fresh — or need multi-region writes and global consistency anyway — a natively-sharded engine (MongoDB, CockroachDB, YugabyteDB) can be cheaper than retrofitting sharding onto a single-node database. The real question is operational appetite: a router you control versus a distributed engine you operate.