A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments.
9.1 Replication Introduction
Replication is the process of synchronizing data across multiple servers.
9.1.1 Purpose of Replication
Replication provides redundancy and increases data availability. With multiple copies of data on different database servers, replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.
9.1.2 Replication in MongoDB
A replica set is a group of mongod instances that host the same data set. One mongod, the primary, receives all write operations. All other instances, secondaries, apply operations from the primary so that they have the same data set.
The primary accepts all write operations from client. The secondaries replicate the primary’s oplog and apply the operations to their data sets such that the secondaries’ data sets reflect the primary’s data set.
9.2 Replication Concepts
- Replica Set Members – Introduces the components of replica sets.
- Replica Set Primary – The primary is the only member of a replica set that accepts write operations.
- Replica Set Secondary Members – Secondary members replicate the primary’s data set and accept read operations. If the set has no primary, a secondary can become primary. The following three-member replica set has two secondary members. The secondaries replicate the primary’s oplog and apply the operations to their data sets.
- Priority 0 Replica Set Members – Priority 0 members are secondaries that cannot become the primary.
- Hidden Replica Set Members – Hidden members are secondaries that are invisible to applications. These members support dedicated workloads, such as reporting or backup.
- Replica Set Arbiter – An arbiter does not maintain a copy of the data set but participate in elections.
- Replica Set Deployment Architectures- Introduces architectural considerations related to replica sets deployment planning.
- Replica Set High Availability – Presents the details of the automatic failover and recovery process with replica sets.
- Replica Set Elections- Elections occur when the primary becomes unavailable and the replica set members autonomously select a new primary.
- Read Preference- Applications specify read preference to control how drivers direct read operations to members of the replica set.
- Master Slave Replication – Master-slave replication provided redundancy in early versions of MongoDB. Replica sets replace master-slave for most use cases.
9.3 Replication Processes
Members of a replica set replicate data continuously. First, a member uses initial sync to capture the data set. Then the member continuously records and applies every operation that modifies the data set. Every member records operations in its oplog which is a capped collection.
- Replica Set Oplog – The oplog records all operations that modify the data in the replica set.
- Replica Set Data Synchronization – Secondaries must replicate all changes accepted by the primary. This process is the basis of replica set operations.
9.4 Replica Set Tutorials
The administration of replica sets includes the initial deployment of the set, adding and removing members to a set, and configuring the operational parameters and properties of the set. Administrators generally need not intervene in failover or replication processes as MongoDB automates these functions. In the exceptional situations that require manual interventions, the tutorials in these sections describe processes such as resyncing a member.
- Deploy a Replica Set – Configure a three-member replica set for production systems.
- Convert a Standalone to a Replica Set – Convert an existing standalone mongod instance into a three-member replica set.
- Adjust Priority for Replica Set Member – Change the precedence given to a replica set members in an election for primary.
- Prevent Secondary from Becoming Primary – Make a secondary member ineligible for election as primary.
- Configure a Hidden Replica Set Member – Configure a secondary member to be invisible to applications in order to support significantly different usage, such as a dedicated backups.
- Change the Size of the Oplog – Increase the size of the oplog which logs operations. In most cases, the default oplog size is sufficient.
- Resync a Member of a Replica Set – Sync the data on a member. Either perform initial sync on a new member or resync the data on an existing member that has fallen too far behind to catch up by way of normal replication.
- Force a Member to Become Primary – Force a replica set member to become primary.
- Change Hostnames in a Replica Set – Update the replica set configuration to reflect changes in members’ hostnames.
Learn more about The Distinctions Between Couchbase And MongoDB in this insightful blog now!