Course contents

05. Distributed Databases Lesson 3 of 10

Synchronous Replication

Watch writes wait for the slowest replica, a dead replica stop all writes, and quorum commit fix it, plus what "synchronous" really waits for.

15 minAdvanced6 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A synchronous primary acknowledges a write only after the replicas in its synchronous set confirm it.
  • Write latency becomes the local commit plus the round trip to the slowest replica waited for, even one that serves no reads.
  • If the primary waits for every replica, one dead replica stalls or fails every write.
  • Quorum (any k of n) or semi-sync commit keeps acknowledged writes on another machine while letting a slow or dead replica drop out.
  • "Synchronous" may mean received, flushed or applied; only applied makes reads on that replica fresh.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

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_count replicas (default 1) to receive the change. With rpl_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=all and min.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_commit levels.
The lesson as textEvery step of the simulation, written out.
  1. 1. A write waits for its replicas

    The primary replicates synchronously to Replicas 1 and 2 in the same region. For every write it runs the query, ships the change, and answers the client only when both replicas have applied it.

    Watch a write’s timeline: the purple replication packets reach the replicas *before* the green response leaves the primary. Because a client only hears “saved” once every replica has the change, a replica read can never miss an acknowledged write: the stale-read counter stays at zero. In-region the wait costs about 20 ms: the hop there, the replica’s apply, and the acknowledgement back.

    Scene: The app sends 400 queries per second, 30% writes. The primary replicates each write to two nearby replicas and answers only after both have applied it. No reads are stale.

    Key idea: Synchronous replication moves the replica’s work into the write path: the acknowledgement means “the replicas have it too”.

  2. 2. A replica in another region

    For disaster recovery we add Replica 3 in Region B, 70 ms away, also synchronous. Nobody reads from it; it only receives changes.

    Every write now waits for the slowest replica. The median write takes about 135 ms longer (the acknowledgement has to come back, so you pay the distance twice), and P95 (with 30% writes, the slowest 5% are all writes) rises from about 60 ms to about 190 ms. The primary also keeps each committing write’s connection for that whole time: at 120 writes/s that is roughly 20 busy connections instead of 2, which is why this lesson’s primary has a bigger pool than the replicas. Reads are exactly as fast as before. A replica that serves no traffic at all still sets the latency of every write.

    Scene: A third replica in another region, 70 ms away, joins the synchronous set. Every write now takes about 135 ms longer (a round trip) and P95 rises; read latency is unchanged.

    Key idea: Sync write latency = local commit + the round trip to the slowest replica in the synchronous set + its apply time; the connection stays busy meanwhile.

  3. 3. The remote replica goes down

    One second in, Replica 3 fails. The primary keeps waiting for its acknowledgement and never gets one. In the simulation every write fails; a real PostgreSQL primary would instead hang each commit until the standby returns or the configuration changes, which looks the same to users, only slower.

    Reads are untouched, and the proxy’s health checks do not help: Replica 3 is not a read backend, so nothing ejects it. About 30% of all requests, every write, fail until someone changes the synchronous set.

    Scene: Replica 3 fails one second in. From then on every write fails because the primary waits for its acknowledgement; reads keep succeeding.

    Key idea: With a fixed synchronous set, write availability is the availability of the least available replica, even one that serves no reads.

  4. 4. Quorum commit: wait for 2 of 3

    Instead of “all replicas”, wait for any 2 of the 3. In PostgreSQL that is synchronous_standby_names = 'ANY 2 (r1, r2, r3)'; MySQL’s semi-synchronous replication waits for a configurable number of replicas (rpl_semi_sync_source_wait_for_replica_count) and, after a timeout, falls back to asynchronous.

    The primary still ships every write to all three replicas and answers as soon as any two have applied it. With Replica 3 down, Replicas 1 and 2 form the quorum: writes succeed again, at the in-region latency. At 3 s Replica 3 comes back and receives writes again, and nothing changes for latency: the two fastest acknowledgements still come from the nearby replicas, 70 ms before Replica 3’s. The slowed-down packets show one commit, including the acks’ way back.

    The guarantee is weaker but still useful: every acknowledged write is on the primary and at least two replicas. A replica outside the quorum can be behind (Replica 3 also missed every write while it was down), so reads from it could be stale.

    Scene: The primary now commits once any two of its three replicas acknowledge. With Replica 3 down, the two nearby replicas form the quorum: writes succeed again at in-region latency. Replica 3 returns at three seconds and receives writes again without slowing them. Slow-motion packets show one commit answered before Replica 3 responds.

    Key idea: Quorum or semi-sync commit keeps “acknowledged = on another machine” while letting one slow or dead replica drop out of the write path.

  5. 5. Durable on the replica is not visible on it

    This step is a scripted illustration; simulated traffic is paused. What exactly does the primary wait for? Most synchronous setups wait until the replica has received and flushed the change to disk, not until it has replayed it into its tables.

    Follow the packets: the write reaches Replica 1, which flushes it and acks; the client hears “saved”; it immediately reads from Replica 1, and gets the old value, because replay has not caught up. The write is durable (it survives losing the primary) but not yet visible there.

    PostgreSQL makes the level explicit: synchronous_commit = remote_write (received), on (flushed), remote_apply (replayed and visible to queries on the standby). Only remote_apply gives read-your-writes on synchronous standbys. The simulation, like remote_apply, waits for the apply.

    Scene: Scripted, traffic paused: a write is flushed on Replica 1, which acknowledges; the client is told it is saved and immediately reads from Replica 1, which returns the old value because it has not replayed the change yet.

    Key idea: Ask what “synchronous” waits for: received, flushed or applied. Only applied makes replica reads fresh.

  6. 6. Choosing what to wait for

    Traffic is back, and the synchronous set is now explicit: writes commit on Replicas 1 and 2, and Replica 3, the disaster-recovery copy in Region B, receives every change asynchronously, about 70 ms behind (the lag tile), without ever being waited for.

    Synchronous replication buys one thing: an acknowledged write is on more than one machine (and, with apply-level waits, visible on replicas). It costs write latency set by the slowest replica you wait for, and write availability tied to those replicas. Quorums and semi-sync limit both costs; put the synchronous replicas close (another zone, not another region) and keep remote copies asynchronous.

    Scene: Traffic resumes at 400 queries per second. Writes commit synchronously on Replicas 1 and 2; Replica 3 receives every change asynchronously, about 70 ms behind. No errors and no stale reads.

    Key idea: Wait for as few, as close, replicas as your durability goal needs; never for all of them.