Skip to main content
databases

Sharding strategies

11 min read
Fully authored

Range, hash, and consistent-hash — the trade-offs and hot-key traps.

When one machine can't hold all your data, you split it — sharding. But the choice of how to split is the single biggest ops decision you'll make for the life of the database. Get it right and you scale smoothly for a decade. Get it wrong and you rewrite everything two years later when the shard key becomes a hot spot.

There are four industrial strategies: Hash sharding (spread evenly by key hash), Range sharding (keys split by value range), Geographic sharding (partition by user location), and Directory-based sharding (a lookup table maps keys → shards). Each has a specific workload where it wins and specific workloads where it fails catastrophically.

The vocabulary

Shard = Partition = Slice — different names for the same concept. Sharding key = partition key = shard column — the value used to decide which shard a row goes to. Rebalancing — moving data when shards fill up or new nodes are added. Hot spot — one shard receiving vastly more traffic than others (the disaster case).

The four strategies

Hash
How: Hash(shard_key) % N. Distribute keys evenly across shards.
✓ Pros: Perfect balance, simple.
✗ Cons: Range queries dead. Adding shards = mass rebalance (unless consistent hashing).
Cassandra, DynamoDB, Vitess, Citus, MongoDB (hashed)
Range
How: Sort keys, split at boundaries. Shard 1 = A-F, Shard 2 = G-M, etc.
✓ Pros: Range queries fast (contiguous shards).
✗ Cons: Hot spots on tail-writing patterns (time-series).
HBase, MongoDB (range mode), CockroachDB
Geographic
How: Split by user location. Users in EU → EU shard, US → US shard.
✓ Pros: Data residency compliance (GDPR). Low local latency.
✗ Cons: Uneven if user distribution skewed. Cross-region queries expensive.
SaaS multi-tenant, Netflix, Uber city-based
Directory-based
How: External lookup table maps each key/customer → shard. Query the table before every request.
✓ Pros: Ultimate flexibility. Move a single customer.
✗ Cons: Lookup adds latency. Directory service = new SPOF.
Multi-tenant SaaS (Slack, ScyllaCloud)

Hash sharding — the industry default

Take the shard key, hash it (Murmur3, CRC32, MD5), modulo by number of shards. Perfect distribution. Simple. Works out of the box for most workloads.

Hash sharding — 12 keys, 4 shards
Shard 0
gina
iris
kaia
liam
Shard 1
gina
iris
kaia
liam
Shard 2
gina
iris
kaia
liam
Shard 3
gina
iris
kaia
liam
Roughly even distribution. Each write is a single-shard operation. Point lookups: 1 round-trip. Range queries: scatter-gather across all 4.

Cost: range queries are dead. You can't SELECT * WHERE created_at BETWEEN X AND Y efficiently — the range hits every shard. You must scatter-gather. This is why Cassandra queries almost always constrain the partition key.

Range sharding — the wrong choice for time-series

Sort keys, split at ranges. Range queries become fast (they hit contiguous shards). But if new writes cluster at the end (like auto-incrementing IDs or timestamps), all writes hit the last shard. The rest of the cluster is idle.

Range sharding + time-based IDs = tail hot spot
Shard 1 (Jan)
Writes/sec: 5
Shard 2 (Feb-Aug)
Writes/sec: 30
Shard 3 (Sep-Nov)
Writes/sec: 60
Shard 4 (Dec — TODAY)
Writes/sec: 9,000
⚠ hot spot
Classic anti-pattern: shard on a monotonically increasing key (timestamp, auto-increment). Shard 4 gets 99% of the writes. Shards 1-3 sit idle. Fix: hash the key.

HBase, MongoDB, and DynamoDB (with sort keys) all offer range sharding. Use it only when you know queries will hit contiguous ranges and writes will spread evenly. Time-based IDs almost always violate this.

Consistent hashing — the modern middle ground

Karger et al. (1997). Assign each shard a range on a ring. When a shard is added or removed, only 1/N of the keys move (rather than all of them). Cassandra, DynamoDB, Redis Cluster all use consistent hashing under the hood. See the dedicated concept page for details.

The hot-spot problem

Every sharding strategy has a hot-spot failure mode:

  • Hash: one key gets 90% of writes (a celebrity user). Shard containing that hash is overloaded.
  • Range: append-only writes to the tail. Time-based data with monotonic IDs.
  • Geographic: one region has 60% of your users (bad initial partition).
P99 latency asymmetry
One shard has P99 5x higher than others. Classic sign.
Uneven CPU/disk usage
One machine at 90%, others at 20%. Monitoring should show this.
Adding shards doesn't help
You scaled out but throughput didn't improve. The hot shard is your ceiling.

Choosing a shard key — the checklist

1.
Does it distribute traffic evenly?
Skewed distribution = hot spots. Test with actual query mix.
2.
Is it stable? (doesn't change)
Changing a shard key requires data migration. Use immutable IDs.
3.
Does it match your query pattern?
Queries that don't constrain shard key scatter-gather. Slow + expensive.
4.
High enough cardinality?
Low cardinality (like status='active') = only 2-3 shards used. Waste.
5.
Compatible with your ordering needs?
Range queries need range/consistent-hash. Point lookups happy with pure hash.
6.
Foreign key co-location?
If you join user_id → posts, both must shard the same way.

Applied in real systems

Vitess
Deep dive

Vitess (YouTube-scale MySQL)

Turns MySQL into a sharded database. Used by YouTube, Slack, GitHub. Consistent-hash on user_id typical. Automatic query rewriting for cross-shard.

Read the deep dive →
Citus
Deep dive

Citus (Postgres extension)

Turns Postgres into a distributed database. Hash sharding by default. Range for time-series. Now part of Microsoft as Cosmos DB for Postgres.

Read the deep dive →
DynamoDB
Deep dive

DynamoDB — hash partitions

Partition key is hashed. Optional sort key gives range queries within a partition. Each partition has a ~1000 write / 3000 read capacity ceiling — hot keys are the enemy.

Read the deep dive →
MongoDB
Deep dive

MongoDB sharding — hash or range

Choose shard key at collection creation. Hash (with hashed index) or range. Config servers hold the map. Automatic balancing (mongos router). Complex to change shard key later.

Read the deep dive →
Cassandra
Deep dive

Cassandra partition key

Compound primary key: partition key (hashed to token) + clustering key (range within partition). Every query must constrain the partition key for efficiency.

Read the deep dive →
CockroachDB
Deep dive

CockroachDB — auto range sharding

Table split into 512 MB ranges by primary key. Auto-splits when hot. Auto-rebalances across nodes. Range sharding by default, but interleaved tables + hash sharding available.

Read the deep dive →
Uber Schemaless
Deep dive

Uber Schemaless — sharded MySQL

Uber's wrapper on MySQL, sharded by UUID hash. Trip records, users. Serves millions of QPS across 1000s of MySQL instances. Article on their engineering blog.

Read the deep dive →
Instagram
Deep dive

Instagram — sharded Postgres

User IDs generated via Snowflake-style, shard-aware. Direct routing to one of ~5000 Postgres instances. Famously wrote about the pattern in 2012.

Read the deep dive →

Key takeaways

  • Four strategies: hash (even distribution), range (query-friendly), geographic (compliance), directory (flexible but coordinator-dependent).
  • Hash is the default. Range breaks spectacularly on time-series data. Geographic makes multi-region compliance easy.
  • Consistent hashing is a specialized hash scheme that limits key movement when shards are added or removed (1/N of keys move, not all).
  • Hot spots are the failure mode of every strategy. One celebrity user, one hot key, one popular region.
  • Shard key choice is permanent. Almost every database makes it very painful to change the shard key later. Choose carefully.
  • Every serious system-design interview will ask about your shard key. Have an answer for hash vs range and know why.

References

  • Karger et al. (1997) — Consistent Hashing.
  • DeCandia et al. (2007) — Dynamo (consistent hashing).
  • Kleppmann (2017) — DDIA, Chapter 6.
  • Vitess, Citus, MongoDB, Cassandra docs on partitioning.

Practice what you just read

Every foundation concept has a companion quiz to close the loop.