Menu

πŸ• Database Sharding Explained Like You're 5
πŸ“°

πŸ• Database Sharding Explained Like You're 5

DEV CommunityΒ·Sreekar ReddyΒ·6 months ago
#NQp14oze
#eli5#database#scaling#shard#catalog
Reading 0:00
15s threshold

Splitting data across servers

Day 121 of 149

πŸ‘‰ Full deep-dive with code examples

The Library Card Catalog Analogy

One huge card catalog is hard to use:

Long lines at one catalog

If it breaks, no one can find books

Split by letters:

A-F catalog in room 1

G-M catalog in room 2

N-Z catalog in room 3

Search is faster, and if one catalog goes down, you can still use the others (but you lose access to that section until it comes back).

Sharding splits your database into pieces across servers!

Why Shard?

One server has limits:

Storage β†’ Runs out of disk space

Speed β†’ Too many queries to handle

Memory β†’ Can't fit all data in RAM

Sharding spreads the load across multiple servers.

How It Works

Split data by some key:

Users A-H β†’ Server 1 Users I-P β†’ Server 2 Users Q-Z β†’ Server 3

Enter fullscreen mode

Exit fullscreen mode

Each piece is a "shard." Each shard handles its portion.

Sharding Strategies

By key range:

Users 1-1000 β†’ Shard 1

Users 1001-2000 β†’ Shard 2

By hash:

hash(user_id) % 3 →…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.