Storage engine overview

This chapter describes the storage engine in src/storage/ as it stands at main @ 8a689c5c (2026-11-30). Every wire (PostgreSQL, MySQL, MongoDB, Redis, Bolt and the rest) writes into one StorageEngine, which is a log-structured merge tree (LSM tree) with a write-ahead log (WAL), sorted string tables (SSTables), a manifest and background compaction (source: src/storage/mod.rs, StorageEngine). The operator view of the same settings is in How it works and the configuration reference.

Components

            write_batch / put / delete                    get / scan
                      |                                       |
                      v                                       v
 +--------------------------------------+     +------------------------------+
 | WAL  wal-<seq>.log  (one open file)  |     | active memtable              |
 |  data entries + commit marker        |     | immutable memtables (<= 4)   |
 +--------------------------------------+     | SSTables L0 .. L6            |
                      |                       +------------------------------+
                      v                                       ^
 +--------------------------------------+                     |
 | active memtable (SkipList + index)   | ---- rotate ---->  immutable queue
 +--------------------------------------+                     |
                                               background flush (100 ms tick)
                                                               v
                                   sst-<id>.sst at L0, recorded in MANIFEST.log
                                                               |
                                    compaction workers (2) merge L(n) -> L(n+1)
Component Type / file What it holds
Active memtable MemTable in Arc<RwLock<Arc<MemTable>>> Recent writes, newest version per key (more while a snapshot is open)
Immutable memtables immutable_memtables Rotated memtables waiting for flush
WAL WriteAheadLog behind a parking_lot mutex, files wal-<seq>.log in wal_dir Every write before it is acknowledged
SSTables levels: Vec<Vec<Arc<SSTableReader>>>, files sst-<16 hex>.sst in data_dir Immutable sorted runs, 7 levels by default
Manifest MANIFEST + MANIFEST.log in data_dir Which SSTables exist at which level, the sequence counter
Compaction CompactionManager, 2 worker threads Merges levels, drops overwritten versions and tombstones
Directory lock LOCK (+ informational LOCK.pid) One engine per data directory
Backup hold WAL_BACKUP_HOLD Lowest sequence a backup chain still needs from the WAL
Key check ENCRYPTION Proof of the at-rest key, when encryption is on

(source: src/storage/mod.rs, StorageEngine, acquire_data_dir_lock, write_wal_backup_hold; src/storage/at_rest.rs, KEY_CHECK_FILE)

Configuration

StorageConfig carries the engine settings. The server builds it from the [storage] section with StorageConfigFile::to_storage_config. Three fields have no config-file key and always take their code defaults: max_memtables, num_levels and level_ratio (source: src/config.rs, StorageConfigFile::to_storage_config).

Field StorageConfig::default() Server default (src/config.rs) Effect
data_dir / wal_dir ./data / ./wal from config SSTables and manifest / WAL segments
memtable_size 64 MiB "64MB" Rotation threshold of the active memtable
block_size 4096 4096 Target SSTable data block size
compression Lz4 lz4 (none, zstd accepted) Block codec
wal_sync_mode EveryWrite every_write (periodic, no_sync accepted) See WAL
max_memtables 4 not configurable Immutable queue length that forces a synchronous flush
num_levels 7 not configurable LSM depth
level_ratio 10 not configurable Size ratio between levels
encryption_enabled / encryption_key_file false / None from config See encryption at rest
wal_retention_bytes None (no reclamation) 1 GB, unlimited/none/off disables WAL size target
compaction_size_amp_ratio 0.5 0.5 Size-amplification trigger, 0 disables

(source: src/storage/mod.rs, StorageConfig::default; src/config.rs, StorageConfigFile::to_storage_config, wal_retention_target, DEFAULT_WAL_RETENTION_BYTES)

Note that a StorageConfig built in code with ..Default::default() (tests, the backup and restore CLI paths after a restore) has no WAL retention target, while a server started from a config file defaults to 1 GB (source: src/storage/mod.rs, StorageConfig::default; src/config.rs, wal_retention_target).

Environment variables read by the engine itself:

Variable Default Read by
SYNERGYDB_WAL_RETENTION_BYTES 1 GB src/config.rs, wal_retention_target
SYNERGYDB_COMPACTION_SIZE_AMP 0.5 src/config.rs, to_storage_config
SYNERGYDB_GROUP_COMMIT_INLINE on (0/false selects the fsync worker) src/storage/mod.rs, GroupCommitCoordinator
SYNERGYDB_GROUP_COMMIT_WINDOW_US 0 (worker mode only) src/storage/mod.rs, GroupCommitCoordinator
SYNERGYDB_STALL_DUMP off (1 starts a 5 s stall watchdog) src/storage/mod.rs, StorageEngine::open
SYNERGYDB_BLOCK_CACHE_MB 1/8 of memory, 256..2048 MB src/storage/sst.rs, block_cache
SYNERGYDB_COLUMN_CACHE_MB 512 src/storage/columnar.rs, cache
SYNERGYDB_COLUMNAR_MIN_SST_BYTES 4 MiB, 0 disables src/storage/mod.rs, columnar_min_sst_bytes
SYNERGYDB_SCAN_WINDOW_MB 16 src/storage/mod.rs, scan_window_bytes
SYNERGYDB_ENCRYPTION_KEY, SYNERGYDB_ENCRYPTION_RETIRED_KEYS unset src/storage/at_rest.rs, load_master_keys

Write path

write_batch first asks the optional ReplicationHook (cluster mode) whether to apply locally, then calls write_batch_local_opts (source: src/storage/mod.rs, write_batch, ReplicationDecision).

  1. Each value is encoded as a bincode StorageValue outside any lock. A tombstone also drops the key's cached serial block (source: src/storage/mod.rs, write_batch_local_opts).
  2. Under the WAL mutex, every entry gets a sequence number from next_sequence and is appended with append_internal_unflushed. A commit marker carrying the entry count follows. If any append fails the WAL is rolled back to its length before the batch (rollback_to) (source: src/storage/mod.rs, write_batch_local_opts; src/storage/wal.rs, append_commit_marker_opts).
  3. The engine counts the batch as in flight, releases the WAL lock and inserts every entry into the active memtable under the memtable read lock (source: src/storage/mod.rs, write_batch_local_opts, wal_inserts_in_flight).
  4. If the memtable is over memtable_size or the WAL file is over the segment cap, the writer takes the WAL for rotation (waiting for in-flight inserts to drain), takes the memtable write lock, re-checks and rotates (source: src/storage/mod.rs, lock_wal_for_rotation, rotate_memtable_locked).
  5. Write watchers are notified. A durable write then joins group commit and returns once its WAL bytes are fsynced (source: src/storage/mod.rs, join_group_commit).

A batch is all-or-nothing on recovery because replay applies a run of entries only when its commit marker is present and its count matches (see Crash recovery).

Two paths skip the WAL: ingest_bulk writes a new L0 SSTable directly, and replace_range_bulk retires whole SSTables in one manifest edit. Both refuse on a node with a replication hook, and replace_range_bulk also refuses while any snapshot transaction is open (source: src/storage/mod.rs, ingest_bulk_prefixed, replace_range_bulk, bulk_ingest_available). Tested by replace_range_retires_inner_files_and_keeps_neighbours and add_primary_key_on_loaded_table_survives_reopen (tests/replace_range_bulk.rs) and the bulk_ingest_tests module in src/storage/mod.rs.

Read path

get looks in the active memtable and returns if it finds a version there. Otherwise it consults every immutable memtable and returns the highest-sequence version found among them. Otherwise it consults every SSTable, in any order, and keeps the version with the highest sequence. An SSTable is skipped when the key is above its max_key, below its true minimum key, or rejected by its user-key bloom filter. The bloom hash is computed once per get (source: src/storage/mod.rs, get, consider_sstable; docs/status/2026-11-24-get-bloom-hash-once.md).

scan merges all sources and, for each user key, keeps the version with the highest sequence that passes the optional timestamp filter. A winning tombstone yields no row. If a source turns out not to be in user-key order (a user key that is a byte prefix of another can interleave in encoded order), scan falls back to the order-independent scan_versioned, and scan_visit sends ScanRow::Restart before replaying every row (source: src/storage/mod.rs, scan, scan_visit, scan_versioned). Tested by merge_scan_equals_map_scan_and_get_on_random_histories (scan_merge_tests) and the oracle tests in src/storage/property_oracle_tests.rs.

Large ranges are merged in windows and large SSTables can be read through column projections; both are described in Caching and memory and Columnar projections, time series and Parquet.

Background threads

Thread Started by Work
Flush loop StorageEngine::open Every 100 ms: flush the oldest immutable memtable, schedule compaction, run the size-amplification check every 50 ticks, reconcile levels with the manifest
synergydb-fsync StorageEngine::open Group-commit fsyncs in worker mode; idles in the default inline mode
Compaction workers (2) CompactionManager::start(2) Execute queued compaction jobs under the compaction lock
Stall watchdog SYNERGYDB_STALL_DUMP=1 Dumps engine state after a 5 s stall

(source: src/storage/mod.rs, background_flush_loop, StorageEngine::open; src/storage/compaction.rs, CompactionManager::start)

Drop for StorageEngine calls shutdown() (unless the engine is in crash-simulation mode) and joins the flush thread, the fsync worker and the watchdog, so no background thread writes into the data directory after the engine is gone (source: src/storage/mod.rs, impl Drop for StorageEngine). Tested by the drop_joins_flush_thread_tests module.

Chapter map

  1. Keys and namespaces
  2. Memtable
  3. Write-ahead log
  4. SSTable format
  5. Manifest
  6. Crash recovery
  7. Compaction
  8. Caching and memory
  9. Columnar projections, time series and Parquet
  10. Backup and restore internals
  11. Encryption at rest internals