Replication and high availability are crucial components of MongoDB that ensure data redundancy, failover, and consistent performance. This chapter covers these topics from basic concepts to advanced techniques, with comprehensive examples and explanations to provide a thorough understanding.
Replication in MongoDB involves synchronizing data across multiple servers to ensure data redundancy and high availability. This process provides fault tolerance, enabling your database to remain operational even if some servers fail.
Replication is the process of copying data from one server (the primary) to one or more servers (the secondaries). In MongoDB, this is achieved through replica sets.
A replica set in MongoDB is a group of mongod instances that maintain the same data set. Replica sets provide redundancy and high availability.
Components of a Replica Set
Install MongoDB on all the nodes that will be part of the replica set.
Initialize the Replica Set: Start MongoDB instances with the --replSet
option.
mongod --replSet "rs0" --dbpath /data/db --port 27017
mongo --port 27017
rs.initiate()
rs.add("localhost:27018")
rs.add("localhost:27019")
rs.status()
rs.initiate(
{
_id: "rs0",
members: [
{ _id: 0, host: "localhost:27017" },
{ _id: 1, host: "localhost:27018" },
{ _id: 2, host: "localhost:27019" }
]
}
)
_id
: Identifier for the replica set.members
: Array of objects defining the members of the replica set._id
(unique within the set) and a host
(address of the node).High availability in MongoDB ensures that the database remains operational and accessible even during hardware failures, network issues, or other disruptions.
db.collection.insert(
{ name: "Alice" },
{ writeConcern: { w: "majority", wtimeout: 5000 } }
)
w: "majority"
: Ensures the write is acknowledged by a majority of the replica set members.wtimeout: 5000
: Sets a timeout of 5000 milliseconds for the write concern.Read preference determines how MongoDB directs read operations to replica set members.
db.getMongo().setReadPref("secondaryPreferred")
Delayed replica sets are secondary members that replicate data with a delay.
secondaryDelaySecs
parameter.
rs.add({
host: "localhost:27020",
priority: 0,
hidden: true,
secondaryDelaySecs: 3600
})
priority: 0
: Prevents the delayed node from becoming primary.hidden: true
: Hides the node from applications.secondaryDelaySecs: 3600
: Sets a delay of 3600 seconds (1 hour).Hidden replica sets do not participate in elections and are not visible to client applications.
Example: Configuring a Hidden Replica Set
rs.add({
host: "localhost:27021",
priority: 0,
hidden: true
})
priority: 0
: Prevents the node from becoming primary.hidden: true
: Hides the node from applications.Replication and high availability are critical components of a robust and resilient MongoDB deployment. By understanding and implementing replica sets, configuring write concerns, and employing strategies like delayed and hidden replica sets, you can ensure that your MongoDB database remains highly available and reliable. Happy coding !❤️