Database.Middle.What is replication lag ?

Replication lag is the delay between when a change occurs on the primary database and when that same change is visible on a replica.


🔍 What Does That Mean?

Imagine this sequence:

  1. A user updates their profile in the primary database.
  2. The update is logged (e.g., in WAL or binary logs).
  3. The read replica eventually receives and applies the change.

If the replica takes, say, 5 seconds to apply the update, then:

  • For those 5 seconds, queries on the replica show stale data.
  • This delay is the replication lag.

🧠 Why Does Replication Lag Happen?

CauseDescription
Network latencyDelay in transmitting logs from primary to replica
IO bottlenecksReplica is slow to write/apply changes
Large transactionsBig or frequent writes take time to replicate
Resource contentionCPU, disk, or memory pressure on the replica
Asynchronous replicationBy design, the replica doesn’t block the primary, but may fall behind

📏 How to Measure It

  • PostgreSQL: Compare pg_current_wal_lsn() on the primary with pg_last_wal_replay_lsn() on the replica
  • MySQL: Check Seconds_Behind_Master in SHOW SLAVE STATUS
  • MongoDB: Use rs.printSlaveReplicationInfo()

⚠️ Why It Matters

RiskExplanation
Stale readsReplica shows outdated data — confusing to users or apps
Failover issuesIf you promote a lagging replica, it may miss recent changes
Data integrityApplications relying on up-to-date reads may break

✅ How to Reduce Replication Lag

  1. Optimize write performance on the primary
  2. Use faster disk and more memory on replicas
  3. Compress replication logs
  4. Monitor and alert when lag exceeds threshold
  5. Use synchronous replication if strong consistency is needed (but slower)

🔄 Summary

TermMeaning
Replication LagTime delay between primary update and replica sync
Caused ByNetwork, IO, large writes, async delay
ResultStale reads, delayed failovers
SolutionsOptimize infra, monitor lag, use sync replication
This entry was posted in Без рубрики. Bookmark the permalink.