MongoDB Replication Interview Questions
Master MongoDB Replication with interview-focused questions covering Replica Sets, Primary and Secondary nodes, Oplog, Elections, Read Preference, Write Concern, Failover, Disaster Recovery, and production best practices.
Introduction
Replication is one of MongoDB's most important enterprise features. It provides:
- High Availability
- Fault Tolerance
- Automatic Failover
- Disaster Recovery
- Read Scaling
- Data Redundancy
MongoDB implements replication using Replica Sets, where multiple MongoDB servers maintain identical copies of the same data.
Almost every production MongoDB deployment uses Replica Sets.
MongoDB Replica Set Architecture
flowchart LR
Application --> Primary
Primary --> Oplog
Oplog --> Secondary1
Oplog --> Secondary2
Oplog --> Arbiter
1. What is MongoDB Replication?
Answer
Replication is the process of maintaining multiple copies of the same data across different MongoDB servers.
Benefits
- High Availability
- Automatic Failover
- Disaster Recovery
- Read Scaling
2. Why is Replication required?
Without replication
Server Crash
↓
Database Down
With replication
Primary Failure
↓
Secondary Becomes Primary
↓
Application Continues
3. What is a Replica Set?
A Replica Set is a group of MongoDB servers that maintain identical copies of data.
Typically consists of
- Primary
- Secondary
- Secondary
- Arbiter (optional)
Replica Set
flowchart LR
Primary --> Secondary1
Primary --> Secondary2
Primary --> Arbiter
4. What is the Primary Node?
Primary Node
- Accepts Writes
- Accepts Reads (default)
- Records Operations
- Replicates Changes
There is only one Primary.
5. What is a Secondary Node?
Secondary Nodes
- Replicate data
- Apply operations from Oplog
- Can serve read requests
- Participate in elections
6. What is an Arbiter?
Arbiter
- Stores NO data
- Votes during election
- Helps maintain majority
Useful for small deployments.
Replica Members
flowchart TD
ReplicaSet --> Primary
ReplicaSet --> Secondary1
ReplicaSet --> Secondary2
ReplicaSet --> Arbiter
7. What is the Oplog?
Oplog
Operational Log
Stores every write operation performed on the Primary.
Secondary nodes continuously read the Oplog.
Oplog Flow
flowchart LR
Write --> Primary --> Oplog --> Secondary
8. Where is the Oplog stored?
Inside
local.oplog.rs
collection.
9. How do Secondary nodes synchronize?
Steps
- Read Oplog
- Replay Operations
- Update Local Data
10. What is Automatic Failover?
If Primary crashes
↓
Election starts
↓
Secondary becomes Primary
↓
Application reconnects
Automatically.
Failover
flowchart LR
PrimaryCrash --> Election --> NewPrimary --> Application
11. What triggers an Election?
Election starts when
- Primary fails
- Network partition
- Step down command
- Startup recovery
12. How is a new Primary selected?
Replica Set members vote.
Node receiving majority votes
↓
Becomes Primary.
13. What is Majority Voting?
More than half of voting members must agree.
Example
5 Nodes
↓
Need
3 Votes
14. Can there be two Primaries?
No.
MongoDB prevents split-brain using majority elections.
15. What is Split Brain?
Two Primaries accepting writes simultaneously.
Replica Sets prevent this using elections and majority voting.
16. What is Read Preference?
Determines where reads occur.
Options
- Primary
- PrimaryPreferred
- Secondary
- SecondaryPreferred
- Nearest
Read Preference
flowchart LR
Application --> ReadPreference
ReadPreference --> Primary
ReadPreference --> Secondary
17. What is Primary Read Preference?
Reads only from Primary.
Provides strongest consistency.
18. What is Secondary Read Preference?
Reads only from Secondary.
Useful for
- Reports
- Analytics
- Dashboards
19. What is Nearest Read Preference?
Reads from the closest node based on network latency.
Useful in global deployments.
20. What is Write Concern?
Write Concern determines when MongoDB acknowledges writes.
Common values
- 0
- 1
- majority
21. What is Majority Write Concern?
Write succeeds only after majority of Replica Set members acknowledge it.
Provides highest durability.
22. Difference between Read Preference and Write Concern?
| Read Preference | Write Concern |
|---|---|
| Controls Reads | Controls Write Acknowledgement |
| Client Reads | Data Durability |
23. What is Read Concern?
Read Concern determines consistency level of read operations.
Options include
- local
- majority
- linearizable
- available
- snapshot
24. What is Rollback?
If a Primary crashes before replication completes,
unreplicated operations may be rolled back.
25. What is Initial Sync?
When a new Secondary joins,
MongoDB copies the complete dataset.
Then
Starts reading Oplog.
Initial Sync
flowchart LR
Primary --> InitialCopy --> Secondary
Secondary --> OplogSync
26. Banking Example
Money Transfer
Primary
↓
Oplog
↓
Secondaries
Ensures data availability.
27. E-Commerce Example
Customer Order
↓
Primary
↓
Replica Set
↓
Secondary used for reports.
28. Social Media Example
Posts
↓
Primary
↓
Followers
↓
Secondary reads
for timelines.
29. Disaster Recovery Example
Primary
↓
Data Center Failure
↓
Secondary promoted
↓
Business continues.
30. What happens during network partition?
Replica Set elects a Primary only if majority exists.
Minority partition becomes read-only.
Network Partition
flowchart LR
NetworkFailure --> MajorityGroup --> Primary
NetworkFailure --> MinorityGroup --> ReadOnly
31. Common replication issues
- Replication Lag
- Network Latency
- Oplog Overflow
- Slow Secondary
- Election Storms
- Disk Bottlenecks
32. What is Replication Lag?
Delay between Primary write and Secondary synchronization.
Large lag affects
- Read Consistency
- Failover
33. How do you monitor Replica Sets?
Useful commands
rs.status()
rs.printReplicationInfo()
rs.printSecondaryReplicationInfo()
MongoDB Atlas also provides monitoring dashboards.
34. Enterprise Best Practices
- Use at least three Replica Set members.
- Use Majority Write Concern for critical data.
- Monitor replication lag.
- Keep Oplog appropriately sized.
- Place nodes across availability zones.
- Avoid reading stale data when consistency is required.
- Monitor elections.
- Regularly test failover.
- Use Atlas alerts.
- Enable backups in addition to replication.
Replica Set Workflow
flowchart LR
Write --> Primary --> Oplog --> Secondary1
Oplog --> Secondary2
Secondary1
-.Election.->Primary
Quick Revision
| Topic | Key Point |
|---|---|
| Replication | Multiple Data Copies |
| Replica Set | Replication Group |
| Primary | Handles Writes |
| Secondary | Replicates Data |
| Arbiter | Votes Only |
| Oplog | Operation Log |
| Election | Select New Primary |
| Failover | Automatic |
| Read Preference | Read Location |
| Write Concern | Write Durability |
| Read Concern | Read Consistency |
| Majority | Strong Durability |
Interview Tips
Interviewers frequently ask
- What is MongoDB Replication?
- Explain Replica Sets.
- What is the Oplog?
- How do elections work?
- What happens if the Primary fails?
- What is Read Preference?
- What is Write Concern?
- Explain Replication Lag.
- Why is an Arbiter used?
- Explain Automatic Failover.
Always explain that MongoDB Replica Sets provide high availability through automatic replication and elections, ensuring applications remain available even when a server fails.
Summary
MongoDB Replication uses Replica Sets to maintain multiple synchronized copies of data across servers. Through the use of Primary and Secondary nodes, the Oplog, automatic elections, Read Preferences, and Write Concerns, MongoDB delivers high availability, fault tolerance, disaster recovery, and read scalability.
Understanding Replica Set architecture, failover mechanisms, consistency options, replication lag, and monitoring techniques is essential for designing resilient MongoDB deployments and succeeding in backend engineering, cloud, and solution architect interviews.