MongoDB Master-Slave Replication Explained
MongoDB’s master-slave replication is a data replication mechanism that allows data to be copied from a primary node to one or more secondary nodes. The principle of master-slave replication is as follows:
- Primary Node: The primary node is the origin of data replication and is the node that serves read and write operations externally. All write operations are executed on the primary node and are recorded in the primary node’s operation log.
- Secondary node: A secondary node is a copy of the primary node, responsible for receiving the primary node’s operation log and applying these operations step by step to its own dataset to keep it consistent with the primary node. By default, secondary nodes are read-only, meaning they can only be used for reading operations, but they can be configured to support read and write operations.
- Oplog (Operation Log): The primary node records the write operations that are executed in a special collection called Oplog. Oplog stores the operation logs performed within a certain period of time, and the secondary nodes continuously read the Oplog to receive data updates from the primary node.
- Initial synchronization: When a new secondary node is added to the primary node cluster, it needs to undergo initial synchronization, which involves replicating the primary node’s data set to its own. This process ensures that the secondary node’s data matches that of the primary node by copying the full data set from the primary node to the secondary node.
- Incremental synchronization: After the initial synchronization is completed, the secondary node will continuously read the Oplog and apply the write operations from the primary node to its data set in order to keep its data consistent with the primary node. Through incremental synchronization, the secondary node continually catches up with the data updates from the primary node.
- Heartbeat mechanism: Nodes periodically send heartbeat signals to the master node to check if it is alive. If the master node fails, the nodes will select a new master node through an election mechanism and continue replicating data.
Using the master-slave replication mechanism, MongoDB can offer high availability and data redundancy. In case of failure of the master node, there can be a smooth transition to the slave node to ensure continuous service availability.