Replication and clustering

Running a cluster

Step by step, three servers and three watchers with automatic failover, using the settings of the project's own test harness, plus how to check, test and stop it.

This page builds a three-node cluster with automatic failover from the commands and settings of the project's real-process failover test and the watcher's settings. The test runs everything on loopback; the notes say what changes across machines. Nothing here has been run by this manual's author on your hardware: test your own cluster as in the last section.

What you need#

  • Three machines (or, for a try-out, three ports on one machine). Three watchers, one per node.
  • A private network or tunnels between them: no TLS on the data stream, the control calls or the watcher messages.
  • The same user lines on every node (accounts are not replicated).

1. The nodes#

Each node's raftdb.conf (from the harness, with the data directory and ports changed per node):

dir = /var/lib/raftdb
addr = 127.0.0.1:7480
shards = 2
control = true
user = ctl:CTL_PASSWORD:control
user = app:APP_PASSWORD:admin
replica-user = app:APP_PASSWORD
sync-replicas = 1
sync-timeout-ms = 1500
  • control = true makes the node read-only until a watcher gives it a role.
  • ctl is the control account the watchers use; it can do nothing with data.
  • app is the data account; replica-user is what a follower uses to read the leader, so it must be able to read every database.
  • sync-replicas = 1 with three nodes: each write is confirmed by one follower.
  • Across machines, bind a network address: addr = 0.0.0.0:7480 needs profile = primary (the single profile refuses non-loopback addresses). Note that with explicit user lines the primary profile creates no replicator account.
  • Use a different dir per node on one machine, and a different addr port.
  • Start each node: raftdb-server --config /etc/raftdb/raftdb.conf.

2. The watchers#

Each watcher's watch.conf (the harness's, for node 1; change cluster_id, name, address per node):

name = n1
address = NODE1_HOST:7480
user = app
password_file = /etc/raftdb/app.pw
probe_write = false
interval_seconds = 5
timeout_ms = 2000
cluster_id = 1
cluster_peers = 1=W1HOST:7600@N1HOST:7480,2=W2HOST:7600@N2HOST:7480,3=W3HOST:7600@N3HOST:7480
cluster_secret = LONG_RANDOM_SECRET
control_user = ctl
control_password_file = /etc/raftdb/ctl.pw
lease_ms = 3000
control_margin_ms = 500
sync_replicas = 1
control_fail_after_ms = 3000
control_dir = /var/lib/raftdb-watch
  • address is this watcher's own node. cluster_id is its id in cluster_peers.
  • sync_replicas must equal the nodes' sync-replicas.
  • control_dir holds the quorum's persistent state. Give each watcher its own.
  • cluster_secret must be the same on all three and kept private.
  • Start each: raftdb-watch --config /etc/raftdb/watch.conf (after --check-config).

3. Check it#

Within a few seconds one node should be primary and the other two followers in sync. Look at:

  • the watcher's status page (status_port): a groups array of {group, epoch, primary};
  • GET /v1/control/state on each node (log in with the ctl account): writable true on exactly one node, inSync on the followers, dirty false.
  • GET /v1/server: node.role is master on the primary and replica on followers.

Write through the primary, read from a follower (a synchronous write is already there).

4. Try a failover#

Only on a test cluster. Stop the primary's server (Ctrl-C or kill). Expect writes to fail for about fail_after + 2 x lease + margin (about 12 seconds with these settings), then another node becomes primary and accepts writes. Start the stopped node: it follows the new primary, becomes clean and in sync. Every acknowledged write must be on the new primary; verify with your own row counts. The project's own failover test does the same with real processes; it is not part of the download.

5. Several groups#

Set groups = N on every node and cluster_groups = N on every watcher, the same everywhere. See Table groups and partitioned tables. The project's own three-group test is not part of the download.

6. Operating it#

TaskHow
Move a primary on purposeHandover and rebalancing
Take a node downHand over its groups first. Stopping two of three nodes leaves no primary.
Add or remove a nodeNot supported on a running cluster: membership is fixed in cluster_peers (the Raft core has no membership change yet).
Replace a lost nodeStart a node with an empty data directory and the same settings; it should fetch the catalog and follow the primary (replica copying is tested; replacing a node of a managed cluster this way is not). cluster_peers keeps the same ids.
Change groupsNot supported on a running cluster.
Add an accountEdit the user lines on every node and restart them one at a time, or the new account works on some nodes only.
Restart a watcherThe node's lease is no longer renewed while it is down: if it is the primary's watcher, the node stops writing when the lease ends and another node takes over. Restart watchers one at a time.

Single primary with manual promotion#

If you want a replica but no automatic failover, do not use control: run the primary with --profile primary (and --sync-replicas if you want) and a replica as in Log shipping and read-only replicas. To fail over, stop the primary, POST /v1/promote on the replica, and point the clients to it. A node started without control and without replica-of is a plain primary; without a watcher quorum there is no automatic failover at all.

See also#