DEV Community

Cover image for Sharding - Architecture Series: Part 5
MUHAMMAD USMAN AWAN
MUHAMMAD USMAN AWAN

Posted on

Sharding - Architecture Series: Part 5

πŸ—οΈ Sharding - Architecture Series: Part 5

βš”οΈ WHAT is Sharding?

Sharding = Horizontally splitting one huge database into many smaller databases (shards), each living on separate servers.

Each shard stores a slice of the whole dataset and handles a slice of total traffic.

Visual:

Single DB (Overloaded)       β†’      Sharded DB (Distributed)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  1TB Data           β”‚            β”‚ Shard 1   β”‚ β”‚ Shard 2   β”‚ β”‚ Shard 3   β”‚
β”‚  15K QPS            β”‚            β”‚ Users A-F β”‚ β”‚ Users G-M β”‚ β”‚ Users N-Z β”‚
β”‚  πŸ’₯ Slow / Choking  β”‚            β”‚ 3K QPS    β”‚ β”‚ 4K QPS    β”‚ β”‚ 3K QPS    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

🚨 WHEN Do You Need Sharding? (Red Flags)

πŸ“Œ Dataset too large for a single server (100GB–TB scale)
πŸ“Œ QPS (queries/sec) exceeding hardware limits
πŸ“Œ One table growing billions of rows
πŸ“Œ Vertical scaling becomes too expensive πŸ’Έ
πŸ“Œ Read/write traffic causing slow queries
Enter fullscreen mode Exit fullscreen mode

When β€œadd more RAM/CPU” stops helping β†’
It's sharding time.


πŸ“± Real Example: How Instagram Shards

Instagram has 1B+ users, petabytes of posts, reels, feed data.

They shard based on hashed user ID:

shard_id = user_id % 1000
Enter fullscreen mode Exit fullscreen mode

So:

user_id 123456  β†’  123456 % 1000  β†’  Shard #456
Enter fullscreen mode Exit fullscreen mode

Everything related to that user (posts, followers, comments) lives on Shard 456, forever.

Why they use hashing?

βœ… Perfect load distribution (no hot shards)
βœ… No manual range management
βœ… Each user always hits same shard β†’ FAST
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Sharding Strategies (Choose Your Weapon)

Strategy How It Works Pros Cons
Range ID 1–100K β†’ Shard 1 Easy Hotspots (popular ranges)
Hash (Instagram) ID % N Balanced No range queries
Consistent Hash Hash ring Minimal reshuffling Complex

Quick snippets:

// Hash Sharding
function getShard(userId) {
  return userId % 1000;
}

// Range Sharding
function getShard(userId) {
  return Math.floor(userId / 100000);
}
Enter fullscreen mode Exit fullscreen mode

πŸ›οΈ Architecture (How Apps Route to Shards)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ App Server (API)     β”‚ ───▢│ Shard Router     β”‚ ───▢│ Shard #456 β”‚
β”‚ user_id=123456       β”‚     β”‚ (calculates ID%) β”‚     β”‚ User Data  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

If a query needs data from multiple shards β†’ the routing layer handles fan-out + aggregation.


πŸ’₯ Why Sharding Is So Powerful

🟒 Linear scalability (add more shards β†’ handle more users)
🟒 Faster queries (smaller DB = faster indexes)
🟒 Fault isolation (Shard 456 down β‰  whole app down)
🟒 Geographic distribution (EU users on EU shards)
🟒 Infinite scaling (theoretically)
Enter fullscreen mode Exit fullscreen mode

This is how Instagram, YouTube, TikTok, Uber handle global scale.


☠️ The Dark Side of Sharding (Things people don’t tell you)

πŸ’€ Cross-shard JOINs = slow and painful
πŸ’€ Rebalancing shards = data migration nightmare
πŸ’€ Monitoring 1000 shards = complex ops
πŸ’€ Schema changes = do it 1000Γ—
πŸ’€ Picking the wrong shard key = disaster
Enter fullscreen mode Exit fullscreen mode

Which is why companies denormalize heavily to avoid cross-shard joins.


🎯 Pro Tips from Real Distributed Systems Engineers

1. Start with 64 or 256 shards, not thousands.
2. Hash your primary keys (best distribution).
3. Never shard on fields that change.
4. Build a routing layer between app ↔ DB.
5. Avoid JOINs across shards β€” duplicate data instead.
6. Monitor shard imbalance regularly.
7. Plan for re-sharding from day 1.
Enter fullscreen mode Exit fullscreen mode

πŸš€ Modern Solutions (2025)

These databases handle sharding automatically:

πŸ“¦ Vitess (YouTube scale)
πŸ“¦ PlanetScale (MySQL + global)
πŸ“¦ YugabyteDB (PostgreSQL + distributed)
πŸ“¦ CockroachDB (ACID + auto-shard)
Enter fullscreen mode Exit fullscreen mode

⭐ Final Summary

Sharding = breaking one big database into many small databases so your system can scale horizontally.

It gives:

  • Infinite scalability
  • Faster queries
  • Better performance
  • Global distribution
  • Instagram/Twitter-level architecture

BUT…

It requires planning, a routing layer, and avoiding cross-shard joins.


πŸ” Missed Previous Parts? Catch Up Here!

If you’ve joined this series recently or missed any of the earlier deep-dives, no worries bro β€” I’ve linked all previous architecture topics below. Each part is designed to build your understanding step-by-step, from caching to replication to sharding. Take your time, go through them in order, and you’ll get a rock-solid grasp of real-world system design fundamentals.

πŸ“˜ Architecture Series – Index


Top comments (0)