If youβre building a modern application that needs speed, availability, and fault tolerance, MongoDB replication is one of the easiest and most powerful features you can use.
Replication ensures you have multiple copies of your data across different servers.
In MongoDB:
The Primary node handles all writes.
Secondary nodes replicate data from the primary and handle reads (optional).
If the primary fails, MongoDB automatically promotes a secondary β new primary.
This setup is known as a Replica Set, which is MongoDBβs improved version of the old masterβslave architecture.
Masterβslave replication exists because one single database server cannot handle everything reliably, efficiently, and safely. Splitting responsibilities between a master (primary) and slaves (replicas) solves several real-world problems.
+-----------------+
| Primary | <--- Write & Read
+-----------------+
/ \
/ \
+-----------------+ +-----------------+
| Secondary 1 | | Secondary 2 |
+-----------------+ +-----------------+
Read Only Read Only
Slave replication means:
One database is the master β accepts all writes
Other databases are slaves/replicas β copy whatever master does
Replicas stay almost up-to-date with the master
Think of it like a teacher (master) writing on the board, and
3 students (slaves/replicas) copying everything in real time.
Sometimes students copy late β this is replication lag.
Below is the internal flow, no fluff:
MySQL β Binary Log (binlog)
MongoDB β Oplog
PostgreSQL β WAL Log
Example log entry:
UPDATE products SET stock = 50 WHERE id = 101
A slave says:
β‘οΈ βMaster, give me the next log entry after position X.β
Whatever operation appears in the log:
INSERT
UPDATE
DELETE
β¦slaves replay them in the same order.
This cycle repeats non-stop.
Slaves may fall behind due to:
Slow network
Heavy load on slave
Large write burst on master
Slow disk I/O
Complex queries running on slave
When the slave is behind, it shows old data temporarily.
Apps often use:
Normal read β from replica (fast)
Critical read (e.g., after update) β from master
This avoids stale data issues.
If a user writes something (POST/UPDATE):
You route that userβs next read to the master only
Until replica catches up
Frameworks like Rails, Laravel, Django already support this.
Tools measure:
replication_lag = slave_last_applied_timestamp - master_timestamp
If lag > X seconds:
β temporarily stop sending reads to that replica
MongoDB allows:
{ writeConcern: { w: "majority" } }
Meaning:
β Write is successful only when most replicas have it
This reduces risk of data inconsistency.
Master waits until at least one slave confirms:
βYep, I received the log.β
This avoids data loss.
Letβs use a real example:
UPDATE users SET name='Nishikanta' WHERE id=5;
Hereβs what happens:
Master updates row in its storage engine.
Master adds entry to binlog/oplog:
{ op: "update", id: 5, name: "Nishikanta" }
Replica 1 β βGive me log #520β
Replica 2 β βGive me log #520β
Master streams them.
Replica updates:
id=5 β name=Nishikanta
Once all logs are applied β theyβre in sync.
βββββββββββββββββββββββββ
β MASTER β
β (Primary - All Writes)β
βββββββββ¬ββββββββββββββββ
β
Writes β Binlog/Oplog generated
β
βββββββββββββββββ΄ββββββββββββββββ
β β
ββββββββββββββββββββββ ββββββββββββββββββββββ
β SLAVE #1 β β SLAVE #2 β
β (Replica - Reads) β β (Replica - Reads) β
βββββββββ¬βββββββββββββ ββββββββββββ¬ββββββββββ
β β
β Fetch log continuously β
β Apply updates β
β Handle replication lag β
βΌ βΌ
Up-to-date copy of DB Up-to-date copy of DB
Masterβslave (primaryβreplica) replication is one of the most essential techniques for building fast, reliable, and highly available modern applications. By separating write operations to the master and distributing read operations across multiple replicas, systems can handle far more traffic, avoid downtime, and guarantee that data remains safe even if a server fails.
Although replication may introduce small delays (replication lag), most real-world systems manage this easily through smart routing, read-after-write strategies, and monitoring tools. The result is a database architecture that provides scalability, resilience, and real-time data redundancy β all without changing how your application writes data.
Whether youβre using MongoDB, MySQL, PostgreSQL, or Redis, mastering replication is a core skill that enables you to build applications that stay fast, stay online, and scale gracefully as your users grow.