liveBook · Manning

MongoDB replica set

As explained in MongoDB 8.0 in Action, Third Edition

A replica set is a group of database servers that maintain replicated copies of data with automatic primary election for availability.

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)
A diagram of a computer application

Description automatically generated

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

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines members and their roles, then identifies election voting and the oplog without walking through replica-set configuration commands.
Examples
Illustrates failover when a shard primary fails and diagrams shards and config servers as replica sets.
What you can do after
Evaluate/understand when replication improves availability and read capacity, and when sharding is needed to address write bottlenecks.
How it connects
Links replica sets to primary elections, the oplog, read distribution, sharding, config servers, and change streams.

Also explained in

Compare how 2 books explain MongoDB replica set →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.