Replication Lag Read Guarantees and Failover

Replication Sharding and the Capstone · 45 min

System Design · Module 7 · Lesson 1 of 4 · 45 min

Replication Lag Read Guarantees and Failover

Distinguish copies from partitions, trace a successful write missing from a follower, and state the durability and authority needed for failover.

A copied dataset is useful only when you state how current it is, which writes it acknowledges and who may become the writer.

01 The idea

The problem this lesson solves

A user creates a7 and immediately opens it. Creation succeeds, but the redirect says the code is missing. The write reached the leader while the read reached a follower that has not applied it yet. Replication creates useful copies, but it also creates a question every read must answer: which copy is allowed to speak for this operation?

Use the API, database, capacity and cache concepts from earlier modules. These lessons add distributed ownership and failure trade-offs.

LeaderThe node authorized to accept writes for a replication group under the design.
Replication lagThe gap between progress on the writer and progress received or applied on a follower.
Read-your-writesA guarantee that a caller can observe its own completed changes under the stated contract.

02 Worked example

Read a newly created link from a lagging follower

Log progress and query visibilityLeader commits a7Accepted on leaderWrite acknowledgedFollower receives logReceived / flushedMay not be queryableFollower replays loga7 becomes visibleFresh read policy mattersLog progress and query visibilityLeader commits a7Accepted on leaderWrite acknowledgedFollower receives logReceived / flushedMay not be queryableFollower replays loga7 becomes visibleFresh read policy matters
A follower can have received the log without replaying it. Route a fresh read to the authority or wait for a suitable replay position.
1
Commit on leaderCreate a7 and acknowledge it to the client.
2
Lag on followerThe follower has not applied a7.
3
Read the followerThe client may see missing unless the design supplies a stronger read policy.
4
Choose recoveryUse authoritative routing or a replay-position check with bounded wait and fallback.

03 The method

Understand the mechanism

Explain what a replica copies

In a leader-follower design, writes are accepted through the designated leader and changes are transferred to followers. A follower may serve suitable reads or become a failover candidate under the deployment’s protocol.

A full replica stores another copy of the same logical dataset. It does not divide the write workload the way sharding does. Each full follower must receive and apply the relevant changes. Adding followers can increase read-serving capacity but also adds replication work and operational state.

Replication can be physical or logical depending on the database and configuration. The designer’s immediate questions are which changes are copied, how far each copy has progressed, what acknowledgment means and how a new authority is chosen after failure.

Keep backups separate. A mistaken deletion can replicate to every follower. Historical recovery needs retained backup or log history under a tested restore policy, not merely another live copy of the current mistake.

Separate receipt, durable storage and replay

A follower can receive a log record before it has durably stored it, and can store it before applying it to query-visible state. These stages matter for both durability and read freshness.

For a7, the leader may have committed and acknowledged the creation while the follower has not replayed the change. A query on that follower can legitimately see no row under its current state. That missing observation is not evidence that the original creation failed.

Waiting for a follower to receive or flush a record is not automatically the same as waiting for it to apply the record for queries. Database-specific synchronous modes define different acknowledgment boundaries. Name the actual condition instead of using “sync” as a complete explanation.

Lag can be described in time, bytes or log positions, and each measurement has interpretation limits. A single lag field may be unavailable or uninformative during idle periods. Combine replication state with workload-aware observations and the guarantee the read path needs.

Identify three user-visible lag problems

Read-your-writes can fail when a user writes to the leader and then reads a lagging follower. The interface appears to forget the user’s own action. A retry can also create duplicate work if the application mistakes follower absence for proof that a prior operation did not exist.

Monotonic reads can fail when a user first reads a more advanced copy and later reads a less advanced copy. The displayed state goes backward. Pinning a user to one copy may reduce some movement, but failover and routing changes still need a consistency policy.

Related facts can be observed inconsistently if they are read from copies at different progress points. A client might see a newly referenced object but not an earlier prerequisite from another path. The exact anomaly depends on the data and replication model, but arbitrary read routing should not be assumed to preserve causal expectations.

These are product behaviors, not only database metrics. Decide which operations tolerate them. A public historical report and a post-purchase confirmation may need different read policies.

Choose an explicit fresh-read policy

One simple policy routes consistency-sensitive reads to the authoritative leader, assuming its read semantics meet the contract. This concentrates some load but makes the reasoning straightforward.

Another approach carries a write progress token and waits until a selected follower has replayed at least that position before reading. The wait must be bounded and the token interpreted correctly across the database’s timeline or epoch changes. On timeout, the service can fall back or return a documented unavailable outcome.

Temporarily routing a user to the leader for a fixed interval after a write is a heuristic unless the system can prove lag stays below that interval under all relevant conditions. It may improve experience, but should not be advertised as an unconditional guarantee.

Caches can defeat any of these policies if they return an older value first. The complete read path must honor the requested consistency, including local caches, shared caches and HTTP caching.

Compare acknowledgment and availability trade-offs

Asynchronous replication allows the leader to acknowledge without waiting for a follower under that mode’s contract. It can reduce write-path latency but leaves a window in which a leader loss may lose acknowledged changes if the surviving copy has not received the needed data.

Waiting for specified remote durability can reduce that loss window for the failures covered by the configuration. It adds network and remote processing to the write path, and an unavailable required acknowledgment partner can affect write availability.

Do not equate every product’s synchronous or semisynchronous terminology. Verify whether acknowledgment concerns receipt, flush, application, a quorum or a fallback mode. The operational configuration is part of the guarantee.

For a concrete product example, MySQL 8.4 semisynchronous replication waits for the configured replica acknowledgments of received and logged events, rather than waiting for those transactions to be applied for queries. A configured acknowledgment timeout can switch the source back to asynchronous behavior. Consequently, “semisynchronous” is not a blanket claim of immediate follower visibility or identical durability through every outage.

Choose from recovery objectives and failure domains. A second copy on the same vulnerable host or storage system does not protect against losing that shared dependency. Place copies and acknowledgment rules to match the failures the service promises to survive.

Fail over without creating competing authorities

Failover must select a suitable new leader, establish that it is allowed to accept writes, redirect clients and prevent the old leader from continuing conflicting writes if it reappears. This last requirement is often called fencing.

Simply promoting a follower because the leader is unreachable can be unsafe if the old leader is still serving another part of the network. The control plane needs an authority protocol appropriate to the database. Consensus and election algorithms are deeper topics; this lesson establishes the problem they must solve rather than pretending a health check alone solves it.

After failover, clients can have old connections, cached addresses and uncertain write outcomes. Recovery still needs idempotency and read policies. A new leader also may not contain every asynchronously acknowledged write, so the data-loss boundary must be honest.

Test failover with ongoing writes and reads. Verify both availability and the survival of acknowledged records. A green process check after promotion does not prove the service preserved its data contract.

Recognize the extra problem introduced by multiple leaders

Multi-leader designs accept writes at more than one location. They can support local writes or disconnected operation, but concurrent changes to the same logical object need a conflict policy.

If two regions assign different destinations to a7 while isolated, both cannot silently remain the single latest value without reconciliation. Last-write-wins chooses according to an ordering rule but can discard a legitimate update; clock assumptions and user expectations matter.

Some data has mergeable operations, such as certain counters or sets under a carefully designed model. Other data, such as exclusive seat ownership, needs stronger coordination or ownership partitioning. “Merge later” is not a universal solution.

Prefer one clear authority when it meets the workload and availability requirements. Introduce multiple writers only with a defined conflict, ordering, recovery and user-visible contract.

Guided tutorial

  1. Trace a missing recent write

    Create a timeline with leader commit of a7, acknowledgment, follower receipt and follower replay. Place a follower read before replay and record “not visible yet.” Explain why the caller must not treat this as proof that creation failed.

  2. Choose a read guarantee

    For the creator’s immediate verification, route to the leader or require an appropriate replay-position check with bounded fallback. For a lag-tolerant report, permit a follower read. Draw the cache behavior on each path so a stale cache cannot bypass the guarantee.

  3. Inspect a configured practice cluster

    The query below observes PostgreSQL replication clients when run with suitable access on a configured leader. An empty result can mean no connected replication clients; it does not prove zero lag. Interpret write, flush and replay observations as distinct stages.

  4. Review failover

    Assume the old leader is unreachable from the control plane but can still receive some client traffic. Describe how the deployment prevents two authorities before allowing the replacement to accept writes. Then verify a known acknowledged record on the replacement under the declared replication contract.

PostgreSQL observation query

# PostgreSQL observation in an appropriately configured practice cluster:
SELECT application_name, state, sync_state,
       write_lag, flush_lag, replay_lag
FROM pg_stat_replication;
# Empty output can mean this server currently has no connected replication clients.

Check your result

A leader with connected replication clients can report rows in pg_stat_replication. No rows does not prove zero lag. Distinguish log write, flush and replay before claiming a value is queryable.

04 Cheat sheet

Review the decisions

Use this table to explain the mechanism and its limit without memorizing a product name.

Choice or conceptRememberCheck
Async copyLower acknowledgment couplingPotential lag and failover loss
Synchronous acknowledgmentWaits for configured remote progressLatency/availability cost; inspect milestone
Leader readsFresh authoritative path under its contractLeader load and availability
Failover authorityOne valid writerRequires election/fencing design

05 Where & why

Check the primary references

Use official references for protocol and implementation details. The questions here are teaching prompts.

Primary reference
PostgreSQL streaming and synchronous replication

Read PostgreSQL streaming and synchronous replication

Primary reference
PostgreSQL replication observations

Read PostgreSQL replication observations

Primary reference
MySQL 8.4 semisynchronous acknowledgment and fallback

Read MySQL 8.4 semisynchronous acknowledgment and fallback

State the implementation version and the workload assumptions when defending your own design.

06 Interview questions

Explain the reasoning out loud

Answer before opening the explanation. The questions progress from vocabulary to decisions and failure behavior.

What does Leader mean?
The node authorized to accept writes for a replication group under the design.
What does Replication lag mean?
The gap between progress on the writer and progress received or applied on a follower.
What does Read-your-writes mean?
A guarantee that a caller can observe its own completed changes under the stated contract.
Why can a follower miss an acknowledged write?
Acknowledgment and follower replay need not happen together in asynchronous replication. The read policy must account for lag.
Does synchronous always mean applied?
No. The configured milestone may be receipt, flush or replay. State the setting’s actual guarantee.
What is split brain in this context?
Competing nodes can each accept writes while believing they are authoritative. The design needs a way to fence invalid writers and establish one authority.
Why are backups still required?
Live replicas can copy an accidental delete or bad update. Restoring a prior state needs retained recovery data and a tested procedure.
Is a fixed leader-pinning duration a proof of read-your-writes?
Only if its assumptions about progress are guaranteed. Otherwise it is a heuristic; a known replay position is a more explicit progress condition.

07 Practice problems

Practice a new case

Solve the new cases before revealing hints. Complete worked solutions follow the set.

Copies and partitions

Easy
A system has 4 shards and 3 full copies per shard; state the logical partitions and total shard copies.
Follow-up
A copy is not necessarily a separate physical machine.
Show the hint
Multiply only when counting copies.

Lag timeline

Easy
Explain a missing result on a follower 200 ms after a successful leader write when replay lag is 500 ms.
Follow-up
The write can be durable on the leader.
Show the hint
Compare the read time with the follower's progress.

Acknowledgment milestone

Medium
A follower has flushed a write but not replayed it; can its query be assumed to see the new row?
Show the hint
Durable receipt and application differ.

Backward read

Medium
A caller sees version 8 on follower A then version 6 on B; name the guarantee violated.
Show the hint
Track one caller’s observation order.

Leader failure

Medium
Explain the risk when an async leader fails before an acknowledged change reaches the promoted follower.
Show the hint
The durable copies may not contain the same progress.

Failover review

Hard
Review a design that promotes a follower solely because one monitoring request timed out.
Show the hint
Timeout is suspicion, not proof the old writer stopped.

Worked solutions

1. Copies and partitions

There are 4 logical partitions and 12 shard copies. Physical machines depend on how copies are placed.

2. Lag timeline

The follower may not have applied the write yet. Route the read to the leader or wait for a known replay position under a bounded consistency policy.

3. Acknowledgment milestone

No. Query visibility depends on replay/application progress, not merely the existence of durable log bytes.

4. Backward read

Monotonic reads are violated. Route consistently or use progress-aware selection under the required consistency contract.

5. Leader failure

The newly authoritative data can omit the acknowledged change. State the allowed recovery point and choose replication/failover settings accordingly.

6. Failover review

The old leader may still accept writes. Require an authority/fencing mechanism, evaluate replication progress and define client recovery and data-loss guarantees. Test partitions as well as process crashes.