← Lessons

quiz vs the machine

Platinum1800

Databases

Read Replica Lag Handling

Replicas offload reads but apply changes slightly behind the primary, so a fresh write may not appear on a replica yet.

5 min read · advanced · beat Platinum to climb

Why Lag Exists

A read replica receives the primary's write stream and applies it to its own copy. Shipping and replaying that stream takes time, so the replica is always slightly behind. Replication lag is the gap between what the primary has committed and what the replica has applied.

The Symptom

A user writes a value, then immediately reads it from a replica and sees the old value or nothing. This read your own writes violation confuses users and corrupts logic that assumes a write is instantly visible everywhere.

Handling Strategies

  • Read from the primary for queries that must see the latest write, such as a confirmation page right after a save.
  • Use monotonic or causal reads by pinning a session to a replica that is at least as fresh as the user's last write position.
  • Set a freshness bound and route only reads that tolerate staleness to replicas.

Operating Concerns

  • Monitor lag continuously and stop routing to a replica that falls too far behind.
  • A long running query or heavy write burst on the primary can suddenly widen lag.

Key idea

Replicas lag behind the primary, so route reads that must see the latest write to the primary or to a sufficiently fresh replica, and monitor lag to stop using stale ones.

Check yourself

Answer to earn rating on the learn ladder.

1. What causes replication lag?

2. How can you guarantee a user reads their own recent write?