Course contents 04 / 35

04 / 35 · Beyond One Machine

Replication and failover

Trace one acknowledged order update from the leader to its replicas and decide what can survive promotion.

The sales service marks order 106 as paid. Its node confirms the update and then stops responding. A second node can serve the same order only if it has a usable copy. Whether that copy contains the payment depends on what the first confirmation meant.

Copy the updates in order#

Replication keeps several copies of the same logical part. Suppose A is its leader and B is a follower. A accepts updates and records their order in a log. B receives log entries and applies them in that order. A new follower can start with a snapshot and then apply entries after the position captured by the snapshot. This is different from placement of distinct records: adding a follower increases the number of copies of each record in the part.

In PostgreSQL physical streaming replication, the primary streams write-ahead log records to standbys. A standby can be ahead in received log bytes compared with bytes it has applied. A read served by a standby observes its replayed state, so both the log position and the stage measured matter when reporting lag. The terms vary by system. Kafka replicates an ordered log per partition with a leader and followers. (PostgreSQL 18, §§26.2.5 and 26.2.8, Kafka 4.0, Replication)

Say what the acknowledgement covers#

In a toy execution, A makes an update durable at 5 ms. If A stays up, the follower’s durable-copy confirmation reaches A at 35 ms, and B finishes applying the update at 45 ms. For the calculation, ignore extra time to send the final response to the client.

Figure

Durable copy and apply happen at different times

Durable copy and apply happen at different timesWithout failure, confirmation of a durable B copy arrives at 35 ms. The new state becomes visible on B at 45 ms.5 msDurable on A35 msDurable B copy confirmed45 msApplied on B
Without failure, confirmation of a durable B copy arrives at 35 ms. The new state becomes visible on B at 45 ms.

With asynchronous replication, A can acknowledge at 5 ms, leaving 30 ms before confirmation of the second durable copy in this execution. Suppose A is permanently lost at 20 ms and B has not received the entry. The acknowledged update is absent if B takes over. An available A could have sent it later. The confirmation covered durability on A under the stated policy.

With a synchronous policy that waits for a durable B copy, the update is acknowledged at 35 ms in the no-failure run. If A stops at 20 ms, this policy has not acknowledged success. That does not tell the client that the update was never recorded. It could have been durable on A or have reached some replicas. A retry still needs the outcome or an operation identity.

At 40 ms, B can have the durable entry while a fresh read on B still shows the earlier state. To guarantee visibility on B after the acknowledgement, the policy must wait for the corresponding apply step or route reads with a replay-position condition. PostgreSQL distinguishes waiting for remote durable flush from waiting for apply with remote_apply. The exact acknowledgement condition belongs to the protocol and configuration. Kafka’s acks=all waits for the current in-sync replica set, with a configurable minimum. Assigned replicas that have fallen out of that set do not take part in that acknowledgement. (PostgreSQL 18, §26.2.8, Kafka 4.0, Replication)

Promote with an authority rule#

Failover changes which replica is authorised to serve a role. The node that looks quiet can still be running on the other side of a network partition. If both A and B accept independent writes as leader, they can produce incompatible histories for one order. An authority mechanism can assign a new term and reject writes under an older one, or isolate the old leader from the storage and clients. The exact mechanism must be part of the write path.

It is also necessary to choose a candidate that retains committed entries. Raft combines majority voting with rules about a candidate’s log. Its leader-completeness property ensures that future leaders contain entries committed by the protocol. Selecting a reachable follower solely because it responds quickly offers no such property. (Ongaro and Ousterhout, §§5.2–5.4)

For 600 GB of logical data, three full copies require 3⋅600=18003\cdot600=1800 GB of payload before log and metadata overhead. Copies placed in the same failure domain can all disappear in one incident. Waiting for more copies can also stall writes if too few replicas are reachable. A failover test therefore has to check both the recovered data and the time until a client can safely resume.

Next#

The follower may be durable and still show an earlier state. Consistency between nodes turns that observation into a contract for clients and asks what the cluster can promise while replicas cannot communicate.

References#

  1. PostgreSQL Global Development Group. Log-Shipping Standby Servers. PostgreSQL 18, §§26.2.5 and 26.2.8. Streaming, synchronous acknowledgement and replay visibility.
  2. Apache Kafka. Design. Kafka 4.0, Replication. Per-partition logs, leader selection and in-sync replicas.
  3. Diego Ongaro and John Ousterhout. In Search of an Understandable Consensus Algorithm. USENIX ATC, 2014, extended version, §§5.2–5.4. Terms, voting, replicated logs and leader completeness.