System Design Academy

Saving…

Lesson 9 of 10 in Module 30 of 54 lessons complete

Sharding Strategies & Hot Spots

Module 3, lesson 9Concept13 min

Definition

Sharding

In plain English
Sharding means splitting data across several database servers so each holds only part of it.
Think of it like
It is like splitting a city's phone book into volumes A–F, G–M and N–Z kept in different libraries.

Shard key

In plain English
A shard key is the value used to decide which server a row lives on.
Think of it like
It is like the first letter of a surname deciding which volume to open.

Why it exists

Your leader’s storage and write capacity are full. Read followers do not remove the leader’s responsibility for every write.

You need several servers to own different portions of the data. Each server then handles writes for its own portion.

How it works

Hash sharding

In plain English
Hash sharding runs the shard key through a formula that scatters keys evenly across servers.
Think of it like
Like dealing cards around a table so everyone gets a similar pile.

Range sharding

In plain English
Range sharding gives each server a continuous range of keys, such as A to F.
Think of it like
Like the A–F, G–M, N–Z phone book volumes.

Directory sharding

In plain English
Directory sharding keeps a lookup table saying which server owns each key or group.
Think of it like
Like a receptionist with a list of which room each team is in.

Hot spot

In plain English
A hot spot is one shard receiving far more traffic than the others.
Think of it like
Like one checkout lane with a queue out the door while others sit empty.

Resharding

In plain English
Resharding is moving data between shards when you add servers or when shards become uneven.
Think of it like
Like rearranging library volumes when a new branch opens.

The app gives a shard key to a router, which chooses one of three database servers holding different data.

A router sends each key to its owning shard.

The app gives a shard key to a router, which chooses one of three database servers holding different data.

StrategySpreadRange queriesMoving dataExample
HashOften even across many keysUsually touches many shardsChanging shard count can move many keysPlayer ID
RangeDepends on key popularityNearby keys stay togetherSplit overloaded rangesTrade symbol
DirectoryChosen per groupDepends on assigned groupsMove a group and update its mappingCity

Examples

Route a player profile

Every profile query includes player_id. You spread profiles across eight shards.

% means the remainder after division; shard numbers run from 0 through 7.
Text
shard = hash(player_id) % 8

Illustrative hash outputs, not a specific hash algorithm:
hash("player-42") = 1,234 → shard 2
hash("player-99") = 5,679 → shard 7

Many keys can spread evenly while one popular key still overloads its shard. Measure requests per shard as well as stored bytes.

Consistent hashing

In plain English
Consistent hashing assigns keys to servers so adding or removing a server usually moves only some keys.
Think of it like
It is like adding a library branch by moving nearby shelves rather than rearranging every book.

Module 5 explains consistent hashing. Queries, joins, and transactions spanning shards still need extra coordination.

The insight

Hash sharding helps distribute keys when lookups include the shard key.

Trade-offs

Pros

  • Adds write and storage capacity across shards.
  • Some failures affect only one data portion.

Cons

  • Cross-shard queries and transactions need coordination.
  • Moving data while serving writes is difficult.
  • A popular key can still overload one shard.

Use when

  • Measured storage or writes exceed one tuned machine.
  • Common requests identify a suitable shard key.

Avoid when

  • Indexes or a larger machine still suffice.
  • The workload mostly needs global joins and transactions.

Quick check

Every query includes player_id, and you want an even spread. Which strategy fits?
One celebrity's data lives on one shard, which is overloaded while others are idle. This is called…

Takeaways

  • Sharding spreads data and writes across servers.
  • Choose a shard key for common queries and balanced demand.
  • Use sharding after simpler capacity improvements prove insufficient.

Try the quick check above whenever you feel ready.

Previous: Partitioning