The Runtime Theory

Replication Catch-Up: How a Standby Catches the Primary

Watch the replication stream — WAL records from the primary, shipped over the network, applied on the standby — close the lag gap until the replica is current.

The Runtime Theory Team08 stages

trace / request.md

PRIMARY COMMITSA TRANSACTIONLOG RECORDSDESCRIBE THE CHANGEWAL SENDERSTREAMS RECORDSRECORDS CROSSTHE NETWORKSTANDBY WRITESITS OWN WALRECORDS AREREPLAYED INTO PAGESBACKLOG DRAINSLAG HITS ZERO

readyThe primary commits a transaction. As always, the commit record goes to the WAL and is fsynced — the same log stream that powers crash recovery also feeds replication.

Replication is crash recovery running live, in the other direction. The same WAL records that let a database rebuild its pages after a crash are shipped over the network and applied on a standby while the primary keeps serving. This diagram walks the full stream: commit on the primary, records read by the WAL sender, transported over a persistent connection, written by the standby's receiver, and replayed into its pages.

Lag is the number this whole pipeline is about. It grows when the network is slow, when the primary writes faster than the link can carry, or when a blip interrupts the stream; it shrinks when the standby drains the backlog. The stages are ordered by dependence — the receiver can only write what the sender sent, replay can only apply what the receiver wrote — and the ordering is exactly what makes the stream safe: applying records in primary order reproduces the primary's state exactly, which is what makes failover possible at all.