AlgoViz

Replication lag and stale reads

hard 7 min

Replication separates log creation, transport, durability on a replica and replay into visible state.

Video lesson · Chapter 1 of 4

A confirmed order vanishes on refresh

0:00 / 2:20

Theory

Read or listen first. About 2 min.

What it is

Replication separates log creation, transport, durability on a replica and replay into visible state. A primary may acknowledge before a particular reader can see its change. Byte lag indicates work outstanding. Time lag also depends on workload, reporting and replay progress. Read scaling introduces a visibility contract as well as extra capacity.

Why it exists

An order creation succeeds and the next request returns no order. Trace the two requests to determine their serving nodes. Compare the write’s position with the selected replica’s replay position. Route the follow-up to a sufficiently caught-up replica or the primary when the session requires read-your-writes. Reserve replica reads for workloads that tolerate staleness.

If the selected reader has applied the write, check transaction snapshot age, cache keys and whether the read targeted the correct tenant.

A confirmed order vanishes on refresh

Primary → Replica replay (ship and replay WAL). Read endpoint → Replica replay (read applied state)
Primary → Replica replay (ship and replay WAL). Read endpoint → Replica replay (read applied state)

Inside this chapter

Single-leader replication
One leader accepts writes for an assigned range and propagates changes to followers. Failover requires authority and a decision about which acknowledged history must survive.
Multi-Leader replication
Multiple leaders can accept independent writes. Concurrent updates may conflict, so the system needs merge semantics that preserve the application’s actual invariants.
Synchronous vs. Asynchronous replication
Synchronous acknowledgment waits for a configured replica condition. Asynchronous acknowledgment can return sooner while leaving more recent data at risk after primary loss.
Replication Lag and its impact
Separate send, receive, flush and replay positions. Two replicas with equal received bytes can serve different visible states because replay progresses at different speeds.
Quorum Reads and Writes
Read and write sets can be chosen to intersect. Intersection alone does not guarantee linearizability under every protocol, membership change, or concurrent write and failure pattern.
Read Replicas and Read Scaling
Read replicas scale workloads that accept their visibility contract. Route a causally dependent read using a replay token or an authoritative path when the product requires it.
Conflict Resolution Strategies
Last-write-wins discards competing versions by a chosen ordering. Domain-aware merges can preserve more information but require rules for conflicts and retries.

How it works

Step through the idea, or press Play.

Step 1 of 4

Order on the primary. The primary assigns the operation a log position.

Understand it

Worked incident

A confirmed order vanishes on refresh

ObservationReplay lag behind primary (MiB)Interpretation
Baseline0The replica replays changes before the follow-up read.
Incident42A backup slows replay while shipping continues.
After intervention1Use a session position or route the follow-up appropriately. Confirm replica replay catches up.
Illustrative measurements from the same workload at three checkpoints.

Choose the next investigation

If the selected reader has applied the write, check transaction snapshot age, cache keys and whether the read targeted the correct tenant.

Summary

The whole topic in one minute.

Received or flushed bytes may still await application to the state used by the reader.

If the selected reader has applied the write, check transaction snapshot age, cache keys and whether the read targeted the correct tenant.

Sources