MongoDB — Replication
What is Replication?
Replication provides redundancy and high availability.
Replica Set
- Primary node receives writes
- Secondary nodes replicate data
- Automatic failover
Initialize Replica Set
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "localhost:27017" },
{ _id: 1, host: "localhost:27018" },
{ _id: 2, host: "localhost:27019" }
]
})
Check Status
rs.status()
Add Member
rs.add("localhost:27018")
Remove Member
rs.remove("localhost:27018")
Mini Practice
- Initialize replica set
- Check status
- Add a member
- Test failover
Up Next
Continue with Sharding — horizontal scaling.
Related Topics
Frequently Asked Questions about Replication
What is Replication in MongoDB?
Replication is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Replication?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Replication.
Why is Replication important in MongoDB?
Replication is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.