Replication keeps multiple copies of the same data on different mongod instances. If one server fails, another copy can keep the application running.
Main benefits: redundancy, high availability, automatic failover, better data availability, disaster-recovery copies, and—when configured—optional read distribution.
Remember
Replication = multiple live copies of the same data
Replica set → primary (writes) + secondaries (replicate)
Oplog → log of operations secondaries apply
Failover → election can choose a new primary
Not the same as a backup.
Architecture
What Is a Replica Set?
A replica set is a group of mongod instances that maintain the same data set. At any time, one member is the primary; the others are secondaries.
The primary receives writes. Secondaries replicate changes from the primary so their data sets converge toward the same state.
Replica set
Application
|
v
Primary
/ \
v v
Secondary Secondary
Motivation
Why Use Replication?
Reason
What it gives you
Server failure protection
Other members still hold the data
High availability
A new primary can be elected after failure
Multiple copies
Redundancy across machines or sites
Disaster recovery / reporting
Dedicated copies for special purposes
Geographic distribution
Data closer to users in more than one region
Optional read distribution
Some reads can target secondaries (may be stale)
Replication is not a backup. Replication maintains additional current copies. Backups provide historical recovery points. You need both for a complete strategy.
Approach
How MongoDB Replication Works
Writes go to the primary. The primary records each change in its oplog. Secondaries read that oplog and apply the same operations asynchronously.
Replication flow
Application
|
v
Primary
|
v
Oplog
|
+----------+
| |
v v
Secondary Secondary
The application sends a write to the primary.
The primary applies the write to its data.
The primary records the operation in its oplog.
Secondaries read operations from the oplog.
Secondaries apply those operations to their own data.
Members converge toward the same data set (async—not instant).
Roles
Primary and Secondary Members
Primary
The primary receives write operations, records changes in the oplog, and can serve reads. It may step down when needed. If it becomes unavailable, an election can select a new primary.
Secondary
A secondary replicates from the primary and keeps its own copy of the data. It can serve reads when the client’s read preference allows it, and an eligible secondary can become primary through an election.
Because replication is asynchronous, a secondary may briefly lag behind the primary. Secondary reads can therefore return data that is not yet identical to the primary.
Mechanism
Oplog
The oplog (operation log) is a special log of write operations on the primary. Secondaries use it as the source of truth for which changes to apply next.
The secondary applies them so its data catches up.
Think
Pseudocode
Pseudocode
Write data to Primary
Primary records the operation
Primary writes operation to oplog
Secondary reads oplog
Secondary applies operation
Repeat for future operations
Trace
Worked Walkthrough
Insert one product through the primary, then follow how secondaries catch up.
Secondaries replicate the operation from the oplog.
Secondaries eventually contain the same document—not necessarily instantly.
Availability
Automatic Failover and Elections
If the primary becomes unavailable long enough, eligible members hold an election to choose a new primary.
Failover
Before failure:
Primary
|
+-- Secondary
+-- Secondary
Primary fails
X
Secondary A
|
Election
|
New Primary
The set detects that the primary is unavailable.
Eligible members participate in an election.
A new primary is selected.
Compatible drivers can detect the change and retry certain writes automatically.
By default, electionTimeoutMillis is 10 seconds (MongoDB’s documented default). Network conditions and configuration affect how long an election actually takes—do not treat failover as instant.
Delay
Replication Lag
Replication lag is the delay between an operation on the primary and the application of that operation on a secondary from the oplog.
Lag idea
Primary:
Insert Order #101
|
| replication delay
v
Secondary:
Insert Order #101
Small lag can be normal.
Large lag can signal performance or infrastructure problems.
Secondary reads can return older data while lag exists.
Monitor lag in production (for example with rs.status()).
Reads
Read Preference
Read preference controls which replica set member(s) a client uses for reads. The default is primary. Non-primary modes may return stale data because replication is asynchronous.
Read Preference
Basic idea
primary
Read from the primary (default)
primaryPreferred
Prefer the primary; use a secondary if needed
secondary
Read from secondary members only
secondaryPreferred
Prefer a secondary; fall back to primary if needed
nearest
Read from a suitable low-latency member (primary or secondary)
Writes
Write Concern
Write concern controls the level of acknowledgment requested for a write operation. For most deployments, the default is w: "majority".
MongoDB waits for the requested acknowledgment level.
With w: "majority", acknowledgment comes after a calculated majority of data-bearing voting members have durably recorded the change in their local oplog.
Do not mix these up: preference chooses where to read; concern settings describe acknowledgment and consistency behavior.
Related
Transactions and Replication
Multi-document transactions are available on replica sets. Transactions that contain reads must use read preference primary, and all operations in a transaction route to the same member.
Replication and transactions are different features: replication copies data across members; transactions group multiple operations into one atomic unit.
Code
MongoDB Replication Examples
Short mongosh-oriented examples. Run them against a replica set (standalone servers do not demonstrate failover or secondary reads).
Default detection uses a 10s election timeout; network conditions can extend the window without a writable primary.
scale
Replica sets are not unlimited read scaling
Secondary reads can help some workloads, but consistency, lag, and write amplification still matter. Scaling reads is a design choice, not free capacity.
Analysis
Performance Notes
Idea
Practical tip
Extra work
Replication adds network and storage work as ops propagate
Oplog apply
Secondaries must read and apply oplog entries continuously
Lag
Monitor lag; heavy write bursts can increase it
Secondary reads
Can offload some reads—choose based on consistency needs, not speed alone
Write concern
Stronger acknowledgment can increase write latency
Reference
Cheat Sheet
Concept
Remember
Replica set
Group of MongoDB instances maintaining the same data
Primary
Receives writes
Secondary
Replicates data from the primary
Oplog
Records operations for replication
Election
Selects a new primary when needed
Replication lag
Delay between primary and secondary application
Read preference
Controls where reads are directed
Write concern
Controls write acknowledgment
Read concern
Controls read consistency / isolation behavior
Remember
Key Takeaways
Replication maintains multiple live copies of MongoDB data.
A replica set has one primary and one or more secondaries.
Writes go to the primary; secondaries apply oplog operations asynchronously.
Elections can select a new primary after failure; timing is not guaranteed.
Replication improves availability but does not replace backups.
One line: A replica set keeps the same data on multiple servers—writes hit the primary, secondaries follow the oplog, and elections restore a writable primary after failure.
Frequently Asked Questions
Replication keeps multiple copies of the same data across mongod instances in a replica set. It provides redundancy and high availability so the deployment can continue if a member fails.
A replica set is a group of mongod processes that maintain the same data set. One member is the primary (accepts writes); the others are secondaries that replicate the primary's oplog.
The primary receives write operations and records them in the oplog. Secondaries asynchronously apply those oplog operations to their own data. Clients read from the primary by default; read preference can target secondaries.
The oplog (operation log) is a special capped collection on the primary that records write operations. Secondaries read those entries and apply them so their data sets converge toward the primary.
No. Secondaries replicate the primary's oplog asynchronously. That means a secondary can temporarily lag behind the primary, and secondary reads may return stale data.
If the primary does not communicate with other members for longer than electionTimeoutMillis (10 seconds by default), an eligible secondary calls an election. A new primary is chosen; actual timing depends on configuration and network conditions.
Yes, by configuring read preference (for example secondary or secondaryPreferred). Non-primary modes may return stale data because replication is asynchronous.
Replication lag is the delay between an operation occurring on the primary and that same operation being applied on a secondary from the oplog. Small lag can be normal; large lag needs investigation.
Read preference controls which replica set member(s) a client uses for reads. The default is primary. Other modes can use secondaries but may see older data.
Write concern is the level of acknowledgment requested for a write. For example, w: "majority" asks MongoDB to acknowledge after a calculated majority of data-bearing voting members have durably recorded the change in their local oplog. It is the default for most deployments.
No. Replication keeps additional live copies of current data for availability. Backups provide historical recovery points. You still need a backup strategy.
There is no single universal number. Production topologies commonly use an odd count of voting members (often three) so the set can elect a primary with a majority. Exact size depends on availability goals, cost, geography, and whether you use arbiters—see the official MongoDB replica set documentation for deployment guidance.