Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

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:

  1. 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.
  2. 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

Database Leader vs Read Replicas
RoleLeader (Primary)Read Replica (Follower)
Permitted OperationsRead and Write (INSERT, UPDATE, DELETE)Strictly Read-Only (SELECT)
Scaling PurposeWrite serialization and consensus coordinatorHorizontal read throughput expansion
Instance CountExactly 1 active write leader per cluster1 to many distributed read replicas
Failure ModeRequires automated failover to promote replicaCan fail without interrupting writes

Leader and followers

WRITEreplicatereplicateREADClientwrite v2Leader · writesversion 1Follower 1version 1Follower 2version 1
Write destination: leaderRead destination: follower

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.

    01Why does leader-follower database replication fail to increase write throughput?
    02What is 'replica lag' in asynchronous database replication?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson