Video in progress
What is sharding, and how does it differ from replication?
Sharding splits the data into pieces and puts each piece on a separate server — each server stores only part of the data. In replication, every server holds a full copy of all the data.
📖 Answer
Simply put: replication is 3 copies of one book, sharding is splitting a thick book into 3 volumes.
Replication improves reads and reliability, but not writes: every write still goes to all the copies. If the data doesn't fit on one server's disk, or the write load is more than one server can take, you need sharding.
Example: users are split across 4 shards by user_id. 0–1M go to the first shard, 1M–2M to the second, and so on.
The cost:
- you can't
JOINdata that sits on two shards; - transactions across shards are complicated;
- when you add a new shard, you have to move data.
In practice both are used together: each shard has its own replicas. But don't rush into sharding — first try indexes, caching, read replicas and vertical scaling.
Sign up to mark studied questions and save your progress.