TechCoder.io / System Design

Database Sharding & Partitioning

Master horizontal partitioning (sharding), sharding strategies (range, hash, directory), hotspot problems, resharding, cross-shard queries, and how companies like Instagram and Discord shard at billion-row scale.

By TechCoder TeamLast updated: 2026-10-17
In a Nutshell

Master horizontal partitioning (sharding), sharding strategies (range, hash, directory), hotspot problems, resharding, cross-shard queries, and how companies like Instagram and Discord shard at billion-row scale. This hands-on tutorial focuses on practical implementation of database sharding & partitioning concepts.

Database Sharding & Partitioning

Database Sharding — Horizontal Partitioning with Shard Router

When a single database server can no longer handle your data volume or write throughput — even with vertical scaling — you must split your data across multiple servers. This is sharding (also called horizontal partitioning).

[!IMPORTANT] Sharding is a last resort, not a first step. The operational complexity of sharding is enormous. Exhaust all other options first: indexing, query optimization, read replicas, caching, and vertical scaling. Shard when you genuinely must.

Vertical Partitioning vs Horizontal Partitioning (Sharding)

Vertical PartitioningHorizontal (Sharding)
Split byTable / featureRow / ID range
ComplexityLowHigh
Scale limitStill limited by largest tableLinear with shard count
Cross-join queriesImpossible (different servers)Complex (scatter-gather)

Sharding Strategies

PYTHON PLAYGROUND
⏳ Loading editor…

The Hotspot Problem

Cross-Shard Queries & Scatter-Gather

The biggest pain point of sharding: queries that need data from multiple shards:

PYTHON PLAYGROUND
⏳ Loading editor…

Quiz

Quiz

Question 1 of 2

You shard a user database by `user_id % 4` (4 shards). Later you add a 5th shard and change to `user_id % 5`. What percentage of data needs to be migrated?

~20% (only the data for the new shard)
~80% (most records map to different shards with the new modulus)
~50% (half the data moves)

Key Takeaways

✅ Shard last — try read replicas, caching, and vertical scaling first.
✅ Hash sharding gives even distribution but resharding migrates ~80% of data; use consistent hashing to minimize migration.
✅ Range sharding enables efficient range queries but risks hotspots on sequential keys (like timestamps).
✅ Scatter-gather pattern runs cross-shard queries in parallel — latency = slowest shard, not sum of all shards.
✅ Cross-shard JOINs, COUNT, ORDER BY are extremely expensive — design your shard key around your most critical query.

Official Resources

What's Next?

Sharding handles write scale — but how do you handle read scale and what happens when a database server goes down?
Next: Replication, CAP Theorem & Consistency — Copying data across multiple servers and the fundamental trade-offs involved.