Sharding Approaches in SQL and NoSQL: Range, Hash, and Directory-Based Strategies Compared
When One PostgreSQL Instance Is No Longer Enough
Sharding splits your database across multiple physical nodes so no single machine carries all the data or absorbs all the writes.
π― System Scenario & Requirements
System Goal
Sharding splits your database across multiple physical nodes so no single machine carries all the data or absorbs all the writes.
Actors & Components
- Application Workers
- Volatile Memory Tier
- Persistent Disk Tier
- Worker Process
- In-Memory Cache
- Primary Database
Functional Requirements
- TLDR: Sharding splits your database across multiple physical nodes so no single machine carries all the data or absorbs all the writes.
- The strategy you choose β range, hash, consistent hashing, or directory β determines whether range queries stay cheap, writes stay balanced, and adding a new node moves 100% or just 1/N of your data.
- The shard key is the single most consequential architectural decision you will make, and the wrong one is nearly impossible to fix without a full migration.
Constraints & Assumptions
- Once data lives on multiple shards, operations that used to be simple become multi-step distributed protocols β and some of them are permanently more expensive than their single-node equivalents.
- A query without a shard key predicate must broadcast to all shards, wait for all responses, and merge the result set at the coordinator.
- Pick your shard key for read locality, not write convenience.
- Auto-increment IDs are easy to generate but create a hot-last-shard problem with range sharding and lose ordering with hash sharding.
β‘ Execution Stages & Decision Trace
Stage 1 β The Vocabulary of Sharding: Keys, Maps, and Routing Fundamentals
Before evaluating any sharding strategy, you need a shared vocabulary.
Misunderstanding these six terms β especially the difference between a shard key and a partition key, or between resharding and replication β is how teams make irreversible architectural decisions.
Deep Dive
Scenario context
Sharding splits your database across multiple physical nodes so no single machine carries all the data or absorbs all the writes. The strategy you choose β range, hash, consistent hashing, or directory β determines whether range queries stay cheap, writes stay balanced, and adding a new node moves 100% or just 1/N of your data. The shard key is the single most consequential architectural decision you will make, andβ¦
This walkthrough focuses on When One PostgreSQL Instance Is No Longer Enough.
Before making a design choice, clarify:
- A single PostgreSQL instance handles 50k writes per second.
- You've already maxed out CPU, RAM, and the fastest NVMe disks money can buy β you cannot scale up further.
- But three teams before yours tried to shard this same table and failed.
Concepts in play: database-sharding, caching, consistency-replication, database-indexing
Stage 1 β The Vocabulary of Sharding: Keys, Maps, and Routing Fundamentals
- Before evaluating any sharding strategy, you need a shared vocabulary.
- Misunderstanding these six terms β especially the difference between a shard key and a partition key, or between resharding and replication β is how teams make irreversible architectural decisions.
- The routing path for every database operation flows through three conceptual layers: the client sends the query to a router (vtgate in Vitess, mongos in MongoDB, the Citus coordinator in PostgreSQL), the router consultsβ¦
- In hash and consistent-hash strategies the router computes the shard address algorithmically β no map lookup needed.
Observable outcome: In hash and consistent-hash strategies the router computes the shard address algorithmically β no map lookup needed.
Stage 2 β The Four Sharding Strategies and What Each One Optimizes
- Range Sharding: Natural Ordering at the Cost of Write Balance Range sharding assigns contiguous key intervals to each shard.
- If your shard key is user id and you have four shards, you might configure: - Shard 1 β user id 0 β 999,999 - Shard 2 β user id 1,000,000 β 1,999,999 - Shard 3 β user id 2,000,000 β 2,999,999 - Shard 4 β user id 3,000,0β¦
- The router holds a simple lookup table of ranges.
- Any query with a known user id lands on exactly one shard.
graph LR
R[Query Router] -->|user_id 0-999999| S1[Shard 1]
R -->|user_id 1M-1.99M| S2[Shard 2]
R -->|user_id 2M-2.99M| S3[Shard 3]
R -->|user_id 3M and above| S4[Shard 4]
Observable outcome: Any query with a known user id lands on exactly one shard.
Stage 3 β Deep Dive: How SQL and NoSQL Databases Wire Sharding Into Their Engines
- Internals: Query Routing, Shard Maps, and Database-Specific Partitioning Models SQL databases were designed around the assumption that all data lives in one engine on one server.
- Sharding is an external capability in most SQL systems, not a native one.
- PostgreSQL's native PARTITION BY feature creates partitions on the same server β useful for query pruning but not horizontal scaling.
- True sharding requires the Citus extension (now part of Azure Cosmos DB for PostgreSQL).
-- Distribute the orders table across worker nodes using hash sharding on user_id
SELECT create_distributed_table('orders', 'user_id');
-- Replicate a small countries table to every worker to enable cheap local joins
SELECT create_reference_table('countries');
Observable outcome: True sharding requires the Citus extension (now part of Azure Cosmos DB for PostgreSQL).
Stage 4 β Shard Key Selection: How Your Choice Shapes Write Balance, Query Locality, and Resharding Cost
- No section in this post matters more than this one.A wrong shard key cannot be fixed without a full data migration.
- Here are the five properties every shard key must satisfy: High cardinality.
- The shard key must have many distinct values.
- Using gender (M/F) gives you two possible shards β you've partitioned a problem in half, not horizontally scaled.
Observable outcome: Using gender (M/F) gives you two possible shards β you've partitioned a problem in half, not horizontally scaled.
Stage 5 β How Instagram, YouTube, and Discord Hit the Sharding Wall
- These three companies represent three different failure modes that drove them to fundamentally redesign their data distribution strategies.
- Instagram and the celebrity hot partition.
- Instagram's original data model sharded the media table by user id .
- This worked well for the median user β posts and reads spread evenly.
Observable outcome: This worked well for the median user β posts and reads spread evenly.
Stage 6 β Selecting the Right Sharding Strategy for Your Access Pattern
- The flowchart below walks through the key decision axes.
- Start at the top with your primary constraint β range query requirement β and follow the branches.
- Read the flowchart from top to bottom.
- The first branch is the most critical: if your workload requires range queries, you start with range sharding or consistent hashing with a secondary index.
graph TD
Start([Start: Define primary access pattern]) --> Q1{Range queries needed?}
Q1 -->|Yes, accept hot spots| RangeS[Range Sharding]
Q1 -->|Yes, avoid hot spots| CHR[Consistent Hashing plus secondary index]
Q1 -->|No| Q2{Uniform write distribution?}
Q2 -->|Yes| Q3{Cloud-managed preference?}
Q2 -->|No - power-law or celebrity keys| CHR
Q3 -->|Yes| Managed[DynamoDB or Azure Cosmos DB]
Q3 -->|No| Q4{Cross-shard transactions required?}
Q4 -->|Yes| SQLLayer[PostgreSQL plus Citus or MySQL plus Vitess]
Q4 -->|No| NoSQL[Cassandra or MongoDB hashed]
RangeS --> Note1[Pre-split ranges and monitor for hot tails]
CHR --> Note2[Add vnodes for even load distribution]
Observable outcome: The first branch is the most critical: if your workload requires range queries, you start with range sharding or consistent hashing with a secondary index.
End-to-end outcome
A single PostgreSQL instance handles 50k writes per second.
Alternatives considered
The source focuses on one primary design. A second architecture is not compared, so no alternative is invented here.
Failure and degraded modes
Cross-Shard Operations, Hot Spots, and the Trade-offs That Horizontal Scale Cannot Eliminate
- Detection: Once data lives on multiple shards, operations that used to be simple become multi-step distributed protocols β and some of them are permanently more expensive than their single-node equivalents.
- User or operator impact: Once data lives on multiple shards, operations that used to be simple become multi-step distributed protocols β and some of them are permanently more expensive than their single-node equivalents.
Lessons Learned From Production Sharding Failures
- Detection: Pick your shard key for read locality, not write convenience.
- User or operator impact: Pick your shard key for read locality, not write convenience.
