A MongoDB replica set is a group of mongod processes that maintain copies of the same data set. Its purpose is redundancy and high availability: if a primary fails, a secondary can be elected to take over. section 9.1 section 1.2
The members work together in three important ways:
- Writes go through the primary and are copied to secondary members. The oplog records ordered write operations that enable replication. section 9.0 section 9.6
- Members vote to elect a primary. MongoDB uses PV1, based on the Raft algorithm, for replica set elections. section 9.6
- Secondary members can serve read queries, reducing load on the primary. Writes still pass through the primary, so replication alone does not remove a write bottleneck. section 9.0
Replication and sharding solve different problems. A replica set keeps copies of one data set available; sharding divides data into partitions to scale storage and read/write capacity. In a sharded cluster, each shard is itself a replica set. Config servers also operate as a replica set to protect the metadata used to route requests. section 9.0 section 9.3.1
A shard loses its primary
Suppose one shard’s primary server fails. Because the shard has secondary members, one can become primary and keep database operations available. Without another copy of that shard’s data, its portion of the data could remain inaccessible until the failed server is restored or replaced. section 1.2 section 9.0
Figure 9.2 This MongoDB sharded cluster configuration includes multiple app servers connected to routers (mongos), which direct queries to the appropriate shard. Three shards, each of which is a replica set, hold segments of the data. Also, an embedded config shard contains the config servers (along with user data), which hold the cluster’s metadata. Starting with MongoDB 8.0, config servers can be embedded in sharded clusters. (Image © MongoDB 2024 CC BY-NC-SA 3.0)
The cluster diagram distinguishes data partitioning from replication: routers direct requests to shards, while each shard is a replica set. It also shows the replica set holding sharding metadata. section 9.3.1
MongoDB Atlas provides replication by default, so an Atlas deployment does not require manual replica-set setup. Sharding is a separate option for horizontal scaling. section 9.0

