Replication and clustering

Replication overview

How Raft DB copies data between nodes, how it chooses a primary, what "Raft" and "master-master" mean in this project today, and a map of the chapter.

Raft DB's replication has three layers. Each is built on the one below.

LayerWhat it doesMechanismPage
1. Copying dataA replica holds a byte-identical copy of a primary's logs and serves reads.The primary streams durable log bytes per shard; the replica appends them unchanged and applies them.Log shipping and read-only replicas
2. Confirming writesA write is acknowledged only after N followers have it on their disks.The follower acknowledges each chunk it has applied and committed; the primary's flusher waits for N acknowledgements.Synchronous replication
3. Choosing the primaryWhen the primary fails, another node takes over automatically, and only one node accepts writes at any time.A quorum of three or more watchers elects a leader with Raft and decrees who is primary; each server keeps a fence (epoch and lease).The fence, Failover with a watcher quorum

On top of that, table groups spread the primaries over the nodes so that different tables have different primaries (Table groups and partitioned tables), and a primary can be moved on purpose (Handover and rebalancing).

What "Raft" means here#

The Raft algorithm (a pure state machine that has been tested in a deterministic simulator with message loss, duplication, reordering, partitions and crashes) runs in the watchers. Its log holds only decrees: "node X is primary of group G at epoch E" and "revoke". The data does not go through Raft: it travels by log shipping with strict synchronous confirmation. The safety argument combines the two: an acknowledged write is on the old primary and on at least sync-replicas followers; the node with the highest (data epoch, log position) among the answering nodes holds a superset of what every answering node holds.

The decision record D50 states the weakness plainly: elections and data movement are two mechanisms, not one log, so a bug between them (the dirty flag, the ranking) is the kind of bug to look for in review and in the failover tests. Replacing log shipping by a Raft-replicated data log is the larger plan ("option A"); it is not built.

What "master-master" means here#

The owner chose option A of the plan ("Raft groups per shard group, leaders spread over the nodes"). Built so far:

  • Table groups (layer 1): groups = N. Each group has one primary; with 3 nodes and 3 groups each node leads one group, so writes to different tables go to different nodes, and any node accepts a connection and forwards a request for a table it does not lead.
  • Partitioned tables (layer 2): one logical table made of several physical tables spread over the groups.

Not built: several writers for the same key or table (that would need conflict resolution, option B), Raft replication of the data log, true partitioning with a replication factor below the node count, online split and merge, cross-partition transactions. See Status: built and open. "Master-master" in Raft DB today means "every node leads some of the data", not "any node accepts any write".

Read-only nodes#

A replica (--profile replica --replica-of ...) and a follower of a managed cluster refuse writes (binary error 12 READ_ONLY, HTTP 403 read_only_replica). They serve reads over the binary protocol and as HTTP queries (POST /v1/query, JSON filters or the server's own SQL subset). Whether raftdb-mysql works against a replica is neither documented nor tested: it writes its table definitions and counters as ordinary rows, and a replica refuses writes. In table-group mode a request for a table led by another node is forwarded to its primary, so reads are as fresh as the primary and a connection reads its own writes.

Where the decisions are made#

DecisionMade by
Which node is primary of a groupThe watcher quorum's leader (decide).
Whether this node may write nowThe node's own fence (epoch and lease).
Whether a write is acknowledgedThe primary's flusher, after its own fsync and N followers' confirmations.
Who gets the data after a failureThe node with the highest (data epoch, log position) among those that answered.

Pages in this chapter#