Replication is a critical feature in MongoDB that allows you to create copies of your data across multiple servers, ensuring high availability, fault tolerance, and data redundancy. Replication in MongoDB is achieved through the use of Replica Sets, which are groups of MongoDB servers that maintain the same data set.
MongoDB replication is the process of synchronizing data across multiple MongoDB servers to ensure that the data is fault-tolerant, highly available, and can withstand server failures. The replication mechanism ensures that if one node (primary) goes down, another node (secondary) can take over, maintaining availability and consistency.
In MongoDB, replication is achieved using Replica Sets. A Replica Set is a group of MongoDB servers that share the same data set. Replica sets provide automatic failover and data redundancy, making them crucial for production environments.
Replication in MongoDB works by having the primary node log all write operations to the oplog. This oplog is then replicated to the secondary nodes to keep them synchronized with the primary.
insert
, update
, delete
).secondary
, nearest
).One of the most important features of MongoDB replication is automatic failover and primary node election. This ensures that if the primary node fails, a secondary node can be promoted to primary without manual intervention.
To set up a MongoDB replica set, you will need at least three nodes: one primary and two secondaries (for fault tolerance). Here’s a basic overview of the setup:
Start each MongoDB instance with the --replSet
option, specifying the replica set name.
mongod --port 27017 --dbpath /data/db --replSet rs0
mongod --port 27018 --dbpath /data/db --replSet rs0 mongod --port 27019 --dbpath /data/db --replSet rs0
After starting the MongoDB instances, connect to the primary node and initialize the replica set.
mongo --port 27017
rs.initiate()
This command will initialize the replica set and configure the first node as the primary.After initializing the replica set, add the secondary nodes to the replica set:
rs.add("localhost:27018") rs.add("localhost:27019")
rs.status()
This will show the status of all nodes, including which node is currently the primary.primary
: Read from the primary node (default).secondary
: Read from a secondary node.nearest
: Read from the nearest available node, either primary or secondary.db.collection.find().readPref('secondary')
db.collection.insert({ name: "John" }, { writeConcern: { w: 2 } })
mongostat
or rs.printSlaveReplicationInfo()
) to check for replication lag.Arbiters are nodes that do not store data but participate in elections. They are used to ensure there is always an odd number of voting nodes in a replica set, which prevents tied elections.
To add an arbiter:
javascript
Copy code
rs.addArb("localhost:27020")
MongoDB replication is a powerful feature that enhances the availability, fault tolerance, and scalability of MongoDB deployments. By using replica sets, MongoDB ensures that your data is replicated across