How does sharding differ from replication?
Sharding and replication solve different problems, even though both relate to distributed data in MongoDB.
Replication
Goal: fault tolerance and read scaling. The idea: the same data is copied onto several nodes.
- there's a Primary and several Secondaries
- every node holds the same set of data
- if Primary fails, a Secondary takes over
- reads can come from Secondary, taking load off Primary
The key idea: several copies for reliability and availability.
Sharding
Goal: scaling by data volume and write load. The idea: data is split into pieces (shards), and each piece lives on its own server.
- each shard holds only part of the collection
- load is spread across different nodes
- the database can grow almost without limit
- writes and reads are distributed across shards
The key idea: splitting large data across several machines.
In one sentence
- Replication, the same data on several servers (reliability + read scaling).
- Sharding, different data on different servers (write and volume scaling).
If you'd like, I can give a simple analogy in my next message, something like "replication is like photocopies, while sharding is like chapters of a book kept in different rooms".
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.