Compare

Sharding vs Replication

ShardingEach machine holds a part

Delivery in10 minutes

Home · Indiranagar, BLR-07

8 PM sale

Starts at 8 PM

Half price on one favourite

All products

  • Butter₹56
  • Bread₹52
  • Eggs₹100
  • Maggi₹14
  • Mangoes₹98
  • Milk₹32
  • Rice₹89
  • Sugar₹48
  • Tomatoes₹30
App serverroutes by name
  • Shard 1 · A–HButter, Bread, Eggs
  • Shard 2 · I–PMaggi, Mangoes, Milk
  • Shard 3 · Q–ZRice, Sugar, Tomatoes
ReplicationEach machine holds a copy

Delivery in10 minutes

Home · Indiranagar, BLR-07

8 PM sale

Starts at 8 PM

Half price on one favourite

All products

  • Butter₹56
  • Bread₹52
  • Eggs₹100
  • Maggi₹14
  • Mangoes₹98
  • Milk₹32
  • Rice₹89
  • Sugar₹48
  • Tomatoes₹30
App serverZepto
  • Primaryevery product
  • Replica 1every product
  • Replica 2every product

Zepto’s products table at its 8 PM sale, split across three machines or copied to three: a flash deal, a rush of orders, then a crash. Illustrative numbers.

Sharding or Replication?

Replicate to share reads and to survive a crash. Shard when the writes or the data outgrow one machine. Big systems do both: they shard the data, and replicate every shard.

DifferenceShardingReplication
Each machine holdsA part of the dataAll of it
ReadsSplit by key: a hot item overloads one shardAny copy answers
WritesSplit across the shardsAll go to the primary
A machine diesIts part is gone until it’s backA replica takes over
Use it forData or writes too big for one machineHeavy reads, staying up

Sharding vs Replication, explained step by step

1. The deal goes live

Tonight is Zepto’s 8 PM sale, and one table holds every product, its price, and how many are left. One machine can’t carry the night alone.

On the left, sharding: the table is split by name across three machines, each holding a third. On the right, replication: the whole table is copied to three machines, one primary and two replicas. Let’s follow the sale through three moments.

First, 8 PM: mangoes at half price, and thousands of shoppers open the same page.

Mangoes live only on the second shard, so every one of those reads lands there.

It runs hot, while the other two sit idle.

With replication, every machine holds mangoes, so any of them can answer, and the same crowd is shared three ways.

A replica may be a moment behind the primary, which is fine for a product page.

2. Orders pour in

Next, the orders pour in, and each one lowers a stock count: thirty thousand writes a second.

Each write goes only to the shard that owns that product, so the load splits three ways, about ten thousand each.

Say one machine handles fifteen thousand: every shard keeps up.

With replication, every write must go to the primary first, and is then copied to both replicas.

Copies add readers, never writers: the primary takes all thirty thousand, and falls behind.

3. A machine dies

Finally, at the peak, the second shard’s machine crashes.

Everything from I to P goes with it: Maggi, mangoes and milk can’t be shown or sold until it’s back.

When the primary crashes here, a replica is promoted to primary within seconds.

Every product is still on every machine, so shoppers barely notice.

In short

So which should you pick? They solve different problems. Replicate to share reads and to survive a crash. Shard when the writes or the data outgrow one machine. Big systems do both: they shard the data, and replicate every shard.