v0.7.0
Menu

#Replication

One server can follow another. A follower is a copy that serves reads and takes no writes. There is one leader, and followers copy its logs. There is no election and no failover, and this is not clustering or sharding.

#Setting up a follower

Start the follower with the address of the leader and a login for it:

FERRITE_REPLICATE_FROM=leader.example.com:7878 \
FERRITE_REPLICATE_TOKEN=<the leader's token> \
FERRITE_DATA=/var/lib/ferrite \
ferrite_db
variable meaning
FERRITE_REPLICATE_FROM host:port of the leader. Setting it makes this server a follower
FERRITE_REPLICATE_TOKEN log in with the leader's token
FERRITE_REPLICATE_USER, FERRITE_REPLICATE_PASSWORD log in as a user instead. The user needs admin on *, since it reads the users log too
FERRITE_REPLICATE_TLS_CA PEM file of the authority that signed the leader's certificate. The host in FERRITE_REPLICATE_FROM has to be a name in the certificate
FERRITE_REPLICATE_TLS_CERT, FERRITE_REPLICATE_TLS_KEY the certificate the follower shows if the leader asks for client certificates

Use the token or a user, not both. With no login, the leader has to be open. The same names work in /etc/ferrite/ferrite.conf, see deployment.

A follower deletes what the leader does not have. Databases and users it had of its own are removed when it connects. Do not point one at the wrong server.

#How it works

The follower keeps its own data directory the same as the leader's, byte for byte. It asks the leader for every log (each database and _users.log) from where its own file ends, applies the records the way a restart replays them, and appends them to its own log. So it is durable and restartable: after a restart or a lost connection it goes on from the end of its own files, retrying after 1 s, 2 s, 4 s and so on up to 30 s.

  • Users and grants follow too, so the leader's logins work on the follower.
  • A transaction is one record and arrives whole or not at all.
  • A database dropped on the leader is dropped on the follower.
  • The leader keeps nothing for a follower in memory. It reads its log files up to the last synced write, a megabyte at a time, with no lock on a collection, so a slow follower only slows itself down.
  • The leader sends a beat every second. A follower that hears nothing for 10 seconds drops the connection and reconnects. A follower that does not read for 60 seconds is cut off, and comes back from where it was.
  • The follower prints following, caught up with, behind (every 10 s while it has bytes to go) and lost the leader lines.

#What a follower answers

Reads: find, get, count, aggregate without $out or $merge, and watch. Everything else gets the code read_only: writes, compact, dropDatabase, transactions and the user commands. A follower never compacts, since that would change its log.

#When the leader compacts

After a compaction the leader's log is a new file, and the follower's place in it means nothing. The follower starts that database again from the whole new log. The leader notices this two ways: a per-log generation counter while a follower is connected, and on reconnect the length of the follower's log plus the checksum of its last record. The leader sends from there only if the same checksum ends a record at that length in its own file. A follower that is ahead of its leader (the leader lost the end of its log in a power cut) is resynced the same way.

While a database is being fetched again, reads of it see it empty or half there.

#replicationStatus

An admin on * can send:

{"cmd":"replicationStatus"}

The reply has role. On a leader it lists followers, each with address, positions (how far into each log it has been sent), lagBytes and seconds connected. On a follower it gives leader, connected, lagBytes (what the leader had that it has not applied, as of the last message) and positions (the length of each of its own logs).

{"cmd":"replicate","logs":{...}} is what a follower sends to the leader. After it comes a binary stream that is not for clients to read.

#Promoting a follower by hand

There is no failover. To promote a follower:

  1. Stop it.
  2. Remove FERRITE_REPLICATE_FROM (and the other FERRITE_REPLICATE_* variables) from its environment or ferrite.conf.
  3. Start it again.

It opens the same data directory and is a leader, with whatever it had received. Anything the old leader wrote after that was not sent is not there. Point the other followers, and the old leader if it comes back, at the new one. They will fetch again every log that differs from it.

Clients have to be moved to the new leader by you.

#Known gaps

Three are still to be decided:

  • A follower deletes databases and users the leader does not have, so pointing it at the wrong leader destroys data.
  • During a resync that database reads empty or partly, and watch streams on it end.
  • Resume compares only the length and the last record's checksum. Two diverged logs that end in the same record at the same offset look the same, because there is no log id.

Also known:

  • A leader compaction makes the follower fetch the whole database again.
  • Leader lag is bytes sent, not bytes applied.
  • If the replication user is dropped, the follower cannot log in again.