CMD Guide
HomeSystem Design

Leader and Follower

Step 20 in the System Design path · 3 concepts · 0 problems

0 / 3 complete

📘 Learn Leader and Follower from zero

Start from the problem. You have one piece of data that many clients want to read and write at once. If every node accepts writes independently, two clients can change the same record concurrently and the copies disagree — there is no single source of truth. You also want the data to survive a machine dying and to serve fast reads under heavy traffic.

The analogy. Picture a classroom with one teacher (the leader) and several teaching assistants (the followers). Only the teacher writes in the master notebook — every change flows through the teacher, so there is never a conflict over the "official" value, and the order changes happen in is fixed. The teacher then dictates each change to the TAs, who copy it into their notebooks. Students may ask any TA a question (a read), spreading the load. If the teacher disappears, the TAs hold a quick vote and promote one of themselves (leader election / failover).

Worked example. A photo app uses one leader DB and two followers. A user posts a caption — that write goes only to the leader, which appends it to its replication log at a definite position and applies it. The leader streams that log to both followers (replication). Millions of viewers then read captions from the followers, so reads scale out. If the leader's disk fails, the system notices the missing heartbeat, the followers elect the one with the most up-to-date log, and writes resume on it. The catch: a follower may lag a few hundred milliseconds, so a viewer can briefly see the old caption — that is replication lag.

Key insight: funnel all writes through ONE leader to get a single, conflict-free order, then replicate that ordered log to followers to scale reads and tolerate failure — trading away write scalability and perfect read freshness in exchange for a simple consistency model and high read availability.

✨ Added by the guide to build intuition — not from the source course.

Lessons in this topic

🎯 Guided practice

  1. Easy — "Where does each operation go?"

    You run one leader and three followers for a user-profile service. A client (a) updates their display name, then (b) loads their profile page. Route each operation.

    Reasoning: Step 1 — classify each op. (a) is a write; writes must go through the single leader to preserve the total order, so it goes to the leader. (b) is a read; reads can be served by any follower to spread load. Step 2 — spot the trap: if you route (b) to a follower immediately after (a), replication may not have caught up, so the user sees their OLD name — a read-your-writes (read-after-write) consistency violation. Step 3 — fix it: for a short window after a user's own write, serve that user's reads from the leader (or from a follower you know has applied that write, e.g. by tracking the log position the write reached). Core takeaway: writes → leader (ordering), reads → followers (scale), but a user's reads of their own recent writes need special routing to beat replication lag.

  2. Medium — "The leader just died. What happens?"

    Leader L and followers F1, F2 use asynchronous replication. L crashes after acknowledging write W to the client but before W reached any follower. The system must stay available. Walk through the recovery and the risk.

    Reasoning: Step 1 — detection: followers miss L's heartbeat past a timeout and declare L dead. Step 2 — election: the followers run a leader-election protocol (e.g. Raft) requiring a majority quorum to agree, which prevents two leaders from being elected at once. Say F1 has the highest replication position (Raft only elects a candidate whose log is at least as up-to-date as the voter's) and wins. Step 3 — the loss: because W was async and never replicated, F1's log lacks W; when F1 becomes leader, W is silently lost even though the client was told "success." Step 4 — the second danger: if the old L revives and still believes it is leader, you get split-brain; the quorum rule stops a second leader from being elected, and fencing tokens stop the stale L's late writes from being accepted by the storage layer. Step 5 — the design fix: switch to semi-synchronous replication so the leader acks W only after at least one follower has stored it, bounding worst-case loss to in-flight writes (assuming the leader and that synchronous follower don't fail together). Core takeaway: single-leader gives clean ordering, but failover forces a choice on the durability-vs-availability spectrum — quorum elections prevent two leaders, fencing tokens neutralize a stale one, and the sync level sets how much acknowledged data a failover can lose.

✨ Added by the guide — work these before the full problem set.

🧠 Review & recall

Active recall is what moves a topic into long-term memory. Flip each card before revealing, then test yourself — your results are saved on this device.

Flashcard
What core problem does the Leader and Follower pattern solve, and what is the leader's job?
tap to reveal →
When many clients read and write the same data, independent writes on multiple nodes cause conflicting copies with no single source of truth. The pattern elects one server (the leader) to handle all writes, coordinate work, and replicate data; this gives a single, conflict-free order of changes.
💡 One teacher writes the master notebook — no two TAs argue over the official value.
Flashcard
What roles do followers play in the Leader and Follower pattern?
tap to reveal →
Followers only accept writes from the leader and serve as backups. They can also serve read requests for load balancing, and if the leader fails one follower can be promoted to become the new leader.
💡 TAs copy the teacher's dictation, answer student questions (reads), and one can be promoted if the teacher vanishes.
Flashcard
Why does the lesson say quorum alone is insufficient, motivating the leader approach?
tap to reveal →
Quorum requires a majority of nodes to participate in every read and write, which lowers availability — the system needs a majority up at all times or operations fail. It is also not fully sufficient because in certain failure scenarios clients can still see inconsistent data.
💡 Majority-vote on every op = fragile + still can show stale data → funnel through one leader instead.
Flashcard
What is replication lag in the Leader and Follower pattern, and what consistency issue does it cause?
tap to reveal →
A follower may lag behind the leader by a few hundred milliseconds, so a viewer reading from a follower can briefly see the old value. If a user reads their own recent write from a lagging follower they see stale data — a read-your-writes (read-after-write) consistency violation.
💡 TA hasn't finished copying yet → you see yesterday's caption for a moment.
Flashcard
During failover, what prevents two leaders from being elected, and how is a stale old leader's writes neutralized?
tap to reveal →
Leader election (e.g. Raft) requires a majority quorum to agree, which prevents two leaders at once (avoiding split-brain). Fencing tokens stop a revived stale leader's late writes from being accepted by the storage layer.
💡 Quorum stops a SECOND leader; fencing tokens disarm the ZOMBIE old leader.
Flashcard
How does the replication sync level (async vs semi-synchronous) trade off durability against availability?
tap to reveal →
With asynchronous replication, the leader can ack a write before any follower stores it, so a crash can silently lose acknowledged data. Semi-synchronous replication makes the leader ack only after at least one follower has stored the write, bounding worst-case loss to in-flight writes.
💡 Async = fast but can lose acked writes; semi-sync = wait for one follower so loss is bounded.
Q1. In the Leader and Follower pattern, where must a write operation go?
Q2. Why does the lesson argue that using quorum (instead of a leader) can be problematic?
Q3. A user updates their display name (a) then immediately loads their profile page (b) on a 1-leader, 3-follower service. What is the correct routing and main trap?
Q4. A leader using asynchronous replication acks write W to the client, then crashes before W reaches any follower. After a Raft failover, what happens to W?
Q5. After a leader crash, which mechanism specifically prevents a revived stale leader's late writes from corrupting storage?