Failover with a watcher quorum
How three or more watchers elect a leader, decide who is primary, renew leases and fail over, every cluster setting with its default, the measured failover times and the resource-control feature.
In Raft DB the election, the failover and the resource decisions are made by the watchers, not by the database (decision D51). A watcher is the agent of its own node and a member of a Raft group of watchers (raftdb-raft). The server keeps only the fence.
How it works#
- Run one watcher per database node, three or more in all, on separate machines. A single watcher is for a single node and cannot fail over. The watchers need each other's peer ports reachable; there is no TLS between them, so use a private network or a tunnel.
- The watchers elect their own leader by Raft. The Raft log holds only decrees:
Assign(node)andRevoke, each with a group number. The epoch of a decree is its log index, so epochs only grow. - The leader decides; the others report. Each watcher reads its node's state (
GET /v1/control/state) and sends it to the leader. The leader leaves a primary alone while its node is reported writable. - Lease renewal rule. An agent renews its node's lease only while its watcher is in contact with the quorum within one contact window (one election timeout): a leader needs answers from a majority; a follower needs an
Appendfrom the leader after which its committed log reached the commit index of thatAppend. So the fence is the quorum: a node cut off from the quorum stops being writable when its lease runs out, with no help from anyone. - Failure of a primary. If the primary's node is not writable for
control_fail_after_ms, the leader decrees a revoke, waitscontact window + lease + margin, then assigns the node with the most advanced data. It assigns only if at leastmembers - sync_replicasnodes answered. - Ranking data. Nodes are ranked by (data epoch, log position), dirty nodes included. A dirty old primary therefore ranks below any node that follows a newer primary, and wins only when it holds the most. A node with less data never becomes primary.
- The safety argument. An acknowledged write is on the old primary and on at least
sync_replicasfollowers (strict sync). If at leastmembers - sync_replicasnodes answered, at least one answering node holds it, and the node with the highest (data epoch, position) holds a superset of what every answering node holds. - A newly assigned primary gets a grace period before it can be called unwell (promoting it stops its replication threads, which takes a second or two).
Failover time#
About control_fail_after + report age + (contact window + lease + margin) + promotion, which the decision log puts at about 12 s with the default 3 s lease. Measured:
| Test | Lease | Result |
|---|---|---|
| Three servers and three watchers in one process, primary killed under write load | 1.5 s | 5.6, 7.7 and 7.7 s in three runs; no acknowledged write lost |
| Same, the primary's watcher cut off from the quorum | 1.5 s | Its node stopped writing 2.0 to 2.3 s after the cut; never a second writable node; nothing lost |
Real processes on a 2-core Linux host, kill -9 of the primary's server under load | 3 s | Another node accepted writes 8.5 s later |
Same, kill -9 of the primary's watcher (node kept running) | 3 s | Its node became non-writable when the lease ended; another node accepted writes 11.05 s after the kill |
| Two primaries killed in a row | 3 s | With two of three nodes down no node was writable (a minority never accepts writes); when the first killed node returned, a primary existed 0.9 s later, the node that had followed the later primary |
The test server A run: 19,527 acknowledged writes, 0 missing, all three nodes ended with 19,527 rows, 554 sampler sweeps never saw two writable nodes. Reading it honestly (the decision record's words): one run, one host, over loopback (no network latency, loss or partition there; partitions are in the in-process tests), process kills not machine or disk failures. Not done: repeated runs for a distribution, the same on three machines, a run with a disk or network failing.
Shorter leases shorten failover and cost more false failovers on a slow node.
Settings of the watchers#
Put these in each watcher's watch.conf besides the single-node keys of Monitoring with the watcher. address must be this watcher's own node.
| Key | Meaning | Default |
|---|---|---|
cluster_id | This watcher's id in the quorum (0 turns the control plane off). | 0 |
cluster_peers | Every member as ID=WATCHERHOST:PORT@NODEHOST:PORT, comma separated: the watcher's address for the other watchers, and the address of the node it looks after. | empty |
cluster_secret | The shared secret that authenticates the watchers' messages. | empty |
cluster_groups | Table groups, 1 to 64; must equal the nodes' groups. | 1 |
control_user | The nodes' control account. | empty |
control_password, control_password_file | Its password. | empty |
lease_ms | How long a primary's lease lasts (300 to 120000). | 3000 |
control_margin_ms | Extra wait after a revoke on top of the lease (0 to 60000). | 500 |
sync_replicas | How many followers a write waits for; must match the nodes' sync-replicas (0 to 16). | 1 |
control_fail_after_ms | How long the primary's node may be unwell before it is replaced (100 and up). | 3000 |
control_rebalance_after_ms | Hand a group to its preferred node when nothing has changed for this long; 0 is never. | 60000 |
control_dir | Directory for the quorum's state file (watch.state). | empty |
A peer entry looks like 1=W1HOST:7600@N1HOST:7480. The watcher's status JSON has the groups' epochs and primaries. When the quorum has no majority, a managed node has no primary: writes are refused. Without these settings the watcher only reports and restarts.
Resource control (D51 step C3)#
With resource_control = true (and control_user and a password), the watcher steers the node's background work by the latency the foreground sees. Every 500 ms it reads GET /v1/control/state and sets POST /v1/control/throttle.
| Key | Meaning | Default |
|---|---|---|
resource_control | Switch. | false |
fg_latency_ms | Foreground p99 to stay under. | 50 |
compaction_floor_mb | Compaction is never limited below this (MiB/s). | 4 |
replication_limit_mb | Fixed limit on the replication sender (MiB/s); 0 is none. | 0 |
cgroup_path | The server's cgroup v2 directory; empty leaves cgroups alone. | empty |
cpu_weight, io_weight | Written to cpu.weight and io.weight; 0 leaves alone. | 0 |
memory_high_mb, memory_max_mb | Written to memory.high and memory.max; 0 leaves alone. | 0 |
Rules: stalled writers lift the compaction limit first; a p99 over the target for two rounds while compaction writes sets the limit to 60% of what it was writing; six calm rounds raise it by half and finally remove it. The p99 is a bucket upper bound (factor 2). Writing the cgroup files needs the right to write them; a failure is logged and retried every 60 seconds. The server must run in its own cgroup (a systemd unit does that). The watcher never deletes or edits data.
Security and operations#
- The watcher's messages are authenticated (HMAC with
cluster_secret) but not encrypted. - Stop the watchers before replacing a server binary if you do not want restarts (or accept the reports).
- The watcher is now part of the availability path: a bug in its decision rule can cause data loss that an in-server version could not (the fence limits the damage to one writer at a time, not to zero), so the decision rule is the part to review hardest (D51's own words).