Skip to content

Synchronous and asynchronous replication

A client sends a write to the primary. At some point the primary answers “done”. The question of replication is what has to be true before it says so.

  • Asynchronous. The primary acknowledges as soon as it has the write, and ships it to the replicas afterwards. The client never waits for a replica.
  • Synchronous. The primary acknowledges only once the replicas have the write. The client waits for the slowest of them.
  • Semi-synchronous. A middle way, as the lab defines it: the primary waits for one replica, not all.

The PostgreSQL documentation describes the two ends. Asynchronous is the default: changes are shipped after the transaction commits, so there is a small delay before they are visible on a standby, and if the primary fails before a change has been shipped, that change is lost. With synchronous replication each commit waits for confirmation that the change has been written on the standby as well, the minimum wait is the round-trip time between the primary and the standby, and data can only be lost if both crash at the same time. The documentation also says that this raises response time.

Mental model. Asynchronous is posting a letter and telling the customer “sent”. Synchronous is waiting at the post office until the other end has signed for it. The first is quick and you cannot be sure. The second is slow and you can.

The lab is the one from the previous lesson, with the same ten writes, one every 100 ms, and replica lags of 100 ms for Node 2 and 300 ms for Node 3. This time change When a write is acknowledged, and read Time to acknowledge a write in the figures.

A primary accepts 10 writes, one every 100 ms, and ships each one to two replicas. Change how late each replica receives them, and when the primary acknowledges a write.

100 ms
300 ms
When a write is acknowledged

The primary acknowledges a write at once, then ships it to the replicas.

1,300 ms
Node 1PrimaryAccepting writesepoch 1Node 2ReplicaIn syncepoch 1Node 3ReplicaIn syncepoch 1
Node 1PrimaryAccepting writesepoch 1Node 2ReplicaIn syncepoch 1Node 3ReplicaIn syncepoch 1
  • A write on its way to a replica
  • A write sent to bring a node level
  • A write that never arrives
  • A link cut by a network partition
  • A write the node has just accepted
  • A write acknowledged to its client
  • A write the node refused
  • A read, with the number of the write it returned

Each square is a write, filled once the node has applied it.

Every replica has applied every write.

What the run came to

Writes acknowledged
10 of 10
Time to acknowledge a write
At once
Most writes a replica was behind
Node 2: 1; Node 3: 3
The nodes at this moment
NodeRoleStateWrites applied
Node 1PrimaryAccepting writes1, 2, 3, 4, 5, 6, 7, 8, 9 and 10
Node 2ReplicaIn sync1, 2, 3, 4, 5, 6, 7, 8, 9 and 10
Node 3ReplicaIn sync1, 2, 3, 4, 5, 6, 7, 8, 9 and 10
Event log: every event of the run, in order
Every event of the run in time order. Events after the current moment are dimmed.
Time in the runEvent
100 msWrite 1 (the application) is accepted by Node 1.
100 msWrite 1 (the application) is acknowledged at once.
200 msNode 2 applies write 1.
200 msWrite 2 (the application) is accepted by Node 1.
200 msWrite 2 (the application) is acknowledged at once.
300 msNode 2 applies write 2.
300 msWrite 3 (the application) is accepted by Node 1.
300 msWrite 3 (the application) is acknowledged at once.
400 msNode 3 applies write 1.
400 msNode 2 applies write 3.
400 msWrite 4 (the application) is accepted by Node 1.
400 msWrite 4 (the application) is acknowledged at once.
500 msNode 3 applies write 2.
500 msNode 2 applies write 4.
500 msWrite 5 (the application) is accepted by Node 1.
500 msWrite 5 (the application) is acknowledged at once.
600 msNode 3 applies write 3.
600 msNode 2 applies write 5.
600 msWrite 6 (the application) is accepted by Node 1.
600 msWrite 6 (the application) is acknowledged at once.
700 msNode 3 applies write 4.
700 msNode 2 applies write 6.
700 msWrite 7 (the application) is accepted by Node 1.
700 msWrite 7 (the application) is acknowledged at once.
800 msNode 3 applies write 5.
800 msNode 2 applies write 7.
800 msWrite 8 (the application) is accepted by Node 1.
800 msWrite 8 (the application) is acknowledged at once.
900 msNode 3 applies write 6.
900 msNode 2 applies write 8.
900 msWrite 9 (the application) is accepted by Node 1.
900 msWrite 9 (the application) is acknowledged at once.
1,000 msNode 3 applies write 7.
1,000 msNode 2 applies write 9.
1,000 msWrite 10 (the application) is accepted by Node 1.
1,000 msWrite 10 (the application) is acknowledged at once.
1,100 msNode 3 applies write 8.
1,100 msNode 2 applies write 10.
1,200 msNode 3 applies write 9.
1,300 msNode 3 applies write 10.

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • There is one record. Each write gets the next number, in the order clients send them, and a node’s version is the number of the last write it has applied.
  • A replica applies a write a fixed time after the primary accepts it: the lag you set. Confirmations travel back at no cost, so an acknowledgement waits only for replicas.
  • A primary acknowledges a write once the number of replicas the mode names have applied it. If it cannot reach enough of them it keeps waiting, because it cannot tell slow from gone.

Simplifications

  • Three nodes, one record and fixed lags. A real replication stream has delays that vary, batches, backlogs and many records.

Parameter values are illustrative. They are not benchmarks of any real product.

Mode The primary waits for A write is acknowledged after
Asynchronous No replica 0 ms, at once
Semi-synchronous One replica, the first to confirm 100 ms
Synchronous Both replicas 300 ms

Each wait is the lag of the replicas the primary must hear from. Semi-synchronous waits for Node 2, which is the quicker, and synchronous waits for Node 3.

Two things do not change. First, the replicas are no faster: Node 2 is still at most 1 write behind and Node 3 at most 3, whatever the mode. The mode decides when the client is told, not when the replica applies. Second, the cost grows with the slowest replica you wait for. Drag Replication lag to Node 3 to 1,000 ms and choose synchronous: a write now waits 1,000 ms for its acknowledgement.

What you buy with the wait is shown in the failover lesson. In short, a write that the replicas already hold cannot be lost with the primary.

What happens if the replica you wait for is gone?

Section titled “What happens if the replica you wait for is gone?”

A primary that waits for a replica cannot tell a slow replica from a dead one. In the lab’s model it keeps waiting, and the write is never acknowledged. The PostgreSQL documentation says the same in its own terms: such commits may never complete if a synchronous standby crashes, and it advises keeping several potential synchronous standbys so that one can fail without stopping the writes.

So synchronous replication trades one risk for another. It stops acknowledged writes being lost, and it makes the primary’s ability to take writes depend on a second machine. With two replicas and a rule that waits for both, either replica failing stops the writes. Waiting for one of the two (semi-synchronous) means a single failure can be tolerated.

Mode What it protects What it costs How it fails
Asynchronous Write latency: no waiting A write acknowledged to the client can be lost if the primary dies before it is shipped Lost writes on failover
Semi-synchronous The write is on at least two nodes when acknowledged A wait for the quicker replica Both replicas lost: the writes wait. One lost: the others still confirm
Synchronous Every replica has the write when it is acknowledged A wait for the slowest replica, on every write One slow or dead replica slows or stops every write
  • Asynchronous acknowledges at once and can lose a write that was not yet shipped. Synchronous waits for the replicas and cannot.
  • In the lab, a write is acknowledged after 0 ms asynchronously, 100 ms semi-synchronously and 300 ms synchronously, which is the lag of the replicas it waits for.
  • The mode changes when the client is told. It does not make a replica apply any faster.
  • A primary that waits for a replica cannot tell slow from gone, so it can stop acknowledging writes when that replica fails.

Next: Replication lag and stale reads.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.