Replication and clustering

Log shipping and read-only replicas

How a replica copies a primary's logs byte for byte, what it serves, how it resumes and resyncs, how to start and promote one, and what it does not copy.

A shard's log is already an ordered, checksummed, durable record stream with byte positions, so a replica is a second copy of the same files (decision D38). The primary streams durable log bytes per shard; the replica appends them unchanged to its own log and applies them with the code that replays a log, so a replica's log file is byte-identical to the primary's. That makes resuming after a restart simple (continue at the file's end), promotion simple (the files are already a valid primary's) and testing simple (compare the bytes).

Syntax#

raftdb-server --profile replica --replica-of HOST:PORT [--replica-user NAME:PASSWORD | --replica-token TOKEN] [--dir DIR]

Or in raftdb.conf:

profile = replica
replica-of = PRIMARY_HOST:7480
replica-user = replicator:PASSWORD

Options#

SettingMeaning
replica-ofHOST:PORT of the primary. Required by the replica profile.
replica-userNAME:PASSWORD of an account on the primary that may read every database (an unrestricted readonly account is enough).
replica-tokenA token of the primary instead.

The primary profile creates a replicator account with readonly role and writes its password to <dir>/replicator.password, and listens on 0.0.0.0:7480.

How it works#

  1. The replica logs in to the primary and asks for the catalog (Catalog): the databases, tables and shard counts. It creates them locally with the primary's shard count (the shard of a key depends on it). A manager thread repeats this every 2 seconds, so new and dropped tables follow.
  2. For every shard of every table it sends Follow{db, table, shard, offset, tail_crc}: where it stands and a CRC-32C of the last 4 KiB it holds.
  3. The primary answers with Log{offset, bytes} chunks, forever; an empty chunk every 5 seconds is a heartbeat. If the CRC does not match (the primary checkpointed and rewrote its log, or is shorter) it answers Resync and the replica rebuilds that table from zero.
  4. The replica applies each chunk, keeps its secondary indexes current, commits it to its own disk and (for synchronous replication) acknowledges it.

What a replica does#

RequestResult
Reads over the binary protocol, POST /v1/query, loginsServed.
Writes over the binary protocol (Put, PutJson, Delete, CreateIndex, FieldId)Error code 12 READ_ONLY.
HTTP writes and catalog changes403 read_only_replica.
POST /v1/promote (an unrestricted admin)Replication stops and writes are allowed; the logs are exactly the primary's up to the point it had reached. A second call is 409; a server that was not started as a replica answers 409.

GET /v1/server shows node.role (master or replica) and replication: the primary's address, lastSyncSecondsAgo, lastError and per table and shard appliedBytes and connected.

Remarks#

  • Asynchronous by default. The primary acknowledges a write after it is durable on its own disk; the replica applies it as soon as it receives it (typically milliseconds on a LAN). A failover from an asynchronous replica can lose the newest acknowledged writes. Use synchronous replication to prevent it.
  • A checkpoint on the primary re-copies tables. After a primary checkpoint (at start by checkpoint-min-mb, or on demand) every replica of that table fetches it again: a few seconds plus the copy time. Replicas never checkpoint themselves, because that would break byte identity.
  • Accounts and roles are not replicated. Give every node its own user lines.
  • Renames are not carried as renames: a renamed table is dropped on the replica and fetched again.
  • Replicas of lsm tables work: each shard keeps a replication log repl-N.log in the same format, so an lsm server and a memory server can follow each other either way. Every row is then written twice on the primary.
  • No TLS on the stream: use a private network or a tunnel.
  • A replica is redundancy, not a backup: a mistaken DELETE is replicated.

Examples#

A primary and a replica on one machine for a try-out (two data directories, two ports):

raftdb-server --profile primary --dir ./p --addr 127.0.0.1:7480

(The primary profile accepts any address, here loopback; the single profile refuses a non-loopback one.) The replica:

raftdb-server --profile replica --replica-of 127.0.0.1:7480 --replica-user replicator:PASSWORD --dir ./r --addr 127.0.0.1:7481

where PASSWORD is the content of ./p/replicator.password.

Tested: a replica converges (data, deletes, indexes, new field names, new tables), refuses writes, picks up live changes, survives a restart, resyncs after a primary checkpoint, follows a dropped table, promotes, and its log files equal the primary's byte for byte.

See also#