Assignments Of Day 22: MongoDB Replication
Assignment 1: Set Up a MongoDB Replica Set with Three Nodes
Objective: Set up a MongoDB replica set with three nodes on your local machine.
Steps to complete:
1. Install MongoDB on your machine.
2. Start three MongoDB instances on different ports (27017, 27018, and 27019).
o Use the command:
bash
CopyEdit
mongod --port 27017 --dbpath /data/db1 --replSet rs0
mongod --port 27018 --dbpath /data/db2 --replSet rs0
mongod --port 27019 --dbpath /data/db3 --replSet rs0
3. Connect to the primary node (port 27017):
bash
CopyEdit
mongo --port 27017
4. Initiate the replica set:
javascript
CopyEdit
rs.initiate()
5. Add the secondary nodes to the replica set:
javascript
CopyEdit
rs.add("localhost:27018")
rs.add("localhost:27019")
6. Verify the replica set configuration by checking the status:
javascript
CopyEdit
rs.status()
Assignment 2: Perform Write Operations on the Primary Node
Objective: Perform some write operations on the primary node and verify data replication to secondary nodes.
Steps to complete:
1. Insert data into the mydb database on the primary node:
javascript
CopyEdit
use mydb
db.myCollection.insert({ name: "Alice", age: 25 })
2. Check the data on the primary node:
javascript
CopyEdit
db.myCollection.find()
3. Check the data on one of the secondary nodes:
bash
CopyEdit
mongo --port 27018
use mydb
db.myCollection.find()
Expected Outcome: The inserted data should appear on the secondary node, indicating replication.
Assignment 3: Simulate Failover by Stopping the Primary Node
Objective: Stop the primary node and observe the failover process and the promotion of a secondary node.
Steps to complete:
1. Stop the primary node:
o In the terminal where the primary node is running (port 27017), press Ctrl + C or kill the process.
2. Verify the new primary node:
o Connect to one of the secondary nodes and check the replica set status:
bash
CopyEdit
mongo --port 27018
rs.status()
3. Perform some write operations on the new primary node:
javascript
CopyEdit
db.myCollection.insert({ name: "Bob", age: 30 })
Expected Outcome: One of the secondary nodes will be promoted to primary, and you should be able to insert data into the new primary.
Assignment 4: Experiment with Write Concerns
Objective: Experiment with different write concerns and observe their behavior.
Steps to complete:
1. Perform a write operation with w: 1 (acknowledgment from primary):
javascript
CopyEdit
db.myCollection.insert({ name: "Charlie", age: 40 }, { writeConcern: { w: 1 } })
2. Perform a write operation with w: "majority" (acknowledgment from majority of replica set members):
javascript
CopyEdit
db.myCollection.insert({ name: "David", age: 45 }, { writeConcern: { w: "majority" } })
3. Check the acknowledgment for both operations and explain the differences.
Expected Outcome: Write concern w: 1 returns acknowledgment only after the primary node confirms, while w: "majority" waits for acknowledgment from the majority of replica set members.
Assignment 5: Experiment with Read Concerns
Objective: Experiment with different read concerns to understand their impact.
Steps to complete:
1. Perform a read operation with readConcern: "local" (reads the most recent data from the replica set):
javascript
CopyEdit
db.myCollection.find().readConcern("local")
2. Perform a read operation with readConcern: "majority" (reads the most recent data acknowledged by the majority of the replica set):
javascript
CopyEdit
db.myCollection.find().readConcern("majority")
3. Compare the results and explain the difference in the data retrieved.
Expected Outcome: Read concern local may return uncommitted data from the primary node, while majority guarantees data consistency as it waits for acknowledgment from the majority of the replica set.
Assignment 6: Set Up Read Preferences for the Replica Set
Objective: Experiment with different read preferences in MongoDB to optimize read operations.
Steps to complete:
1. Perform a read operation using the default read preference (primary):
javascript
CopyEdit
db.myCollection.find().readPref("primary")
2. Perform a read operation from a secondary node:
javascript
CopyEdit
db.myCollection.find().readPref("secondary")
3. Perform a read operation from the nearest node:
javascript
CopyEdit
db.myCollection.find().readPref("nearest")
4. Compare the results and discuss the performance implications of each read preference.
Expected Outcome: Read preference primary reads from the primary node, secondary reads from a secondary node, and nearest reads from the nearest available node in terms of latency.
Assignment 7: Monitor Replica Set Status and Logs
Objective: Monitor the replica set status and understand logs for replica set operations.
Steps to complete:
1. Check the replica set status using:
javascript
CopyEdit
rs.status()
2. Analyze the MongoDB logs to identify any issues related to replica set operations (e.g., elections, failover).
Expected Outcome: You should be able to monitor the health of the replica set and understand key events like node elections and failovers.
Assignment 8: Backup and Restore a MongoDB Replica Set
Objective: Perform a backup and restore operation on a MongoDB replica set.
Steps to complete:
1. Use mongodump to back up the replica set data:
bash
CopyEdit
mongodump --host localhost --port 27017 --out /backup/mydb
2. Restore the backup using mongorestore:
bash
CopyEdit
mongorestore --host localhost --port 27017 /backup/mydb
Expected Outcome: The backup and restore process should complete successfully without data loss, ensuring data availability.
Assignment 9: Test Data Consistency with Different Write Concerns
Objective: Test data consistency by using different write concerns on write operations.
Steps to complete:
1. Insert data with writeConcern: { w: 1 }:
javascript
CopyEdit
db.myCollection.insert({ name: "Eve", age: 50 }, { writeConcern: { w: 1 } })
2. Insert data with writeConcern: { w: "majority" }:
javascript
CopyEdit
db.myCollection.insert({ name: "Frank", age: 60 }, { writeConcern: { w: "majority" } })
3. Observe the behavior when reading the data from secondary nodes before the operation is fully acknowledged.
Expected Outcome: Write concern w: "majority" guarantees data consistency across replica set members, while w: 1 might lead to data inconsistency if reading from secondaries immediately after a write operation.
Assignment 10: Simulate a Primary Node Failure and Observe Replica Set Behavior
Objective: Simulate a failure of the primary node and observe the automatic promotion of a secondary node.
Steps to complete:
1. Stop the primary node (port 27017):
bash
CopyEdit
kill <PID_of_Node1>
2. Observe the failover process and new primary node:
bash
CopyEdit
mongo --port 27018
rs.status()
3. Perform write operations on the new primary node and check replication to secondary nodes.
Expected Outcome: After the primary node fails, one of the secondary nodes will be automatically promoted to primary. Write operations should be accepted on the new primary node.
Conclusion:
These assignments provide a comprehensive guide to understanding and practicing MongoDB replication, including replica set configuration, failover simulation, write and read concerns, and read preferences. Each exercise helps students grasp essential concepts for managing and optimizing MongoDB replica sets for high availability and fault tolerance.
