Topic 4 of 5 · 5 min read
Replication lag and read consistency
Goal: Predict the stale read a replica-lag scenario produces and choose a fix that routes that read correctly.
With leader-follower replication, a write lands on the leader first and reaches replicas a moment later, so a read sent to a replica right after a write can return old data. Decide per read whether that is acceptable, and route the reads that cannot be stale to the leader.
Check yourself · 1 of 3
A user updates their email and reloads the page, which reads from an asynchronous replica. What may they see?
Why it matters
Replicas exist to spread read load and to survive a failure. They are cheap to add, and the bug they cause is easy to miss in testing, because on a quiet development machine the lag is close to zero. In production under load a replica can fall seconds or more behind. The user then saves a profile, reloads, and sees the old profile.
How it works
In leader-follower (also called primary-replica) replication, the application writes to the leader. The leader ships its changes to followers, which apply them. With asynchronous replication the leader confirms the write before followers have it. That gives fast writes and a window of lag. Synchronous replication closes the window at the price of slower writes and a stall when a replica is down. PostgreSQL supports both modes.
The result is eventual consistency on replicas: given no new writes, they catch up. That is fine for some reads and wrong for others.
Choosing the route per read
- Read-your-writes: after a user changes something, they must see it. Read from the leader for a short window, or read from a replica only when it has reached the write's position.
- Monotonic reads: a user must not see data go backwards between two page loads. Pin a user's session to one replica.
- Stale is fine: dashboards, public product lists, search counts. Use replicas freely.
Before and after
// Weak: every read goes to a replica, even right after the write.
await db.leader.update("profiles", { id, bio });
return db.replica.findOne("profiles", { id }); // may return the old bio
// Fixed: the read that follows the write goes to the leader.
await db.leader.update("profiles", { id, bio });
return db.leader.findOne("profiles", { id });
Routing every read to the leader after a write is simple and correct for a few seconds. Do it only for the user who wrote, not for everyone.
What goes wrong
- Testing on one node. The bug never shows until production.
- Read-modify-write on a replica. A stale read feeds a write that overwrites newer data.
- Failover losing recent writes. An asynchronous replica promoted to leader may be missing the latest commits.
- Blaming caching. The page is stale because of the replica, and clearing the cache does nothing.
Examples
Profile save then reload
A user saves a new display name; the app redirects to the profile page, which reads from a replica 400 ms behind. The old name shows. Fix: for 5 seconds after any write by that user, send their reads to the leader. Other users keep reading from replicas.
Counter overwrite
Code reads stock from a replica (10), subtracts 1 and writes 9. The leader already had 7 from earlier sales. The write overwrites newer data. Use UPDATE stock SET qty = qty - 1 WHERE qty > 0 on the leader.
Key points
- Asynchronous replicas lag behind the leader, so a read right after a write may return old data.
- Decide per read whether staleness is acceptable: read-your-writes, monotonic reads, or stale is fine.
- Send reads that must see a user's own write to the leader, or wait until the replica has caught up.
- Never base a write on a value read from a replica; read it from the leader or use a conditional update.
- Synchronous replication reduces lost writes but slows writes and can stall when a replica fails.
Common mistakes
- Adding replicas without deciding which reads may be stale.
- Reading from a replica and then writing a value computed from it.
- Assuming failover never loses acknowledged writes under asynchronous replication.
- Testing only against a single local database.
Check yourself · 2 of 3
Which read can safely use an asynchronous replica?
Check yourself · 3 of 3
A user must see their own change immediately after saving. What is the simplest correct fix?
Practice items, not counted towards your mastery.
Start here
- Designing Data-Intensive Applications (opens in a new tab)BookPaid
The standard book on storage engines, replication and consistency trade-offs behind every database choice.
- SQLite: Appropriate uses (opens in a new tab)DocsFree
The SQLite authors list where an embedded database fits and where a client/server one is the better tool.
- MongoDB: Data modeling (opens in a new tab)DocsFree
Embedding versus referencing, and the schema design patterns for document stores.
More for this topic
- PostgreSQL: High availability, load balancing and replication (opens in a new tab)DocsFree
How PostgreSQL streaming replication works and what a standby can and cannot guarantee on reads.