Video in progress

What is the difference between synchronous and asynchronous replication, and which one can lose data?

With synchronous replication, the primary says "committed" only after a replica confirms the write — nothing is lost, but writes are slower. With asynchronous, the primary answers right away and the replica catches up later — if the primary dies, the last few transactions can be lost.

📖 Answer

Asynchronous:

client → primary: COMMIT → "OK" (immediately)
primary → replica: WAL is shipped later

It's fast, and if the replica slows down or dies, the primary isn't affected. But transactions the primary has answered "OK" for and that haven't reached the replica can be lost along with the primary's disk. After a failover, the user saw "payment went through", but it's not in the database.

Synchronous:

client → primary: COMMIT → waits for the replica to confirm → "OK"

Nothing is lost, but every commit gets a network round trip added. Worst of all, if the replica goes down, the primary can't write either.

On prod people often pick the middle path. In PostgreSQL:

-- confirmation from any one of the two replicas is enough
ALTER SYSTEM SET synchronous_standby_names = 'ANY 1 (replica_a, replica_b)';

If one replica goes down, the other still confirms, and writes don't stop.

Another subtle point: in PostgreSQL, synchronous_commit = on waits for the WAL to be written to the replica's disk, but not for it to become visible to reads on the replica. If you want it to be visible on the replica right away, you need remote_apply.

The business decides which one you need: synchronous for money and orders, asynchronous is enough for logs and analytics.