Replication
Keep read replicas of a server, promote one when the primary is gone, and back up every write so you can restore to any moment.
Start a replica
Give the primary a replication token, then start a second
fennec-server on its own file pointed at the primary. The replica
serves reads; when the primary is gone, promote it to take writes.
# $HTTP and $REPL: the same two tokens on both machines, each 32 bytes or
# more (openssl rand -hex 32), as a server listening past loopback takes them
# the primary: replicas are fed over its HTTP listener
fennec-server --file primary.fennec --http 0.0.0.0:8080 --http-token "$HTTP" \
--replication-token "$REPL" --sync always
# a replica, over a file of its own
fennec-server --file replica.fennec --http 0.0.0.0:8081 --http-token "$HTTP" \
--replication-token "$REPL" --replica-of http://primary:8080
# the primary is gone: the replica takes writes from here on
curl -X POST replica:8081/_replication/promote -H "Authorization: Bearer $REPL"
# {"promoted":true,"seq":41873,"history":"5f0e2c9a81b44d17"}- Every read works on a replica, subscriptions included, and a call of a named statement whose body only reads.
- Writes are refused until it is promoted, by hand.
- A failover can lose only what the primary's
--synchad not yet put on disk. - The stream can run over HTTPS. Give the replica
--replica-of https://primary:8443for a primary that serves it. Its certificate, chain and host name, is checked against Mozilla's roots, or against the CAs of--tls-ca <pem>in their place for a private CA, and a certificate refused is refused before the token is sent (Dialing HTTPS).
What a replica is and answers
A primary sends its replicas the writes that have reached its disk, and each applies them to a file of its own and serves reads from it.
A replica is a whole fennec-server: every read works on it,
subscriptions included, from its own copy in memory, and a write gets 403 —
a call of a named statement too where its body writes; one whose body only
reads is a read, run from the statements the replica's file follows.
GET /_replication says which role a server has, so a client or a
load balancer can tell a replica from the primary.
What is sent
A replica is sent the primary's writes in order and applies each one to its own file. Only writes already on the primary's disk are sent.
- A restarted replica asks only for what it missed.
- A replica far behind (past 64 MiB of writes) is sent a whole copy of the database instead.
- A whole copy is read from the primary's file as it is sent, and
written to disk as it arrives — a new replica's, a backup's, an
archive's, a moved tenant's — never gathered in memory on either side,
and with no lock held while it goes. The replica,
fennec backupand an archiver check every record's checksum as the copy streams in; a replica puts it in place of its file only once it is whole, so a copy cut off or damaged on the way leaves the replica as it was. A database larger than the memory of either machine follows and backs up, and a tenant moves between the nodes of a region up to the 1 GiB an image installed on a node may be. A copy taken slower than 64 KiB a second, past its first 10 seconds, is ended by the primary, which logs it, and the receiver says so as it logs the cut; a copy a crash left half taken beside a file is removed the next time the file is opened to be written. --sync offis refused on a server with a replication token.- A
compact, the primary's or the replica's own, is not sent and changes nothing a replica reads: a dropped field's place stays in every document, so the writes after it read alike on both (from 0.2.0).
How records are sent and caught up
A fennecdb file is already a log: an image, then every write appended as a
record. A replica is sent those records, whole and in order, and applies each
the way the write path made it — the document, and the upkeep of every index
on it — and appends it to its own file. Its file therefore ends as the
primary's does, and a replica stopped and started again reopens where it was
and asks only for what came after. The primary keeps the last 64 MiB of
writes for this (--replication-buffer); a replica further behind
than that is sent an image of the whole database instead, and follows write
by write from there.
Only writes on the primary's disk are sent. A write enters the feed when it
is appended and leaves it once an fsync has covered it, so a primary that
crashes comes back holding every write any replica was sent, and a replica
never has to be told to forget one. The price is latency, set by the
primary's --sync: under always a write goes out right
after its own fsync, under 250 up to 250 ms later. Under
off nothing is on disk before shutdown, so a server with a
replication token refuses to start with it.
Positions and histories
A replica's position is a change number that means the same on the primary and on every replica. After a promotion the history forks, and a replica is only continued from a point the new primary also passed through; otherwise it is sent a fresh copy.
| A replica that… | is sent |
|---|---|
| reconnects to a primary that restarted, even after a crash | the writes it missed |
| was stopped and started again over its file | the writes it missed |
| is pointed at a promoted replica, holding nothing past the fork | the writes it missed, and the new history |
| is pointed at a promoted replica, holding writes past the fork — the old primary, say | an image |
| is further behind than the primary keeps writes for | an image |
| starts empty | the writes from the first, or an image |
| could not apply a write — refused, or stopped part way by a panic, which puts the write back whole before any read sees it | an image |
Positions, histories and forks
Every write moves the change counter by one, on the primary and on each
replica alike, so a replica's position is its own counter and a subscriber's
cursor means the same change on either. A block — the writes of one statement,
a /batch — is one record numbered by its last
write, and is sent and applied whole: a replica never stands inside one.
A replica of a replica is fed exactly as its primary was.
Numbers alone stop being enough once a history forks. A replica promoted after its primary died writes its own change 1 001; the old primary, or a second replica that had got further along, may hold a different 1 001. Streaming the new primary's 1 002 on top of that would go unnoticed. So each fork gets a fresh id and remembers the change where it left its parent, as PostgreSQL numbers its timelines, and a replica is continued only from a position the primary's own history passed through, as the table above shows.
The history lives in the file (record kind 8), and the first time a primary serves replicas it draws an id of its own — the root history is every database's, so it names none.
Reading your own writes
A replica can be a moment behind, so a read sent right after a write may
miss it. Send the number a write's answer carries in
Fennec-Seq as Fennec-After, and the replica waits
until it has that write.
curl -i -X POST http://primary:8080/query -d '{"query":"put carts {user: 7}"}'
# Fennec-Seq: 1042
curl -H 'Fennec-After: 1042' -X POST http://replica:8080/query \
-d '{"query":"get carts where user = 7"}'- It waits 5 seconds unless
Fennec-Waitsays otherwise (in milliseconds, 30 s at most), then answers 504. - A subscription and
/_changeswait too, before their head: a stream sent with the header is seeded with the write, and a change-stream page'sFennec-Seqis never behind it. - Across a failover, send
Fennec-Historytoo: the answer names the history its change is of, which a promotion forks. A replica left on the old branch holds a change of that number of its own, and answers no read sent after the new one. - Its token comes first: a request the server refuses (401, or 403 for another tenant's token) is answered at once, not after the wait, and learns nothing of where the replica stands.
- A read marked
Fennec-Read: replicanames where it read: the answer carries the copy'sFennec-SeqandFennec-History, so a read sent after it never reads older (Read replicas). A read nobody marked names nothing. - The Python client keeps the number for you:
client.seq, sent withquery(..., after=...).
How Fennec-Seq and Fennec-After work
A replica is behind its primary by the time a write takes to reach it —
0.04 ms at the median under --sync always, up to the sync
interval otherwise — so a client that writes on the primary and reads on a
replica can read from before its own write. Over HTTP a write's answer
names the change it left the database at, Fennec-Seq; a request
sent to a replica with Fennec-After: <that number> is
served once the replica holds it, and answered 504 with where the replica
stands if it does not within the wait — never from before the write.
A change number names a write within one history. A promotion forks the
history where the replica promoted stood: a write the old primary answered
and the replica never got is lost, the new primary's next write takes its
number, and a replica still following the old primary holds a change of that
number — the lost write. So an answer of a database that has had a replica
also names its history, Fennec-History: <term>-<id>,
the term one more at each promotion; a read sent with it beside
Fennec-After is served once the replica holds that change of that
history — a replica on the old branch waits until it follows the new primary
and takes its image. A primary asked for a position its history does not
hold — the write lost — answers 504 at once, with its own
seq and history, which a client takes as where it
stands from then — over the position it sent that read after alone, and
never an earlier term of the same lineage, which only a fenced old primary
names. A history's lineage is the top half of its id: every promotion and
restore keeps it, and a database made anew — a tenant deleted and made again
under its name, starting again at term 1 — has another, whose position a
client takes, its term lower or not.
The wait is 5 seconds unless Fennec-Wait gives it in
milliseconds, 30 seconds at most, and a waiting request is woken by the
write that reaches its change, not by each write before it: while a replica
catches up, each read waiting on it costs a look ten times a second, not one
at every write it applies. On a tenant node
a wait takes one of the tenant's streams, but only once it must wait: a read
after a write the node holds already takes none, as a tenant whose streams
its own subscriptions fill still reads its writes. Writing the
header costs the server nothing measurable; a client that parses every
header, as Python's http.client does, reads one more.
Promotion
Promote a replica by hand: POST /_replication/promote on its
HTTP listener, or restart it over its file with --promote in
place of --replica-of. Keeping the old primary from taking more
writes is up to you.
What promotion forks, and the old primary's part
Either way forks the history where the replica stands and turns writes on.
A replica's file does not open any other way — started with neither flag,
fennec-server refuses and says which to give — because a replica
taking writes without a fork would continue its primary's history with
writes the primary never made.
Making sure the old primary takes no more writes is the operator's part, as
it is with PostgreSQL: nothing here fences it. Brought back, it rejoins under
the new primary with --replica-of, and is sent an image if it
wrote past the fork.
What a failover loses
With --sync always on the primary, a failover loses no write
the replica was sent, and in ten failovers of a replica keeping up no
acknowledged write was lost. A replica not connected, or behind, when its
primary is lost is promoted where it stands, without what it was not sent —
a write waits a second at most for a stream that keeps up and not at all for
one behind or reconnecting — so watch connected,
behind and lag_ms in
/_replication/status and fennec_replica_connected,
fennec_replica_behind and
fennec_replica_lag_seconds in /_metrics (Monitoring), and over a network others can reach
follow https://: on a plain-HTTP hop, whoever can reach it can
cut the stream. behind counts writes from what the stream last
said of the primary's disk, and a primary's sent is what it has
written into the replica's connection; writes waiting in the sockets between
the two — several megabytes on Linux — count in neither. A replica reading
slower than its primary writes, as one did for 20 seconds during a
compaction on a loaded machine, read behind 0 while it was 6
seconds behind; its lag_ms, read off the time its primary
stamped on each write, said so as it read them. A replica taking an image is
counted as sent each write from the image's flush on, so one promoted
before it has applied the image loses the writes answered meanwhile,
connected false. With the default --sync
250, a failover
loses up to the last 250 ms of writes.
A power cut was measured on the primary, with no failover after it: no
replica was promoted. Cut ten times on a Linux guest (ext4), in two runs, a
primary under --sync 250 lost 54 to 794 acknowledged writes a
cut, none answered more than 316 ms before it — 250 ms plus the sync's own
time — and kept every write its change feed had handed a reader, as a prefix
of the writes. A --dir node under --sync always lost
no acknowledged write at any cut. Its standby, read as each cut left it, held
nothing the node came back without and was 0 to 51 changes behind it, a 20 MiB
spill still unanswered at the cut among them. The second run counted
the writes the node had answered as durable that the standby lacked: none, at
all ten tenant cuts, so a promotion at any of them would have lost nothing.
That was measured, not promised: a write answered as durable is on the
primary's disk and in its socket to each replica, and a power cut can take the
socket before it sends, where a killed process's kernel still delivers it.
Once the node was back, the standby held the same rows again within 3.4 s of
being asked. Under --sync 250, where a primary sends a write once a
sync covered it, the standby held every write the node had on disk at every
cut of three runs, two of a node of two tenants and one of four: what it
lacked, the cut took from the node too.
primary's --sync | acknowledged writes/s | lost per failover |
|---|---|---|
always | 284 | 0 in all ten |
250 | 53 565 | 9 700–10 788: the last ≤ 201 ms |
Method, and what a failover loses
How it was measured: a primary killed with SIGKILL while one
client writes to it as fast as it answers, the replica promoted half a second
later, ten times per policy; counted are the writes the client was told
succeeded that the promoted replica does not hold (make
replica-bench, macOS, loopback).
Under 250 the loss is the policy's own: those writes were
answered before any fsync and were not on the primary's disk either. Under
always a write is answered after its fsync and once the stream
to each replica has written it into its socket, which the kernel delivers
after the process that wrote it is killed. Sent after the answer, as it was
before, a write answered in the fraction of a millisecond before it
was sent was lost: none of these ten failovers caught one, while a node of
the SaaS example killed under eight writers lost one in 3 of 60 runs, and
none in 120 since. A stream that has not sent a write within a second is
not waited for until it catches up again, so a stalled replica costs the
writes one wait, and a replica reconnecting none: writes answered meanwhile
are on no replica, and a failover then loses them. The ten failovers above
promoted a replica that was keeping up. A machine that dies takes its sockets with it: this holds
for a process killed, not for a power loss. No failover left the replica
holding a write the client had not been answered for.
What it costs
A write is readable on a replica 0.04 ms after the primary answered it
under --sync always, and within the sync interval otherwise. A
replica keeps up with a bulk load.
primary's --sync | p50 | p95 | p99 | max |
|---|---|---|---|---|
always, writes back to back | 0.04 ms | 0.06 ms | 0.09 ms | 2.77 ms |
250, 250 writes a second | 148 ms | 262 ms | 273 ms | 282 ms |
Method, and the lag and bulk-load figures
How it was measured: primary and replica in one process over loopback, each over its own file; lag is from a write's answer on the primary to the write being readable on the replica, over 1 000 writes.
A replica keeps up with a bulk load: 100 000 documents of 128 dimensions with an HNSW index, which the primary wrote in 23.4 s in statements of 1 000, were applied from the stream in 19.7 s — 5 088 writes a second, the graph built as they arrive. A new replica of that database sent it as an image — 58.3 MB, the graph in it — answered from it 0.17 s after it connected.
The primary keeps the writes in memory up to its buffer, and copies the
image it sends; a replica holds the database in memory as a primary does. The
replica's HNSW graph is built from the same vectors in the same order, but not
in the same batches, so an approximate near can rank a close call
the other way on the two; everything else answers identically.
Backups and restoring to a moment
An archive is the replication stream written to a directory: base images
and every write after them, timestamped. From it, fennec restore
rebuilds the database as it stood at any moment or change. A primary or node
that serves HTTPS is given as https://host:port to fennec
backup and fennec archive, with --tls-ca <pem>
for a private CA (Dialing HTTPS).
# the running database, taken whole: a file that opens as a database of its own
fennec backup http://primary:8080 nightly.fennec --token "$REPL"
# every write that reaches the primary's disk, until interrupted; an image of
# its own every hour, and what no restore to the last week needs let go of
fennec archive http://primary:8080 /backups/archive --token "$REPL" --image-every 1h --keep 7d
# what the archive can restore, read as a restore would; exits 1 if it cannot
fennec verify /backups/archive
# the database as it stood just before the mistake
fennec restore /backups/archive before.fennec --to 2026-09-22T10:14:59Z
fennec restore /backups/archive before.fennec --to-change 41873--tostops at a moment;--to-changeafter a change number.--keep(orfennec prune) deletes what no restore within that window needs.fennec verifysays from when to when the archive can restore.- A restore across a
compacton the primary reads the writes after it as the primary did, from an image taken before it too: a compact keeps every field's place (from 0.2.0). - A backup is written to its file as it arrives, sealed as it goes, and every record's checksum is checked on the way: a database damaged in transit is refused, its file never put in place, and a database larger than the backing-up machine's memory backs up.
- A backup or a restore into a file holds the file while it is
written, as the shell does: one a running server has open is refused, its
holder named, rather than renamed over under it. A file there already is
refused too (exit 2) unless
--forcesays to replace it. - On Linux and macOS a backup, a restore and an archive are their
owner's alone: each file mode
0600, an archive's directory0700, whatever the umask. A file replaced with--forcekeeps the mode it had, so achmod 640for a backup group is set once, in a directory of that group, setgid and searchable by it (chmod g+xs); an archive's new images and segments are its owner's, and read by its user (Files on a shared machine).fennec verifyrestores the archive's end into a new directory of its own in the temp directory, mode0700, and removes it after.
How a restore to a moment works
The same stream written to a directory instead of applied is an archive:
base images, and every write after them with the time the primary appended
it — or, where that clock stepped back, the latest time stored before it,
which verify and a restore then report. The
fennec command line writes and reads one.
A restore takes the latest image at or before the point asked for and
appends the archived writes after it up to there — a fennecdb file is an image
and the writes after it, so that is the whole restore — then opens the result
to check it, forks its history and checkpoints it. --to keeps
every write the primary appended at or before that moment and stops at the
first one after it; --to-change stops after that change. A
statement writes each document as a change of its own, so a del
of ten documents is ten changes.
The archive is fed as a replica is: only writes that were on the primary's
disk, so none a crash took back, and a compact on the primary
loses it nothing — the writes were archived before a compact folded them into
an image. An archiver that falls further behind than the primary keeps writes
for takes a new image and goes on from there; the archive then holds no write
between the two images, and a restore to a point in that gap says so rather
than inventing one. Restoring the 100 000 × 128 load above from an image and
its 100 000 writes took 19.9 s, most of it building the graph for them, and the
file then opened in 63 ms; to the load's middle, 7.8 s. A base image a day
keeps a restore to a day of writes.
image-<change>-<time>.fennec a base image: the database at that change
writes-<first change>.log per record, its stored time and the record
history the history the archive is onAn archive takes images of its own: every --image-every (an
hour unless given), once a write came after the last one, it opens its last
image and the writes after it and adds an image of the database there — the
primary is asked for nothing — named by the time of its last write, and the
segment being written ends so that the writes before the image are in
segments of their own. A restore then replays at most that much. With
--keep, what no restore within that window needs goes after
each image: the images before the newest one taken by the window's start,
which a restore to that moment starts from, and the segments whose writes
all came before the oldest image kept — never the one being written.
fennec prune <dir> --keep 7d does the same once. A clock
that ran ahead by some time holds pruning back that long: every later write
and image is stored at that time until real time passes it.
fennec verify <dir> reads the archive as a restore
would: every image opens at the change its name says and each of its
records matches its checksum, the segments run on from the oldest image
with no change missing, only the last segment may end in a record cut
short or one failing its checksum with nothing whole after it saying the
segment was synced past it, and a restore to the end opens. It prints from when
to when the archive can restore, names any gap — changes in no segment
between two images, where a restore to the end passes but a restore to a
moment inside cannot — and exits 1 when no restore can be made.
fennec verify data.fennec holds every record of a database
file to its checksum, the ones an open trusts as synced too, with what the
sync log beside it holds put back first, as an open would.
Replicas and archivers keep following a primary of a newer version
through an upgrade: a follower that does not say it reads record checksums
(sums=1 on /_replication, or on a node's
POST /_replication/tenants) is sent each record and image as a
binary from before them wrote it
(File format); the order to upgrade a
cluster in, and what 1.x promises, are on
Compatibility and upgrades. An archiver writes each
record it takes with a checksum of its own and how far it had synced the
segment, a plain one from a primary from before too, so a power cut on
the archiver's machine leaves an archive a restore reads to its last
synced write.
Archiving every tenant of a node
A node of tenants (fennec-server --dir)
is archived by one process over one connection: an archive a tenant under a
root, each the archive above, which fennec restore,
verify and prune read as they read any.
# every tenant of the node into /backups/n1/<tenant>/, until interrupted
fennec archive --tenants http://n1:8081 /backups/n1 --token "$REPL" --image-every 1h --keep 7d
# every tenant's archive read as a restore would; exits 1 if any cannot
fennec verify --tenants /backups/n1
# the node's directory as it stood at a moment: <tenant>.fennec for each tenant it held then
fennec restore --tenants /backups/n1 /data/n1 --to 2026-09-22T10:14:59Z
fennec-server --dir /data/n1 --replication-token "$REPL" ...- One connection and one thread on the node, however many tenants it holds, and no tenant held open: an idle tenant stays closed. One tenant's image or a slow archiver holds up no other tenant's writes.
- A tenant made on the node is archived from its first image; one deleted or moved to another node is no longer followed, and its archive is kept, noted gone.
- A tenant restored to an earlier moment, or made again under its name, begins a new archive; the one before is kept beside it, and answers for the time before.
- One tenant's damage holds up no other. A tenant whose archive does not read is left as it is for you to look at, and one whose stream sends a record failing its checksum is passed over until the archiver connects again; the archiver's log names the tenant, and every other tenant is archived on.
--key-file,--image-everyand--keepapply to each tenant's archive.- Under load the archive trails the node. Under
--sync alwaysa write is answered once the stream has sent it, as a replica's stream would, but only while the stream keeps up: while it is busy — a tenant's image, a closed tenant opened, a tenant's lock waited for, an archiver reading slower than the node writes — writes are answered without it. Lose the node's disk then and the archive lacks them; where the node survives they reach the archive later, at worst inside an image. Under--sync 250it trails anyway, as a replica does.
How one stream archives every tenant
A tenant's own stream (/t/<tenant>/_replication) is a
connection and a thread on the node for as long as it runs, one of
--max-connections, and it holds its tenant open the while.
Archived that way, a node of a thousand tenants holds a thousand of each and
every tenant open for good — and at the default of 100 connections it
cannot be archived at all. fennec archive --tenants asks the node
for POST /_replication/tenants instead, with the replication
token: every tenant's writes over one stream, each message led by its
tenant's name. Its body says where each archive under the root stands; the
node says hello to every tenant it holds — where the archive goes on from,
or an image where its place does not lead to the tenant's writes — and that
every tenant the archive lists and the node does not hold is gone.
The stream sends a tenant's writes from its feed while it is open, and
nothing while it is closed, whose file does not change. A tenant closes only
once the stream has sent what its disk holds — closed with writes unsent,
its feed would go with it and they would come again as an image — and is
looked at again at the next pass, never waited for. Under --sync
always a write waits for the stream as for a replica while the stream
keeps up; whatever can keep it busy — reading or opening a closed tenant,
waiting for a tenant's lock, which its longest write holds (a
/batch, a compact under the lock), an image's snapshot and its
sending, an archiver reading slower than the stream writes — it does held
up, and no write waits for it then. One stream for every tenant held each
tenant's next write for a second while it opened another one; each tenant's
own stream holds up its own tenant alone. An image waits for its tenant's
disk without holding up the stream — and is taken again, from the tenant as
it is then, where the tenant closed and opened again meanwhile — and the
node's syncer and a closing tenant wait for no stream at all, since no
client's answer waits on them.
So the archive of a node is synchronous only while its stream keeps up,
and under load it is not: a write answered while the stream was held up, or
behind after an image, is on the node's disk and not yet in the archive. It
is sent once the stream comes back to its tenant, or, where the stream fell
behind the tenant's feed, as part of an image; if the node's disk is lost
before that, the archive does not have it. A replica's own stream
(/t/<tenant>/_replication) keeps a write's answer waiting
for it, up to a second.
Which tenants have writes is told, not polled: a tenant's feed tells the
stream as its writes reach the disk — with no stream connected a sync pays
two loads, the same 13 ns a durable advance either way — and a tenant made,
installed, deleted, promoted or made to follow tells it too; one released
from under the stream is looked at again until it is gone or back. Where a
closed tenant stands is known where it closed in the node's process, and
read from its file otherwise — the first stream after the node starts: 1 001
tenants in 0.29–0.33 s, where an open, which fsyncs the file first, was
6.3 ms a tenant. A closed tenant the stream is told of is read again rather than
taken where it closed, since a holder of its database — the
@ttl sweeper — may have written it since; one with a sync log
beside it, after the machine went down, is opened.
A tenant's archive is <root>/<tenant>/, written
as fennec archive writes one; its segment file is open only while
the tenant writes, so the archiver holds a descriptor a writing tenant, not a
tenant. When the node says a tenant is gone — deleted, or moved to another
node, whose own archiver takes it up from its image on the same history —
the archive is kept as it stands and
<root>/<tenant>/presence notes the time; a tenant
that comes back is followed again and noted back, the time it was away a
gap. The times are the node's clock, as its writes' are: gone is when the
node deleted the tenant, though the stream was busy or the archiver away
when it did — or, where the node does not know (it restarted since), when
the stream saw it go — and back is when the node said hello to it again. A
tenant deleted and made again while the stream was busy is noted gone at the
delete before the new copy's hello; one deleted and made again while the
archiver was away is heard of as the new copy alone, and the line it moves
aside (below) is noted gone by that hello at the latest, so a moment between
the delete and then restores the old copy. A tenant whose copy on the node no longer leads from its archive's
place, at or before it — restored there to an earlier moment, or another
tenant made under the name — would land its writes among changes of the same
numbers: the line so far is moved aside to
<root>/<tenant>.<history>/ and a new one begins.
(fennec archive of one database refuses such a primary instead,
and leaves its archive as it was.) A promotion's new history reaches the
archive before the writes after it, so a promoted tenant's archive goes on
without an image. <root>/archiving.lock keeps a second
archiver off a root.
fennec restore --tenants <root> <dir> writes
<dir>/<tenant>.fennec for each tenant the node held at
--to — from the line of its history that began last by then, so
a line moved aside answers for the time before the next began — or at each
archive's end. A tenant the node said was gone then, deleted or living on
another node, is passed over, and so is one archived only after it; one
whose moment --keep let go of is refused, and the restore exits
1 — each line knows when it began, which the prune that lets its first
image go writes down first (began). --to-change names one database's change, and is
refused. fennec verify --tenants and fennec prune
--tenants run on every line, those moved aside too, and verify names a
gap an image alone ends.
Measured with make node-archive-bench: a node of 1 000
tenants of ten rows each and one of 64 MB, closed when idle, ten of them
written ten times a second from before the archiver connected and for 20 s
after, under --sync always, in turns, on this M1:
| no archiver | a stream each | the node's stream | |
|---|---|---|---|
| node: threads | 15 | 1 016 | 16 |
| node: descriptors | 27 | 4 011 | 29 |
| node: tenants open | 10 | 1 001 | 10 |
| node: CPU, of a core | 4.7–5.1% | 7.0–8.0% | 3.9–6.0% |
| archiver: threads | — | +1 001 | +1 |
| archiver: descriptors | — | 2 021 | 21 |
| archiver: CPU, of a core, the writers' 1.2–1.3% in it | — | 3.4–4.2% | 2.1–2.9% |
| first images of the 1 001 | — | 11.2–15.5 s | 14.8–15.2 s |
| writes as they were taken, p50 / p99 / max | — | 28–33 / 176–600 / 436–1 113 ms | 14.5–14.7 / 32–35 / 43–116 ms |
| writes after, p50 / p99 | 11.8–12.1 / 22.7–26.4 ms | 13.0–14.2 / 28.7–55.4 ms | 12.4–13.8 / 25.6–33.8 ms |
| node restarted: memory, tenants open | — | 121–157 MB, 1 001 | 2.2–4.6 MB, 0 |
--sync 250: the syncer's pass, p50 / max | 30.7–32.6 / 45–63 ms | 65–86 / 84–353 ms | 52.3–52.7 / 91–168 ms |
--sync 250: a tenant's close, p50 / max | 3.8–4.0 / 12–25 ms | — (none closes) | 4.0 / 15–33 ms |
The archiver's process runs the bench's writers as well, and the
descriptors were counted before each open tenant held its
<tenant>.fennec.lock: count one more an open tenant. A write's time
under --sync always is the Mac's F_FULLFSYNC; a
write waits for the node's stream as for a replica, and the stream sends
within the fsync's noise. While the first images were taken no write
waited for the node's stream; held up as a replica's stream is, it held
every written tenant's next write for a second whenever it opened another
tenant. A stream each held the writes up as images went out over a thousand
sockets at once, the build before alike (p99 86–351 ms, at most 110–994).
The node's stream
takes a new archive's first images one tenant at a time, each tenant opened
— an fsync — and its image written: about 15 s for the thousand and one,
once, the 64 MB one among them. After the node restarted, the archiver
connecting again, the stream said hello to all 1 001 tenants in 0.29–0.33 s,
each read from its file rather than opened; a thousand streams opened every
tenant again and held it.
Under --sync 250 (node_archive_passes: eight
tenants written every 5 ms, eight more opened, written and closed in turn,
one of 32 MB closed) the syncer's pass took 52 ms at the median beside the
node's stream against 31–33 with none: the archiver writes the same disk,
and the passes are its fsyncs and the node's together. Before the syncer
and a close stopped waiting for streams, a stream busy with one tenant held
a pass for a second a tenant, 5.06 s over five, and a close under its
lock for 1.01 s. Against the build before, in turns: with no archiver the
syncer's pass took 31.0–31.4 ms at the median and a close 3.6–3.9 ms, and
with a stream each the pass 67–73 ms, as here; the writes with no archiver
or a stream each as in the table. A pass here is every dirty tenant synced
in turn, as a node synced them then; a node now syncs each tenant on a
thread of its own, so the archiver's fsyncs lengthen each tenant's own sync
rather than a pass every tenant waits for.
In object storage
To keep an archive in S3, R2 or GCS, run a sync tool beside the archiver; fennecdb has no object-storage client of its own.
rclone sync --local-no-check-updated /backups/archive r2:backups/archive # every few minutes
aws s3 sync /backups/archive s3://backups/archive --delete
# to restore: bring it back, check it, restore it
rclone sync r2:backups/archive ./archive && fennec verify ./archive
fennec restore ./archive before.fennec --to 2026-09-22T10:14:59ZHow a bucket copy stays consistent
fennecdb has no client for a bucket, and S3, R2 and GCS speak only HTTPS, so an archive goes there the way any directory does: a sync tool beside the archiver.
Every file but the segment being written is written once: a segment is
closed and fsynced before the next begins, and an image is written beside
and renamed into place. A sync tool lists the directory and then copies
each file as it stands, so the copy it leaves is the archive up to a
moment, its last segment at most cut in a record's middle, or holding no
write when the copy caught it as it was made. A restore passes over either.
verify reports the bytes cut short, a record's or a start of
the segment's magic, as the last segment ends in a record cut short (N
bytes); a segment caught empty, or with its magic whole and no write
yet, has nothing cut, and verify only counts it among the
segments. Any segment before the last holding no write it refuses by name.
An archiver stopped there goes on from the segment before. A test copies an
archive a file at a time while writes and images go on, and every copy
verifies and restores to a prefix of what the primary wrote.
--delete, or rclone sync's own, carries a prune
over to the copy; without it the copy keeps everything. rclone needs
--local-no-check-updated: by default it refuses a file that
changed while it read it (can't copy - source file is being
updated), which the segment being written does for as long as its
database is busy, so that segment never reaches the bucket; the
deploy/ kit's sync runs with it for the same reason.
An image the archive takes of its own is put together in
consolidating-<change>.fennec, a file of that round's
own, removed once the image is in place. A copy taken meanwhile may hold
one, which no restore reads; --exclude 'consolidating*'
(rclone's, and aws s3 sync's) leaves it out. Where a sync tool
or an indexer holds one as its round ends — on Windows, a file open
elsewhere cannot be deleted — it is removed at the next image, or as the
archiver starts again, said once in its log, and no round ever writes over
it.
Sealed archives and backups
Give a key file and every archive file and backup is encrypted and
tamper-checked. Keep the key apart from the archive: without it nothing can
be restored. fennec key makes the file readable by its owner
alone — mode 0600, and on Windows, where a file takes its
folder's permissions, an ACL granting the account that ran it alone, set
before the key is written — and never writes over a file already there. Run
elevated, Windows makes the Administrators group the file's owner, which may
change its ACL: any administrator can then grant themselves a read, as one
can take any file's ownership anyway.
fennec key /secrets/archive.key # 64 hex digits, readable by its owner alone
fennec archive http://primary:8080 /backups/archive --key-file /secrets/archive.key
fennec verify /backups/archive --key-file /secrets/archive.key
fennec restore /backups/archive before.fennec --to 2026-09-22T10:14:59Z \
--key-file /secrets/archive.key
fennec backup http://primary:8080 today.sealed --key-file /secrets/archive.key
fennec restore today.sealed today.fennec --key-file /secrets/archive.keyHow archives and backups are sealed
A copy in a bucket or on another disk is out of reach of the machine's disk encryption. With a key every file of an archive — its images, its segments, its history, a tenant's presence — and a backup file are sealed: encrypted and authenticated with ChaCha20-Poly1305 (RFC 8439), each file under a key of its own made from the file's random id (HChaCha20, as XChaCha20 makes one) under a key made from yours for that alone (HKDF-Expand, RFC 5869), never from yours itself, a random nonce a frame, and under each frame's tag the tag of the frame before it, its place in the file and whether it is the last. A byte changed, a frame moved or dropped, a frame taken from another file sealed with the same key, two runs of a segment's frames spliced where it was cut and went on, a file cut short where it was sealed whole, or the wrong key, and the file is refused — never read as something else. A segment is sealed a frame for each batch of writes the primary sends, a mebibyte at most, so a crash cuts off at most the frames being written, as it cut a record short before.
Each sealed file also says what it is, under its tags: a segment of writes, an image, the history, a tenant's presence or a backup; the archive it belongs to, by an id the archive made as it began; the tenant whose archive it is; and the change a segment begins at, or an image's change and time. Every read holds a file to its name and to the archive's other files, and refuses by name, with nothing written:
- a segment renamed into another's place — one deleted and the next given its name;
- a file of another archive, or another tenant's, among an archive's —
a tenant's directory, or a line moved aside, copied over another's,
which
restore --tenantsrefuses rather than restore as that tenant's or pass over as gone; - an archive's image given to
fennec restoreas a backup file, or a backup put where an image is.
The plain began file is held to what the images say, and a
tenant's presence is sealed: editing either cannot choose
another line, or mark a tenant gone, without a refusal. To mark a tenant
gone by hand in a sealed archive — a node lost before it said so — leave
its file out of what restore --tenants writes, or move its
lines out of the root first; in a plain archive a line in its
presence (gone <ms>) does it as before.
What a seal does not refuse: a file cut at a frame's edge opens as the
shorter file, as the one a crash leaves does — in the last segment it is
the archive's end, and fennec verify says where that is and
how much is torn; anywhere else it leaves changes no segment holds, which
verify refuses, or names as a gap where an image ends it, as it
names a segment deleted. A tenant's line copied where one database's
archive was restores as that database: fennec restore and
fennec verify of a directory say the tenant its files are
sealed as (sealed as tenant `acme`'s archive), and one
database's archive names no database. An older copy of a whole file, or of a whole
archive or tenant's line, put back opens as what it was, and so does a line
whose later file was deleted — a tenant's presence gone, the
tenant restores as never gone: a seal holds a file to its place, not to its
age. A backup names no database, so one backup file put in another's place
opens as itself. Keep archives and backups in a bucket that keeps versions
or locks objects for as long as you keep them.
An archive is sealed or it is not; a file of the other kind in
it is refused. FENNEC_KEY_FILE names the key file too. Sealing
and opening run at about 400 MB/s on one core, where writing the archive
waits on the network and the disk. The database file itself stays plain:
it is mapped and read where it lies, which encryption would take away —
encrypt the disk it is on (LUKS, FileVault, an encrypted cloud volume).
Files sealed by 0.2.0 and earlier still open, and an archive they began
goes on: before its next write the upgraded archiver seals an image of the
archive's end as files are now — read from the archive, as the hourly
image is, once; a node's archiver seals each tenant's as it starts, beside
its stream — and the write begins a segment sealed so after the older one.
That image is a one-time lag: the archiver spends about 9 seconds a
gigabyte of database on it, one database's archiver before it takes the
primary's next write — the primary waits for its stream at most a second,
on one durable write, and answers the writes after without it until it
catches up. An archive whose end does not restore stops fennec
archive (a node's archiver passes over that tenant), naming why and
the ways out: put right what fennec verify refuses in it, or
begin a new line and keep the old one for restores to earlier moments —
archive into a new directory, or, for a node's tenant, stop the archiver
and move the tenant's directory aside as
<root>/<tenant>.<any name>, and the archiver
begins the new line with the node's image. The older
files say nothing of what they are — their frames are bound to their
places alone — so a frame of another such file sealed with the same key
could be put in one unnoticed, and an older file of any archive sealed
with that key reads wherever it is put: in an archive sealed as files are
now from that image on, an older image or segment after it is refused by
name, and a restore past the upgrade starts from that image or a later
one; one before it — read by a restore to a moment before the upgrade —
or in an archive holding nothing sealed now, as a tenant's line moved
aside before the upgrade, reads as the archive's own; an older image put
in before the upgraded archiver seals that image, standing after the
archive's own last one, is what that image is made from. No archiver puts
an older file where it is refused, so fennec verify or a
restore that refuses one (image-…: an image of change 7, sealed
before version 3, at or past its image of change 7 taken at …, the first
file this archive holds of version 3: no archiver puts it there, so it is
not this archive's) names the way out: move that file out of the
archive's directory, and the archive is read without it. Because the older
files still open, that stays true of every key that ever sealed one,
beside an archive or a backup sealed now. Pruning the older files does not
end it, and neither does restoring a backup and sealing it again with the
same key: copies of the older files may be anywhere. To be rid of it, make
a new key (fennec key), seal with it from then on, and begin a
new archive with it, in a directory or bucket path of its own; read the
older archive and backups with the old key for as long as you keep
them.
A binary from 0.2.0 or earlier refuses a file sealed now (not a
sealed file, not sealed, where the archive is), and an
upgraded archiver seals its next image and segment so at once: upgrade
what reads an archive — fennec restore, fennec
verify, the hosts a restore drill runs on — before what writes
it. A key file others can read is warned of as it is read, by name and
mode; chmod 600 it.
Endpoints
Three endpoints — a --dir node's tenants each have the
first two under /t/<tenant>/, and the node one more, the
stream of every tenant — all on the HTTP listener and all wanting the
replication token, not the --http-token. A tenant's copy is
promoted by the node's admin, POST /_admin/tenants/<t>/promote,
which a router's failover calls (Sharding
reference); /t/<tenant>/_replication/promote is a 404.
GET /_replication?since=<change>&history=<id> the stream a replica is fed
GET /_replication/status role, position, replicas, lag
POST /_replication/promote a replica's promotion
POST /_replication/tenants on a --dir node: every tenant's writes
over one stream (fennec archive --tenants)Which token the endpoints want, and the status
All of them want Authorization: Bearer <replication token>,
not the --http-token: the token is for replicas and for whoever
promotes a file's replica. On a node it promotes nothing: a tenant's copy is
held to the router's lease and to what the router knows of the tenant, which
only the admin's promotion asks. The status of a replica names its primary and says whether it
is connected — until a read of the stream fails, 10 s of silence where
its primary's machine is gone — how many writes it is behind
the primary's disk, as the primary says every 2 s, how far it trails it in
time (lag_ms) and how long ago it last heard from it; a
primary's lists the replicas it is feeding and how far each has been
sent — into its connection, not yet read.
lag_ms is how old, by the primary's clock, the writes the
replica holds were as it read its last message. Read with a write, it is
that write's age: the time from its append on the primary to its read on
the replica. Read with a keep-alive, or the hello, that found the replica
holding the primary's last write, it is that message's own time on its way,
past the quickest way across: a few milliseconds where nothing held it up.
One that found it short of that write counts from its own time once the
replica has read the write it named. Once nothing has come for 3 seconds —
a keep-alive is due every 2 — it is how old they are now. Each age is counted past the quickest way across, so it leaves out
the network's least delay.
The two clocks are compared by the messages that queue behind no write — the hello that opens the stream, read as it comes, and the keep-alives the primary sends once two seconds pass with nothing to send — taking the quickest of them over the last 10 to 20 seconds, and never one alone where an earlier one is at hand. So a difference between the clocks is not taken for lag, and a backlog, however long it lasts, is never taken for a difference between them. A clock stepped on either side is followed within 20 seconds of a stream with nothing to send, at the second keep-alive after a busy spell, or when the replica connects again: at once where the primary's clock went forward, and where it went back at the first keep-alive after the reconnect, or, on a primary written more often than every 2 seconds, which sends none, at the second reconnect. While the stream stays busy, the step reads as lag, or hides as much of it, until then.
A hello or keep-alive held up on its way makes lag_ms read
short by its own delay, for as long as the stream stays busy behind it,
where nothing else is at hand to compare by. A replica's first connection
— each start of the replica, and each open of a following tenant on a node
— has only its hello until its first keep-alive, and a primary written more
often than every 2 seconds sends none: with the hello 800 ms late, a
3-second lag read 2.2 seconds an hour on. Two such messages, each held up,
hide the lesser of their delays: a reconnect whose hello and first
keep-alive both came 800 ms late read a 3-second lag as 2.2 seconds, and
two keep-alives each 1.2 seconds late after a busy spell read a 4-second
lag as 2.8.
Flags
--replication-token <value> | turns replication on; needs --file, and --http to feed replicas. Also read from FENNEC_REPLICATION_TOKEN. 32 bytes at least where --http is past loopback (Security) |
--replica-of http://host:port | follows that primary into --file; needs the token. https://host:port follows it over TLS |
--tls-ca <pem> | the CAs an https:// primary is held to, in place of Mozilla's roots; also a flag of fennec backup and fennec archive |
--promote | opens a replica's file to take writes, forking its history |
--replication-buffer <MiB> | writes kept for replicas that fall behind, and for /_changes; default 64 |
--cdc | keeps the writes for GET /_changes with no replica fed; on already with --replication-token |
Upgrading from 0.2.0: a dropped field's place
Up to 0.2.0, a compact took a dropped field's place out of
every document and told no replica, archive or standby, which then read
every later write of that collection one field off. From 1.0 a compact
keeps the place. While 0.2.0 and 1.0 run beside each other it can still
happen, both ways round:
- A 0.2.0 primary, a newer replica (replicas upgrade first). The
replica notices at the primary's first write of that collection after
such a compact: it refuses the write, logs
the primary's writes moved the fields ofand takes a fresh copy of the primary on its own. Where the dropped field was the last one, the writes after the compact read the same on both, and the replica notices at the primary's nextalter collectionorcreate indexof that collection instead. An archive does not notice: a restore across that compact reads the writes after it one field off. - A 1.0 primary, a 0.2.0 replica or standby, as a node pair
leaves it while one node is upgraded and the other is not. A field
dropped on the primary keeps its place, but the 0.2.0 standby's own
--auto-compacttakes it out of its documents, and it reads every later write of that collection one field late, with nothing to tell it. After a failover those values are the tenant's. The primary cannot tell an older replica from its connection, so nothing refuses the drop.
- Drop no field until every node and every replica runs 1.0.
If a drop on a 1.0 primary cannot wait, run every 0.2.0 replica and
standby with
--auto-compact off, and nocompacton one, until it is upgraded. - Before upgrading the first replica, if a field was ever
dropped on the primary, the replicas may already hold rows read one
field off. Run
compacton the primary and seed every replica and standby again from afennec backupof it taken after; begin an archive again in a directory of its own, keeping the old one for restores to a moment before. - Promote no replica that logged
moved the fields ofuntil it has caught up again.
What it does not do
- No synchronous replication. A write is answered when it is on the primary's disk, never when a replica has it.
- No automatic failover of a single file. Promotion is by hand, and so is keeping the old primary from taking writes. A router's tenants can have one: nodes that take its lease stop writing when they lose it, and are failed over after (Automatic failover).
- No object-storage client. An archive is a directory; a sync tool carries it to S3, R2 or GCS (In object storage).