Synchronous replication
The sync-replicas and sync-timeout-ms settings, what an acknowledgement means with them, the timeout fallback and its counter, strict mode under a watcher, and the measured cost.
Syntax#
raftdb-server --sync-replicas N [--sync-timeout-ms MS]# raftdb.conf
sync-replicas = 1
sync-timeout-ms = 1000Options#
| Setting | Meaning | Default |
|---|---|---|
sync-replicas | How many replicas must confirm a write before it is acknowledged. 0 is asynchronous. | 0 |
sync-timeout-ms | How long a write waits for them. | 1000 |
How it works (D39)#
- A replica sends
Ack{offset}on itsFollowconnection after each chunk it has applied and committed to its own disk. - The primary's per-shard flusher, after its own
fsync, holds the replies of a commit until N connected followers have confirmed a log position at or beyond that commit's position. - Writes by several clients share one wait (group commit).
"Confirmed" therefore means applied and durable on the replica's disk. A client whose write returned can read it from any confirming replica, and a primary failure cannot lose it. (Tested: after each of 120 synchronous writes the replica already holds the row.)
The timeout fallback#
If the confirmations do not arrive within sync-timeout-ms, the write is acknowledged anyway and counted as a degraded commit: GET /v1/server shows sync.degradedCommits (with sync.replicas and sync.timeoutMs). The reason: refusing or blocking every write when a replica is down turns one failure into an outage. A dead replica costs each write up to the timeout, it does not stop writes. Operators who prefer blocking can set a very long timeout. The watcher's server check reports degraded commits as a warning.
While a write is degraded it is not protected against a primary failure. Do not treat sync-replicas = 1 as a guarantee unless degradedCommits stays zero, or unless the node is managed (next section).
Strict mode in a managed cluster#
A node started with control = true runs with strict synchronous replication (D42, D51): an unconfirmed write is an error instead of a degraded acknowledgement. sync-replicas (and the watchers' sync_replicas, which must match) say how many followers must confirm. This is what makes the failover argument hold: an acknowledged write is on the primary and on at least sync-replicas followers, so with members - sync-replicas or more nodes answering, at least one answering node holds it. See Failover with a watcher quorum.
Sending to the followers while the primary syncs (ship-early)#
By default the primary writes a batch, syncs it, and only then sends it to the followers, so a synchronous write costs the primary's sync, the network and the follower's sync one after the other. With ship-early = true the log reports the bytes it has written before its sync, the replication stream sends them at once, and the follower writes and syncs while the primary syncs.
- The acknowledgement rule is unchanged. A client is answered only after the primary's own sync and the confirmations of
sync-replicasfollowers. Anysync-replicasof the connected followers count, so a slow or dead one does not hold a write back, and each follower has its own stream, so the sending to all of them is already at the same moment. - Why it is safe. A follower may hold bytes the primary never made durable. If the primary dies before its sync, no client was told those bytes were stored, so keeping or dropping them is allowed. The election ranks nodes by data epoch and log position, so the follower with more data may become primary, and the old primary then fetches from it again (its log is shorter, which the normal resynchronisation handles). A follower can never claim a position beyond what was sent to it.
- Not for Windows logs. A Windows log writes through, so there is no gap between writing and syncing to use; the setting has no effect there.
- Off by default. It is a speed setting, not a durability setting; turn it on after testing with your own disks and network.
Measured cost#
Development PC, loopback, one replica, 400 sequential durable writes (latency_of_synchronous_replication, an ignored test): p50 143 to 304 microseconds, p99 962 to 1,401 microseconds. A real network adds its round trip and the replica's fsync.
Remarks#
- Tested: with no replica the write returns after the timeout and is counted; with a replica attached 20 writes are fast and not degraded; after the replica leaves, writes degrade again instead of failing.
- Not done: a quorum over a configured replica set (the count is "N connected followers"), automatic reporting of degrade and recover beyond the counter, per-table settings.
Examples#
raftdb-server --profile primary --sync-replicas 1 --sync-timeout-ms 2000Check it:
curl -s http://127.0.0.1:7480/v1/server -H "Authorization: Bearer TOKEN"and read the sync object.