Replication: Leader, Followers, and Read Replicas
Understand the idea
Why read replicas handle millions of viewers but do not increase your write capacity.
The idea
means automatically copying data from one primary database server to one or more secondary servers over the network. In most web apps, users read data far more often than they write it (for example, thousands of people read a social post or product page for every one person who creates one). Replication is the standard way to handle heavy read traffic and keep a backup ready if a server goes down.
In a Leader-Follower setup:
- The Leader (Primary) handles all changes (INSERT, UPDATE, DELETE). It writes each change to its and sends that log over the network to the followers.
- The Followers () receive the log, apply the changes, and serve read-only queries (SELECT).
An important rule to remember: replication scales reads, but it does NOT scale writes. Every change must still go through the single leader server. Also, because copying takes a few milliseconds, there is replica lag: if a user updates their profile and immediately loads a page served by a replica that hasn't caught up yet, they might briefly see their old information.
What changes
| Role | Leader (Primary) | Read Replica (Follower) |
|---|---|---|
| Permitted Operations | Read and Write (INSERT, UPDATE, DELETE) | Strictly Read-Only (SELECT) |
| Scaling Purpose | Write serialization and consensus coordinator | Horizontal read throughput expansion |
| Instance Count | Exactly 1 active write leader per cluster | 1 to many distributed read replicas |
| Failure Mode | Requires automated failover to promote replica | Can fail without interrupting writes |
Leader and followers
The leader and both followers start with version 1.
01 / 07Leader and followers
The command
Simulating read replica lag and read-your-own-writes
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Run the example to see how an immediate read from a lagged follower returns stale data until asynchronous replication catches up.
Guided challenge
Implement read-your-own-writes routing
Update the function to return 'leader' if isWrite is true OR justWrote is true, and 'replica' otherwise.
Implement read-your-own-writes routing
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
leader leader replica
Hints 0/1
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.