Whenever we think of replication the thought that comes to mind is similar or duplicated to the original. The concept of replication can be used in the case of databases too, specifically for backup. Let us learn about the concept of replication in the case of MongoDB.
Replication in MongoDB
Replication can be said to be, a process of copying or reproducing something similar to the original. In the case of databases, a particular database is made a replica of. The reason we replicate the databases is so that the data can be backed up in case of digital disasters or maybe for faster processing.
Let us suppose we have made projects for our school. This project is really important for us to pass the exam. So to ensure the security of the project we keep it in a safe place. But just keeping the project in a safe place doesn't always guarantee its protection. There's always a chance that the project could be destroyed in one way or the other. So, how do we solve this problem of security? We make copies of the project so that if one project is lost we always have the copy of the project as backup to ensure the security of the project.
The same is done in the case of the data. The data is replicated and if one of the data is destroyed or corrupted we always have a copy of the data as a backup.
Working on replication in MongoDB
In MongoDB, there is a specific way in which replication works. before understanding the workings of replication in MongoDB it is necessary to understand what a node is.
The term "nodes" refers to individual computers or servers that are part of a larger network or system, both in the context of MongoDB replication and more generally in computer networking and distributed systems. While each node functions independently, they communicate with other nodes to accomplish shared objectives, including processing data or managing a database.
There are several nodes in MongoDB with specific roles. Let us understand how replication works.
Primary node
The primary node is the main node that manages all write operations. A replica set can only include one primary node at a time. All modifications to the primary node's data sets are documented in the oplog(The oplog or operations log in MongoDB is like a special diary or logbook that keeps track of all the changes made to the data in the database), which is replicated by the subsidiary nodes.
Secondary Node
MongoDB's data redundancy and replication rely heavily on secondary nodes within a replica set. These nodes replicate the oplog from the primary and apply the operations to their data sets. By using this procedure, you can be guaranteed that every secondary backup is keeping an updated copy of the database. The secondary node can serve the purpose of a read operation, but by default, all read operations go to the primary node to ensure consistency.
Arbitrary Node
In MongoDB, an arbitrary node is a special kind of node. This node does not participate in any kind of backup or storage of data sets. The purpose of an arbitrary node is solely to participate in an election of the primary node. In the election of the primary node, it is also used to break ties if there are even number of nodes. This ensures that even if the primary node is corrupted the new primary node is elected. This node is very crucial for maintaining the availability of the dataset.
Process of replication
Now that we know how each node works individually let us understand the process of how the replication works.
Let us suppose, we have a replica set that contains three nodes one primary and the other secondary. Each of the nodes is running on separate servers on their instance of Mongod, The Mongod process is the main software program that runs on a server and manages all the activities of a MongoDB database. Among the 3 nodes, the primary node is considered as the main node as it is the one the client interacts with, and the other nodes are for backup.
Now let us understand the scenario
- Initially, we have 3 servers each running their Mongod. With the 3 servers, we have to configure a replica set named 'rs0'. Let the names of the servers be Server 1, Server 2, and Server 3.
- Now that the servers are set, one of the servers is elected to be a primary node. let the elected primary node be Server 1. Here the election is done via the Arbritiary node.
- The client application now connects to the primary node(Server 1) and performs a write operation inserting a new document. Let us suppose a teacher is writing information on one of their students.
db.users.insertOne({ studName: "Harry", email: "[email protected]" })
- The following write operation is now recorded in the Oplog of the primary node.
- The secondary nodes (Server 2 and Server 3) continuously poll(checking continuously and consistently) the oplog of the first node for new operation. Once the new oplog is detected in the primary node the following operation is copied in the secondary nodes.
- The process continues, and as such consistency and availability are maintained.
Advantages of replication in MongoDB
From such a hectic process of replication, it comes with its advantages.
- Data redundancy: It offers several copies of the data to safeguard against data loss and hardware malfunction.
- High Availability: In the event that a single node fails, the replica set ensures that the database is still accessible for read and write activities because of its remaining backed-up servers.
- Read Scalability: Sometimes both read and write operations for a primary node may seem too loaded. So the Secondary nodes can be used to scale read operations and distribute the load.
- No Downtime for Maintenance: The database can remain available even when members of the replica set are brought offline for maintenance.
Conclusion
In conclusion, replication in MongoDB is a robust mechanism designed to ensure data durability and high availability. By creating multiple copies of the data across different nodes, MongoDB provides a safety net against data loss due to hardware failure, network issues, or other unforeseen disasters. The primary node takes charge of all write operations, while secondary nodes maintain updated copies of the data, ready to take over in the event of primary node failure. The presence of an arbitrary node aids in the election process, ensuring that a new primary can be selected swiftly to maintain service continuity.