Sub topic 10
Replicating state: synchronous versus asynchronous
This chapter assumes you know what a primary and a replica are, and that you have seen how a failover moves traffic to a healthy node. Here we look at the data. A standby server is only useful if it holds the state the failed server held.
Why state is the hard part
Stateless servers are easy to replace, because any copy can serve any request. A database, queue, or file store is different. Its copies must agree, and copying data takes time. The central choice is when the primary tells the client "your write is saved": before or after the replica has the data.
Synchronous replication
With synchronous replication, the primary sends each write to the replica and waits for confirmation. Only then does it acknowledge the client.
- The replica has every acknowledged write, so a failover loses no acknowledged data.
- Each write pays the network round trip to the replica.
- If the replica is slow or unreachable, writes stall or fail, unless you configure the primary to continue without it.
Asynchronous replication
With asynchronous replication, the primary acknowledges the client right after it writes locally. It ships the change to the replica in the background.
- Writes are fast, and a slow replica does not block clients.
- The replica lags behind by some amount of time or some number of writes.
- If the primary dies, writes that were acknowledged but not yet shipped are lost.
A small simulation
This program models both modes. In asynchronous mode, two writes are acknowledged but never shipped before the primary "crashes".
class Replica:
def __init__(self):
self.log = []
class Primary:
def __init__(self, replica, synchronous):
self.log = []
self.pending = []
self.replica = replica
self.synchronous = synchronous
def write(self, value):
self.log.append(value)
if self.synchronous:
self.replica.log.append(value) # wait for the replica
else:
self.pending.append(value) # ship later
return "ack"
def ship(self):
self.replica.log.extend(self.pending)
self.pending.clear()
for synchronous in (True, False):
replica = Replica()
primary = Primary(replica, synchronous)
primary.write("a")
primary.ship() # background shipping runs once
primary.write("b")
primary.write("c")
# The primary crashes here. Promote the replica.
lost = [v for v in primary.log if v not in replica.log]
print("sync" if synchronous else "async", "lost:", lost)The output is sync lost: [] and async lost: ['b', 'c']. The client received an acknowledgment for b and c in both runs, but in the asynchronous run the new primary never saw them.
How the choice affects recovery
The amount of acknowledged data you can lose on failover is set by replication lag. Synchronous replication brings it close to zero. Asynchronous replication makes it equal to the lag at the moment of failure. The next chapter turns this into a formal recovery point objective.
Replication also affects recovery time. A replica that is current can be promoted at once. A lagging replica may need to replay its backlog first, or you may have to decide whether to accept the loss.
Middle options
Many systems offer something between the two extremes.
- Semi-synchronous: wait for one replica out of several, and replicate to the rest asynchronously.
- Quorum writes: acknowledge once a majority has the write. A later chapter covers quorums.
- Synchronous within a region, asynchronous across regions: you get low latency and zero loss for host and zone failures, and accept some loss in the rare case of losing a whole region.
Exercise
A primary acknowledges writes immediately and ships them to a replica every second. The primary crashes, and you promote the replica. What data can be lost, and how could you reduce that loss without waiting for every write on every replica?
Show answer
Up to about one second of acknowledged writes can be lost, plus any additional lag. To reduce the loss, use semi-synchronous replication, where the primary waits for at least one replica to confirm, or shorten the shipping interval. Semi-synchronous replication keeps a copy of every acknowledged write on another node, and the other replicas can still lag.