Mongodb Replica Set

A replica set in MongoDB is a collection of MongoDB instances that share the same dataset. A replica set consists of one primary node and multiple secondary nodes, with each node holding a complete copy of the data. The primary node serves as the core of the replica set; it handles all write operations and replicates them to all secondary nodes. Secondary nodes are responsible for synchronizing data from the primary node and can be used for read operation load balancing. If the primary node becomes unavailable, one of the secondary nodes is elected as the new primary node to ensure system availability and fault tolerance.

In a replica set, each node has a unique name, such as "node1", "node2", etc., which are synchronized via heartbeat mechanisms among themselves. In case the primary node fails or network issues arise, secondary nodes automatically initiate an election process to select a new primary node, ensuring high availability for the system.

Replica sets also offer additional features like data backup and recovery, failover between data centers, and read-write separation. By utilizing replica sets, MongoDB provides high availability and fault tolerance while enabling rapid failure recovery.

Creating Replica Sets

The process of creating a replica set in MongoDB involves several steps:

  1. Start MongoDB Instances: Launch at least three MongoDB instances; one will serve as the primary node, while others act as secondary nodes. You can start a MongoDB instance using the mongod command by specifying different parameters such as --port, --dbpath, and --replSet to set up the port number, data directory, and replica set name for each instance respectively.

  2. Initialize Replica Set: Initialize the replica set on the primary node using the rs.initiate() command, like this: (where _id represents the replica set name, and members contains information about all nodes, including their ID (_id) as well as hostname and port number (host).)

rs.initiate(
  {
    _id: "myReplicaSet",
    members: [
      { _id: 0, host: "mongo1:27017" },
      { _id: 1, host: "mongo2:27017" },
      { _id: 2, host: "mongo3:27017" }
    ]
  }
)
  1. Add Nodes: Add new nodes to the replica set using the rs.add() command, for example: (where "mongo4:27017" represents the hostname and port number of the newly added node.)
rs.add("mongo4:27017")
  1. Check Replica Set Status: Use the rs.status() command to check the status of the replica set, allowing you to understand the state of each node—whether it is primary, secondary, or unavailable.

  2. Perform Read and Write Operations: For write operations, data can be written directly to the primary node; this operation will automatically synchronize the data to all secondary nodes. For read operations, requests may be sent either to the primary node or a secondary node depending on your configured read-write separation settings.

By following these steps, you can create a replica set in MongoDB and achieve high availability along with fault tolerance capabilities.