Questions
How do you do replication in MongoDB?
The Scenario
You are a database administrator at a social media company. You are responsible for a MongoDB database that is critical to the business.
You need to set up replication to a secondary database to provide high availability and disaster recovery.
The Challenge
Explain how you would set up replication in MongoDB. What is a replica set, and what are the different types of nodes in a replica set?
A junior engineer might not be aware of replication. They might try to solve this problem by just taking periodic backups of the database, which would not provide high availability.
A senior engineer would know that replication is a critical part of database administration. They would be able to explain what a replica set is and would have a clear plan for how to set up replication for a production database.
Step 1: Understand What a Replica Set Is
A replica set is a group of MongoDB servers that maintain the same data set. A replica set provides redundancy and high availability.
Step 2: The Different Types of Nodes
| Node Type | Description |
|---|---|
| Primary | The primary node is the only node that can accept write operations. |
| Secondary | The secondary nodes replicate the data from the primary node. They can be used for read operations. |
| Arbiter | An arbiter does not store any data, but it participates in elections to choose a new primary. |
Step 3: Set Up a Replica Set
Here’s how we can set up a simple replica set with one primary and two secondaries:
1. Start the MongoDB servers:
Start three MongoDB servers on different machines.
2. Initiate the replica set:
Connect to one of the servers and initiate the replica set.
rs.initiate({
_id: "myReplicaSet",
members: [
{ _id: 0, host: "host1:27017" },
{ _id: 1, host: "host2:27017" },
{ _id: 2, host: "host3:27017" }
]
})3. Check the status of the replica set:
You can check the status of the replica set by running the rs.status() command.
Failover
If the primary node goes down, the remaining nodes will hold an election to choose a new primary. This process is automatic and ensures that the database is always available.
Practice Question
You have a three-node replica set and you want to be able to perform reads from the secondary nodes. What should you do?