AlgoViz

Sharding & replication

medium 30 min

Scale a database past one machine by splitting rows over shards, and keep it available by copying every shard.

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

Pieces for room, copies for safety.

A choir in several buses. A fixed seating rule says where each group sits, a new bus takes whole groups, and every bus has a spare driver with the list.

Sharding & replication in one picture

Cheat sheet

Adds writes and space
sharding
Adds safety and reads
replication
mod N to N+1
N/(N+1) moves
Logical shards
1/(N+1) moves
Query without key
every shard
1% slow, 100 shards
63% of queries slow
Lag fix
position, sticky, or primary

Spot it when you see

  • Writes or data beyond one machine.
  • Every request names one tenant or user.
  • A hot or full primary after indexes and cache.

Tricks that stick

  • Key, logical shard, machine: three words, two steps.
  • The rule is fixed, the map moves.
  • Piece, then copy each piece.

Do not forget

  • Modulo by the number of machines.
  • A key that the main query does not name.
  • Followers without a plan for lag.
  • Routers that write with an old map.
  • Shards without replicas.