In this lesson · topic 6 of 7
Topic 6
How a follower falls hours behind
6 min read
In the lab, lag came from network delay and stayed small. In production the common cause is different: the follower cannot keep up with the leader at all.
Backlog arithmetic
A leader serves many connections at once, so it can commit writes in parallel. A follower must apply them in the leader’s order. Many databases historically applied the log with a single thread, and even parallel appliers are limited by how many transactions touch independent rows.
Say the leader handles 1,500 writes per second at the evening peak, and the follower can apply 1,000 per second. The backlog grows by 500 entries every second. After a 10-minute peak:
- backlog: 500 x 600 = 300,000 entries
- delay for the newest write to appear: 300,000 / 1,000 = 300 seconds
Now the peak ends and the leader drops to 600 writes per second. The follower applies 1,000 and takes in 600 new ones, so it gains only 400 per second on the backlog:
- catch-up time: 300,000 / 400 = 750 seconds, about 12.5 minutes
Predict first
The shape matters more than the numbers. Catch-up needs spare capacity, and a follower sized to run at 90 percent of its apply rate during normal hours recovers very slowly from any burst.
Queries that fight the log
Followers serve reads, and reads can hold things the log needs to change. In PostgreSQL’s hot standby mode, replaying a log entry that removes row versions a long-running query still needs creates a conflict. The follower must either pause replay or cancel the query. Its max_standby_streaming_delay setting decides how long replay may wait, and the default is 30 seconds. A reporting query that runs for ten minutes is either killed or leaves the follower ten minutes behind, depending on the setting.
The usual answer is to separate the jobs. Point analytics at a dedicated follower that nobody expects to be fresh, and keep the followers that serve user traffic for short queries.
Measuring it honestly
Lag has two units, and each hides something. Entries or bytes behind tells you how big the backlog is, but not how long it matters. Seconds behind tells you the user-visible effect, but only if the leader is writing. An idle leader writes nothing, so a follower may report zero lag because there is nothing new to be behind on, or report a growing number because the last timestamp it saw is old.
A practical fix is a heartbeat. A small job on the leader updates one row with the current time every second. The lag then reads off the difference between that timestamp and the follower’s clock, and it keeps moving when real traffic stops.
What the router does with it
Earlier you saw a read-your-writes policy send reads to the leader when a follower was behind. Real routers apply the same idea with a threshold: take a follower out of the read pool when it is more than a few seconds behind, and put it back once it is within range. Leave a gap between those two thresholds, so a follower hovering at the line does not flap in and out of the pool.
Sources
- The PostgreSQL project, Hot standby, on query conflicts during recovery, and Replication settings for
max_standby_streaming_delay. - Martin Kleppmann, Designing Data-Intensive Applications, O’Reilly, 2017. Chapter 5 covers replication lag and its consequences.