Replica Sets
Replica Sets: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 61 of 100
Replica Sets
Foundations & CRUD ✓ → Queries & Schema ✓ → Aggregation & Scale → Atlas & Projects
Aggregation & Scale · 3 — Pipelines · ~10 min · MongoDB — Replication & Sharding
What is this?
A replica set is a group of mongod nodes that hold the same data set — one primary for writes, secondaries that replicate the oplog. It provides redundancy and high availability.
Why should you care?
A single VM dying should not erase your SaaS. Replica sets keep copies and enable automatic failover.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
// Initiate a local 1-node set (dev) or multi-node in real setups
rs.initiate({
_id: "rs0",
members: [{ _id: 0, host: "localhost:27017" }]
})
rs.status()
db.hello()
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- rs.initiate configures the replica set.
- rs.status shows members and health.
- db.hello() reports whether you are connected to the primary.
- Production uses 3+ members across failure domains.
Practice next
- On a practice machine, initiate a replica set as docs recommend for your OS.
- Run rs.status() and find the primary.
- Insert on primary and read from a secondary with secondaryOk/readPref.
- Inspect oplog with db.getSiblingDB("local").oplog.rs.find().limit(1).
- Add a second member in a docker lab if you can.
Remember
Replica sets copy data across nodes. Primary takes writes; secondaries replicate. Foundation for failover and read scaling.
Always-on hospital records
Three-node replica set across racks stores patient appointments.
Outcome: One rack loss does not take the booking system offline.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!