Menu
#Durability
The database lives in memory, and an append-only log on disk is how it survives a restart. At start the log is replayed. Each database has its own log, <FERRITE_DATA>/<name>.log, and users are in <FERRITE_DATA>/_users.log. The default data directory is ferrite_data in the directory the server runs in. The service uses /var/lib/ferrite.
#The log
- A file starts with 8 bytes of magic,
FERRITE1, then records. - A record is the entry length (
u32), the CRC-32 of that length, the entry, and the CRC-32 of the entry. All numbers are little endian. - An entry is an op byte, the collection name, and the fields of that op. Values use the same binary encoding as the binary wire.
- The length has its own checksum so that a flipped bit in a length cannot make good data look like a record cut short.
- A transaction commit, an
insertwithdocs, and$outand$mergeare written as one record that holds all their entries, so after a crash the log has all of them or none.
Logging is the operation, not the result: an update is stored as its filter and update and replays the same way.
A change is applied in memory first and logged second, so a failed change is never logged.
#Sync policies
How the log reaches the disk is set by the second command-line argument or FERRITE_SYNC (the argument wins if both are given).
| value | behaviour |
|---|---|
| a number of milliseconds | fsync at that interval. 100 is the default |
group |
a write is answered only once it is on disk; writes from different clients share one fsync |
0 |
fsync on every write |
ferrite_db 127.0.0.1:7878 group
FERRITE_SYNC=250 ferrite_db- With an interval, a crash of the process (even
kill -9) loses nothing, because the data is already in the operating system. A power cut or kernel crash can lose the last interval of writes, 100 ms by default. - With
groupno acknowledged write is lost, and throughput rises with the number of clients writing at once. In group mode a reader can briefly see a write that is not on disk yet. - With
0the guarantee is the same asgroup, but writes run at about 1.7k a second however many clients there are. _users.logis synced on every write whatever the policy.SIGINTandSIGTERMsync every log before exiting.
At start the server prints opening <dir>, sync <value>.
#Compaction
The log rewrites itself once it is mostly out-of-date entries, so it does not grow forever. The new file is built from the whole database in memory, synced, and swapped in, keeping the permissions of the old one. A new log is created with mode 0600.
An admin can run it by hand:
{"cmd":"compact"}A compaction that fails is not tried again until the log has grown by a minimum number of entries. A follower never compacts, see replication.
#Crash behaviour
At start the log is read a record at a time and each entry is applied.
- A torn tail. If fewer than 8 bytes are left, or a valid length reaches past the end of the file, the last write was cut short. The end is cut off after every earlier record has been read and found good, and the server prints a
warning: ... dropped N bytes at the endline. - Corruption. A bad length checksum or a bad entry checksum anywhere else is corruption. The open is refused and the file is left as it was. Every flipped bit is caught.
- Recovery output. One line per database:
recovered <name>: N log entries replayed, M documents, T ms. - Fail-stop. If a write or an fsync fails, the memory has a change the log does not. The log then refuses every write, and so does the database, until the server is restarted and reads what is really on disk. Reads still work. After a failed fsync the operating system may have dropped the data, so retrying would not be safe.
There is no file locking, so two processes on one log would corrupt it.
#Older logs
Logs written before the binary format (JSON lines) do not open at all. The error says the file is from an older version and the file is left alone. There is no conversion.
An old single ferrite.log (or the file FERRITE_LOG names) is moved into the data directory on the first start as the database default, with its users, and kept as ferrite.log.old.
#Backups
Copying the files while the server runs is not something the docs can promise is safe. Stop the server, or use a follower, whose logs are byte-identical to the leader's.