What is Replication in MongoDB?
Learn what replication is in MongoDB, how the primary-secondary replica set model and oplog work, and how automatic failover keeps data available.
Expected Interview Answer
Replication in MongoDB is the process of maintaining multiple synchronized copies of the same data across a group of servers called a replica set, so the database stays available and durable even if a server fails.
A replica set has one primary node that accepts all writes and any number of secondary nodes that continuously replicate those writes from the primary's oplog, applying them in the same order. If the primary becomes unreachable, the remaining nodes hold an election and promote a new primary automatically, typically restoring writability within seconds. Secondaries can also serve read traffic when the application explicitly opts into eligible read preferences, and reads can be tuned for consistency versus staleness. Replica sets are the standard deployment topology for any production MongoDB workload, since even a single unsharded database is normally still replicated for durability.
- Automatic failover if the primary node fails
- Data durability through multiple synchronized copies
- Optional read scaling from secondaries with read preference
- Oplog-based replication keeps writes ordered and consistent
- Standard baseline for production availability, sharded or not
AI Mentor Explanation
Replication is like a broadcaster keeping backup commentary booths synced to the main booth's live feed, so if the lead commentator loses signal, a backup booth takes over instantly. Each backup booth mirrors the primary's commentary in the same order, the way MongoDB secondaries replay the primary's oplog to stay in sync for failover.
Replica set write and failover flow
Client Application
- Sends writes to primary
- Can read from secondaries
Primary Node
- Accepts all writes
- Records operations in oplog
Secondary Node A
- Replicates oplog
- Eligible for election
Secondary Node B
- Replicates oplog
- Eligible for election
Step-by-Step Explanation
Step 1
Primary accepts writes
All write operations go to the single primary node, which records them in its operation log, the oplog.
Step 2
Secondaries replicate
Secondary nodes continuously pull and apply oplog entries from the primary, staying nearly in sync.
Step 3
Heartbeats monitor health
Replica set members exchange heartbeats to detect whether the primary is still reachable.
Step 4
Automatic election
If the primary is unreachable, eligible secondaries hold an election and promote a new primary.
Step 5
Optional read scaling
Applications can set a read preference to route some reads to secondaries, trading consistency for throughput.
What Interviewer Expects
- Explains replication as maintaining synchronized copies via a replica set
- Describes the primary-secondary model and the oplog
- Knows automatic election promotes a new primary on failure
- Understands read preference options for reading from secondaries
- Recognizes replication as standard even without sharding
Common Mistakes
- Confusing replication with sharding, which is about scale, not availability
- Assuming reads from secondaries are always perfectly up to date
- Believing failover is instant with zero brief unavailability window
- Running a single-node deployment in production without a replica set
Best Answer (HR Friendly)
“Replication is how MongoDB keeps several backup copies of the same data on different servers, so if one server goes down, another already has the same information ready to take over. It keeps applications running smoothly without data loss during hardware failures.”
Code Example
// Initiate a 3-node replica set
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo1:27017" },
{ _id: 1, host: "mongo2:27017" },
{ _id: 2, host: "mongo3:27017" }
]
});
// Check replica set status and current primary
rs.status();
// => members: [{ name: "mongo1:27017", stateStr: "PRIMARY" }, { name: "mongo2:27017", stateStr: "SECONDARY" }, ...]Follow-up Questions
- What is the oplog and how does replication use it?
- How does a replica set elect a new primary?
- What are the tradeoffs of reading from secondary nodes?
- What is the difference between replication and sharding?
- How many nodes are typically recommended in a production replica set?
MCQ Practice
1. What is the main purpose of replication in MongoDB?
Replication maintains synchronized copies of data across nodes so the database stays available and durable if a node fails.
2. What log do secondaries replay to stay in sync with the primary?
Secondaries continuously apply entries from the primary's oplog to stay synchronized.
3. What happens when a replica set's primary becomes unreachable?
Remaining eligible secondaries automatically hold an election and promote a new primary.
Flash Cards
What is replication in MongoDB? — Maintaining synchronized copies of data across nodes in a replica set for availability and durability.
What log drives replica set synchronization? — The oplog, which secondaries replay from the primary.
What happens if a primary node fails? — Eligible secondaries hold an election and automatically promote a new primary.
Is replication the same as sharding? — No — replication targets availability and durability, while sharding targets horizontal scale.