Write-ahead log
The WAL makes a write durable before the memtable that holds it is flushed. The engine keeps one open segment at a time; rotation seals it and opens the next. Recovery replays the segments that are not yet covered by SSTables (source: src/storage/wal.rs, WriteAheadLog; src/storage/mod.rs, rotate_memtable_locked, StorageEngine::open).
Segment files
Segments live in wal_dir and are named wal-<sequence>.log, where the number is the engine's sequence counter when the segment was opened. Because sequences are assigned under the WAL lock, segment i holds sequences in [start(i), start(i+1)). A legacy current.wal is also recognized (source: src/storage/mod.rs, rotate_memtable_locked, StorageEngine::open).
File format
offset 0 8
+------------------+------------+------------------------------------------------
| magic "WLOG" | version | entry | entry | ... | [zero-filled preallocation]
| u32 LE 0x574C4F47| u32 LE |
+------------------+------------+------------------------------------------------
entry
+-------------+-------------+---------------+-----------+-------------+
| crc32 u32LE | key_len u32 | value_len u32 | key bytes | value bytes |
+-------------+-------------+---------------+-----------+-------------+
CRC covers key_len || value_len || key || value (HEADER_SIZE = 12)
| Version | Meaning |
|---|---|
| 1 | Plain keys and values |
| 2 | Values encrypted with the legacy master-direct cipher |
| 3 | Keyring sealing: keys and values sealed separately, except the commit-marker key, which stays plaintext |
| Limit | Value |
|---|---|
MAX_KEY_SIZE |
64 MiB |
MAX_VALUE_SIZE |
1 GiB |
(source: src/storage/wal.rs, WAL_MAGIC, WalEntry::encode, HEADER_SIZE, MAX_KEY_SIZE, MAX_VALUE_SIZE)
The CRC is crc32fast over the frame as written, so under version 3 it covers the sealed bytes. A data entry's key is the encoded InternalKey and its value the bincode StorageValue (see Keys and namespaces) (source: src/storage/wal.rs, WalEntry; src/storage/mod.rs, write_batch_local_opts).
The engine streams frames with append_internal_unflushed instead of building a WalEntry; streaming_append_matches_entry_encoder (tests/wal_frame_equivalence.rs) checks that both produce the same bytes. Tested also by test_wal_entry_encode_decode and test_wal_entry_crc_verification (src/storage/wal.rs, tests).
Commit markers
Every batch ends with a commit marker entry:
| Field | Value |
|---|---|
| Key |
WAL_COMMIT_MAGIC, 16 bytes 00 FF 'S' 'Y' 'N' 'C' 'M' 'T' FE 00 'W' 'A' 'L' 01 FF 00
|
| Value | Number of data entries in the batch, u32 LE (sealed under versions 2 and 3) |
A new segment starts with an initial marker of count 0, then an fsync of the file and of the directory (source: src/storage/wal.rs, WAL_COMMIT_MAGIC, append_commit_marker_opts, parse_commit_marker, WriteAheadLog::open). Recovery applies a run of entries only when it is followed by a marker whose count equals the number of buffered entries (Crash recovery). Tested by commit_marker_is_not_mistaken_for_data and committed_multiwrite_batch_recovers_atomically (src/storage/mod.rs, crash_atomicity_tests).
Version 3 sealing
Under the keyring, each field is sealed with ring.seal(Purpose::WalEntry, None, file_id_for(path), field) where field is 0 for the key and 1 for the value. The "offset" slot of the associated data therefore holds the field number, not a byte offset; it binds the field to its segment name and its role but not to its position in the file (source: src/storage/wal.rs, seal_key, seal_value; src/storage/keyring.rs, file_id_for). See Encryption at rest internals.
Preallocation and logical end
The writer extends the file in zero-filled chunks of 4 MiB (WAL_PREALLOC_CHUNK) ahead of the data (reserve). An all-zero entry header marks a clean end of the log: on reopen logical_end finds it and appends from there. seal(), called at rotation, fsyncs and trims the unused preallocated tail (source: src/storage/wal.rs, WAL_PREALLOC_CHUNK, reserve, logical_end, seal). Tested by test_preallocated_tail_is_a_clean_end_and_reopen_appends_after_entries and test_appends_past_one_chunk_extend_the_preallocation (src/storage/wal.rs, tests).
sync_dir fsyncs a directory; it ignores errors on Windows and Unsupported/InvalidInput errors elsewhere (source: src/storage/wal.rs, sync_dir).
Sync modes
wal_sync_mode maps to a DurabilityLevel:
wal_sync_mode |
DurabilityLevel |
When a commit round fsyncs |
|---|---|---|
every_write (default) |
FsyncEveryCommit |
Every round with pending data |
periodic |
FsyncBatched { batch_size: 100, timeout_ms: 10 } |
Only when at least 100 entries were appended since the last fsync, or at least 10 ms have passed since it |
no_sync |
Async |
Never; data reaches the OS when the batch's buffer is flushed |
(source: src/storage/wal.rs, DurabilityLevel, prepare_group_sync; src/config.rs, to_storage_config)
Under all three modes a synced batch's data entries and marker are handed to the OS together, in one buffer flush, when the marker is appended (source: src/storage/wal.rs, append_commit_marker_opts). The difference is only whether an fsync follows.
periodic has no timer of its own. The check runs only when a committer starts a commit round, so after the last write of a burst the tail stays unsynced until a later qualifying round, a rotation (seal) or a shutdown. WriteAheadLog::maybe_sync, which would sync on the timeout, has no caller in the engine (source: src/storage/wal.rs, prepare_group_sync, maybe_sync; src/storage/mod.rs, group_fsync). Not verified: no test measures the loss window of periodic after a power failure or OS crash, which matches the guide's note in How it works.
Group commit
Durable writers share fsyncs through GroupCommitCoordinator.
writer A ----push tx----> pending [A] leader_active = true (A leads)
writer B ----push tx----> pending [A, B]
writer C ----push tx----> pending [A, B, C]
A: group_commit_lead
take pending -> [A, B, C]
group_fsync:
WAL mutex: prepare_group_sync (flush buffer, clone file handle)
no mutex: sync_data on the clone
hand Lead to the next waiting committer, if any
send Durable to B, C (staggered by 100 spin_loop hints)
| Mode | Selected by | Behaviour |
|---|---|---|
| Inline (default) |
SYNERGYDB_GROUP_COMMIT_INLINE unset or not 0/false
|
The first committer to find leader_active false leads the fsync on its own thread; the others wait on a bounded crossbeam channel |
| Worker | SYNERGYDB_GROUP_COMMIT_INLINE=0 |
The synergydb-fsync thread drains the queue twice, waits up to SYNERGYDB_GROUP_COMMIT_WINDOW_US (default 0) to coalesce, at most 64 committers per round, with a 50 ms safety-net wait |
(source: src/storage/mod.rs, GroupCommitCoordinator, join_group_commit, group_commit_lead, group_fsync, group_commit_inline)
The fsync runs outside the WAL mutex, so new batches can append while a round is in flight. If the fsync fails, mark_sync_pending restores the pending flag and waiters receive an error (source: src/storage/mod.rs, group_fsync; src/storage/wal.rs, mark_sync_pending). After each commit a thread spins a random 0 to 511 spin_loop hints; the code calls this a convoy break (source: src/storage/mod.rs, join_group_commit).
put_unsynced and write_batch_unsynced append without flushing the buffer. group_sync then flushes the buffer, returns at once under no_sync, and otherwise joins a commit round, so a caller can make many writes durable with one fsync (source: src/storage/mod.rs, put_unsynced, group_sync, commits_fsync).
Tested by concurrent_durable_commits_all_complete_and_survive_reopen (16 threads x 150 commits, src/storage/mod.rs, group_commit_tests). Worker mode has no dedicated test. Not verified: worker mode and SYNERGYDB_GROUP_COMMIT_WINDOW_US.
Rollback
If an append fails partway through a batch, rollback_to(offset) opens a fresh handle, truncates the file to the offset before the batch (discarding anything still in the writer's buffer) and fsyncs (source: src/storage/wal.rs, rollback_to; src/storage/mod.rs, write_batch_local_opts). Tested by crash_close_discards_buffered_wal_bytes and test_wal_truncate_at (src/storage/wal.rs, tests).
Retention and reclamation
Old segments are deleted by cleanup_old_wal_files(config, max_flushed_seq, backup_retain_seq), which runs after each flush and once at open:
- If
wal_retention_bytesis None, do nothing. -
floor = min(max_flushed_seq, backup_retain_seq). - Walk segments oldest first. While the total size is above the target and
floor >= start(next) - 1(every sequence in the segment is flushed and not needed by a backup), delete the segment. - Never delete the newest segment.
(source: src/storage/mod.rs, cleanup_old_wal_files)
The target is a size goal, not a cap: segments that still hold unflushed data or data a backup needs stay on disk. The engine logs a warning at open when one segment exceeds max(4 x memtable_size, 256 MB) or the total exceeds twice the target (source: src/storage/mod.rs, StorageEngine::open). The guide's operational view is in Operations.
Backup hold
WAL_BACKUP_HOLD in data_dir stores, as decimal text, the lowest sequence a backup chain still needs. It is written as a temporary file, fsynced and renamed. A missing file means no hold (u64::MAX); an unreadable or unparsable file makes open fail. create_backup and create_incremental_backup set it to the backup's end_sequence (source: src/storage/mod.rs, read_wal_backup_hold, write_wal_backup_hold, set_wal_backup_retain_seq; src/storage/backup.rs, create_backup). Tested by backup_wal_hold_survives_restart and corrupt_backup_wal_hold_refuses_to_open (src/storage/mod.rs, wal_backup_hold_tests) and wal_retention_reclaims_but_keeps_backup_chain (src/storage/backup.rs, tests).
Tested by reopen_skips_fully_flushed_segments and overwrite_heavy_load_keeps_wal_near_retention_target (src/storage/mod.rs, wal_replay_bounded_tests).
Not wired
Not wired: CheckpointManager (magic CHKP, interval 10,000 entries) and WalManager are defined in src/storage/wal.rs and have no caller outside that file. WriteAheadLog::maybe_sync has no engine caller (source: src/storage/wal.rs, CheckpointManager, WalManager, maybe_sync).