Introduction
PostgreSQL replication lag is one of those problems that looks scary but is often misunderstood. In many production systems, everything runs smoothly for days or weeks, and then suddenly the replica starts falling behind. Read queries slow down, dashboards show red alerts, and teams worry about data consistency or failover safety.
In simple words, replication lag means the primary database is producing changes faster than the replica can process them. What confuses most teams is that the lag appears suddenly, even though the real cause has usually been building quietly in the background.
Think of PostgreSQL replication like a courier system. The primary database is the warehouse that sends packages, and the replica is the delivery center that receives and unpacks them. If packages start arriving faster than they can be unpacked, a backlog forms. Nothing is broken, but delays become visible.
This article explains why PostgreSQL replication lag spikes in production, using real-life examples, detailed human-style explanations, and clear advantages and disadvantages so readers understand what happens if these issues are ignored.
What Developers Usually See in Production
Teams commonly report situations like these:
Replication lag jumps from a few seconds to several minutes
Primary database looks healthy
Replica CPU or disk suddenly spikes
Read traffic on replicas slows down
Lag disappears on its own later
This creates the impression that replication lag is random or unpredictable.
Wrong Assumption vs Reality
Wrong assumption: Replication lag means the replica database is broken or unreliable.
Reality: In most cases, replication lag means the replica is temporarily overloaded.
Real-life example:
Imagine a restaurant kitchen during peak dinner hours. Orders keep coming in, but the number of cooks stays the same. Food starts taking longer to reach tables. The kitchen is not broken—it is just overloaded for that period.
The same thing happens with PostgreSQL replicas. They are still working correctly, but they cannot keep up with the workload at that moment.
How PostgreSQL Replication Works (Simple View)
PostgreSQL streaming replication works by sending WAL (Write-Ahead Log) records from the primary to replicas.
In short:
Primary writes changes to WAL
WAL is streamed to replicas
Replicas replay WAL to stay in sync
Replication lag increases when any step in this pipeline slows down.
Cause 1: Sudden Spike in Write Traffic
This is the most common and most misunderstood cause of replication lag.
What actually happens
When the primary database suddenly receives a lot of write operations—such as inserts, updates, or deletes—it generates a large amount of WAL data in a short time. The replica must receive and replay all of this WAL before it can catch up.
Real-life example
A company runs a nightly batch job to update prices for millions of products. The job finishes in 15 minutes on the primary database, but the replica takes 45 minutes to replay all the changes. During this window, replication lag keeps increasing.
Why it looks sudden
Lag does not jump instantly. It grows quietly during the write burst and becomes visible only after crossing alert thresholds.
Advantages of handling this properly
Replicas stay close to real-time
Read queries remain fast
Failover remains safe
Disadvantages if ignored
Replicas fall far behind
Reporting queries become outdated
Failover may cause data loss or inconsistencies
Join the conversation! Your thoughts help the community grow.