What it is
With synchronous replication the primary does not acknowledge a write until one or more replicas have confirmed they have it. "Saved" then means "saved on more than one machine". The set of replicas the primary waits for is the synchronous set; how many of them it waits for, and what exactly it waits for (received, flushed, applied), defines the guarantee.
The Database Replication lesson contrasts sync with async in one step. This lesson takes the synchronous side apart: where the latency comes from, why availability suffers, and how quorum commit repairs most of it.
Why it exists
Asynchronous replication acknowledges writes that exist on one machine only. If that machine is lost before the change is shipped, the write is gone even though the client was told it succeeded. Synchronous replication closes that window: a recovery point objective (RPO) of zero for the failures it covers. With apply-level waits it also makes replicas safe to read immediately after a write.
How it works
The commit path. The primary executes the write and writes its log locally, sends the change to the replicas in the synchronous set, waits for their confirmations, and only then reports success. In the lesson the replication packets visibly reach both replicas before the response leaves the primary.
Latency. A write takes the local commit plus the round trip to the slowest replica it waits for, plus that replica's own flush or apply time. Within a zone that is well under a millisecond of network; across zones in one region, typically one to a few milliseconds; across regions, tens to hundreds of milliseconds. A distant replica that serves no reads at all still adds its round trip to every write.
Availability. If the primary waits for every replica, the slowest replica sets the latency and any unavailable replica blocks commits. What "blocks" means depends on the database: PostgreSQL leaves the committing session waiting until a standby returns or the setting changes; MySQL semi-sync waits up to rpl_semi_sync_source_timeout (10 s by default) and then falls back to asynchronous; the simulation reports the write as failed.
Quorum and semi-sync commit. Wait for some replicas instead of all:
- PostgreSQL
synchronous_standby_names = 'ANY 2 (r1, r2, r3)'commits once any two of the three listed standbys confirm;FIRST 1 (r1, r2)waits for the highest-priority available standby. - MySQL semi-synchronous replication waits for
rpl_semi_sync_source_wait_for_replica_countreplicas (default 1) to receive the change. Withrpl_semi_sync_source_wait_point = AFTER_SYNC(the default) the transaction is not visible to other clients on the primary until a replica has received it. - Consensus systems (Raft, Paxos) commit when a majority of replicas has persisted the entry; that is a quorum commit with leader election built in.
The guarantee becomes "every acknowledged write is on the primary and at least k replicas". One slow or dead replica no longer sets latency or blocks writes. Replicas outside the quorum can be behind, so reads from them can be stale.
What is waited for. PostgreSQL's synchronous_commit shows the levels: remote_write (the standby received the WAL and handed it to its OS), on (the standby flushed it to disk), remote_apply (the standby replayed it, so queries there see it). Only remote_apply gives read-your-writes on standbys; on gives durability without visibility. MySQL semi-sync waits for the replica to write the event to its relay log (received), not to apply it.
When to use it
- Data where losing an acknowledged write is unacceptable (payments, orders, ledgers) and the cost of a few extra milliseconds per write is fine.
- With replicas in other zones of the same region, so the round trip is small.
- With a quorum or semi-sync setting, so one replica does not hold writes hostage.
- With
remote_apply(or equivalent) when replicas must serve reads that follow a write.
When not to
- When the only candidate replicas are in another region and write latency matters: use async there and accept a small RPO, or use a database designed for multi-region consensus.
- With a single synchronous replica and no fallback, unless you accept that its outage stops writes.
- For bulk or analytics pipelines where throughput matters more than per-write durability on a second node.
Trade-offs
| Setting | Durability | Write latency | Write availability | Replica reads |
|---|---|---|---|---|
| Async | Last writes can be lost on failover | Local only | Independent of replicas | Can be stale |
| Sync, wait for all | Every replica has every acknowledged write | Slowest replica | Any replica down blocks writes | Fresh only with apply-level waits |
| Quorum / semi-sync (k of n) | Primary + k replicas have it | k-th fastest replica | Tolerates n − k replicas down | Replicas outside the quorum can be stale |
| Semi-sync with timeout fallback | As above until the timeout, then async | k-th fastest, capped by the timeout | Writes continue after the timeout | As above |
Alternatives
- Asynchronous replication with good monitoring and a small, known RPO.
- Consensus-replicated databases (CockroachDB, Spanner, YugabyteDB, etcd) that commit on a majority and elect leaders automatically.
- Shared, replicated storage (Aurora writes to six storage copies across three zones and commits on four of them), which moves the quorum below the database engine.
- Application-level durability: write to a replicated log (Kafka with
acks=allandmin.insync.replicas) and build state from it.
Failure modes
- A synchronous replica dies and writes stall until it is removed from the set or a timeout falls back to async (silently weakening durability: alert on it).
- A slow replica (disk contention, a long replay, network loss) slows every write in wait-for-all mode.
- Fallback flapping: semi-sync switching between sync and async under load, so the actual guarantee changes minute to minute.
- Assuming fresh reads: reading from a synchronous standby right after a commit and getting old data, because the wait was for flush, not apply.
- Zone loss with co-located replicas: if the primary and its synchronous replicas share a zone, losing the zone loses the guarantee you paid for.
In production
Keep the synchronous set small and close: one or two replicas in other zones of the same region, chosen with ANY or FIRST so one can fail. Keep cross-region copies asynchronous. Monitor which replicas are currently synchronous (pg_stat_replication.sync_state, MySQL semi-sync status variables) and alert when the set shrinks or semi-sync falls back. Measure commit latency percentiles, not averages: a synchronous write's tail follows the slowest replica's tail. If reads go to standbys right after writes, use remote_apply or route those reads to the primary.
Common misconceptions
- "Synchronous means all replicas are always identical." Only for the replicas waited for, and only at the level waited for (flush is not apply).
- "A DR replica that nobody reads can't hurt." If it is synchronous, every write pays its distance and depends on its uptime.
- "Sync replication makes writes safe, so failover is trivial." It removes data loss for the covered failures; detection, promotion and fencing are still needed.
- "Semi-sync is just slower async." It guarantees another machine has the change before the client is told, which async never does, until a timeout fallback switches it off.
Interview angle
When a requirement says "no data loss", propose synchronous or quorum commit to replicas in other zones, and immediately state the costs: a few milliseconds per write, and the need for k-of-n so one replica cannot block writes. Distinguish durability from read freshness (flush vs apply). If the interviewer adds a second region, explain why synchronous cross-region commit adds tens of milliseconds to every write and suggest async DR or a consensus database built for it. Numbers help: "commit ≈ 2 ms local + 2 ms to the nearest of two other-zone replicas".
Model assumptions
The simulation is a simplified model, not a benchmark. Replicas have 12 connections and the primary 40, with a 30 ms mean read and a 15 ms mean write (lognormal, cv 0.4). The app tier sends 400 queries/s, 30% writes, over 300 keys (Zipf 1.0); a proxy sends writes to the primary and reads to Replicas 1 and 2 by least connections. In synchronous mode the primary ships each write to every connected replica and answers once the required replicas have applied it: all of them at first, any two of three in the quorum step (k of n), and in the recap Replicas 1 and 2 only, with Replica 3 receiving every write asynchronously. A synchronous write takes: its local write, the replication hop to the replica (2 ms in Region A, 70 ms to Replica 3), the replica's apply time (by default its own mean write time, 15 ms; a record shipped asynchronously costs no apply time in this model, so asynchronous lag is measured to arrival), and the acknowledgement's hop back. A k-of-n commit is answered when the k-th acknowledgement reaches the primary. While it waits, the write keeps its connection on the primary, as a PostgreSQL backend does while waiting for its synchronous standby; with too small a pool the writes queue and time out (at 120 writes/s and a 70 ms replica about 20 connections are busy, which is why the primary here has 40). Failures are not delayed: a replica that is down or no longer replicating counts as a missing acknowledgement immediately, without a timeout. A write whose required replicas can no longer all acknowledge fails (real PostgreSQL would block instead, MySQL semi-sync would fall back to async after a timeout). A replica that was down misses the writes of its outage. The slow-motion packets in the quorum step and the whole flush-vs-apply step are scripted illustrations; the engine does not distinguish received, flushed and applied. Numbers show behaviour and orders of magnitude only.
How to read this demonstration.
- Actual engine simulation: the write path of every simulated step and of the sandbox: local write, replication hop, the replica's apply time, the acknowledgement's hop back, the connection the write holds on the primary, k-of-n acknowledgement counting, a replica going down, asynchronous replication to a replica outside the synchronous set.
- Scripted animation: the whole flush-vs-apply step (traffic paused); the engine does not distinguish received, flushed and applied, it always waits for apply.
- Hybrid demonstration: the quorum step: its latency and error figures come from the engine, while the slow-motion packets show one commit in a scripted sequence.
- Simplified model: fixed link delays, one apply time per replica, lognormal write times; a write that cannot get its acknowledgements fails (at once when the replica is known to be down, otherwise after the wait bound), where PostgreSQL would keep waiting and MySQL semi-sync would fall back to asynchronous.
- Time compression: the lesson plays in slow motion (about 0.1 to 0.15 times real time) so the packets can be followed; the tiles use simulated seconds, not wall-clock seconds.
- Not supported by the simulation: network partitions (a failed replica is a node failure; the lesson never cuts a link), consensus or leader election, WAL shipping, group commit, and the configurable PostgreSQL
synchronous_commitlevels.