Master–Slave (Primary–Replica) Replication

Search for a command to run...

No comments yet. Be the first to comment.
Mind maps are an excellent addition to an MCP ecosystem when they help users understand relationships between data, tools, and reasoning. They should complement—not replace—traditional dashboards, tab
Most AI agents today depend heavily on cloud APIs. They're fast, but every request costs money, depends on an internet connection, and sends your data to external providers. Over the weekend, I experi
How I use Fable 5 to research, build, and maintain custom Claude Code plugins, marketplaces, agents, and skills—creating reusable AI tooling that works across every project. 01 · The problem with copy

The way people search is changing. Is your site ready? For the past two decades, SEO (Search Engine Optimization) was the undisputed king of web visibility. Rank on Google's first page and you win tr
How we made 30 GB PSD uploads feel instant without expensive backend conversions. When we first built our media upload system, the architecture looked completely normal. A user uploaded a file, the b

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.