Replication, Read Replicas and Lag

Copies of the database that serve reads, and the window in which they are wrong.

Replication, Read Replicas and Lag

This module needs JavaScript: it steps through two transactions rather than showing a finished picture.

Worth knowing

One primary takes the writes and streams its log to replicas, which apply it and serve reads.
Replication lag is the delay between a commit on the primary and its arrival on a replica. It is never zero.
The classic bug: write, redirect, read from a replica, and the user does not see their own change.
Synchronous replication removes the lag and adds a network round trip to every commit. That is the trade, and it cannot be avoided.

Overview

In a common setup, one primary accepts writes and sends changes to one or more replicas. Replicas can serve reads, provide additional availability, or support backups and analytics. A replica is not automatically a backup: deletion or corruption can also be replicated.

In this guide
  1. Primary and replicas
  2. The read-after-write surprise
  3. Replicas do not make every query safe
  4. Choose a consistency policy per read
  5. Do not confuse a replica with a backup
  6. Further reading

Primary and replicas

With asynchronous replication, the primary can confirm a write before every replica has applied it. The delay between the primary and a replica is replication lag. A read sent to a lagging replica may not see a write that just succeeded on the primary.

The read-after-write surprise

A user updates a profile and is immediately redirected to a page that reads from a replica. If replication has not caught up, the old profile may appear. Applications can route that user's next read to the primary, wait for a replication position, or use synchronous replication where its latency and availability trade-offs are acceptable.

Failover also needs care: if a primary fails before a confirmed write reaches a promoted replica, that write may be lost depending on the replication mode and failover policy. Define the durability guarantee and recovery point you need.

Replicas do not make every query safe

Read scaling depends on replica capacity and workload. A large analytical query can compete with other replica reads. Monitor lag, replica health, and query load, and ensure backups are independently recoverable.

Replication behavior is product-specific. PostgreSQL supports streaming and synchronous replication modes; other engines use different protocols and guarantees. See CAP theorem.

Choose a consistency policy per read

Not every read needs the same freshness. A dashboard might accept a few seconds of lag, while a confirmation page immediately after a payment should usually read from the primary or wait until the replica has applied that write. Some systems expose a replication position or consistency token that lets an application wait for a particular write. This can improve read scaling without silently showing old state. Define a timeout and fallback for when the replica never catches up within the application's latency budget.

Do not confuse a replica with a backup

A replica can improve availability or provide extra read capacity, but it receives the same logical changes as the primary. An accidental deletion, bad migration, or malicious update can therefore propagate. Keep independent backups with a tested restore procedure and retention policy. Monitor replication lag in units meaningful to the workload—time, log position, or applied transactions—and alert when it exceeds the freshness budget. A healthy replica can still be too stale for a particular request.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why does a user sometimes not see their own change after saving?

  2. What does synchronous replication cost?

  3. What happens if a primary fails while a replica is 200ms behind?

Cheat sheet

Replication, Read Replicas and Lag

Set up a primary and a read replica, write to one and read from the other, and measure the lag window where a query returns stale data.

DATABASE · vizlearn.in/database/replication_and_lag.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.