Memtable

The memtable is the in-memory write buffer. Every write lands in the active memtable after its WAL append; a full memtable is rotated into an immutable queue and flushed to a level-0 SSTable by the background flush loop (source: src/storage/memtable.rs, module doc; src/storage/mod.rs, rotate_memtable_locked, background_flush_loop).

Data structure

MemTable
 +-- data:  crossbeam_skiplist::base::SkipList<InternalKey, Arc<[u8]>>
 |          ordered by (user_key, sequence, op_type); value = encoded StorageValue
 +-- point: PointIndex  (32 shards, open addressing)
 |          user_key -> LatestSlot { sequence, op_type, value: Arc<[u8]> }
 +-- size:  AtomicUsize  (sum of user_key + value + 16 per entry)
 +-- max_size
Part Detail
Skip list crossbeam_skiplist::base::SkipList with crossbeam_epoch guards. Value buffers are Arc<[u8]>, shared with the point index without copying
Point index 32 shards (POINT_SHARDS). Each shard is a linear-probing hash table keyed by a 64-bit hash, kept at most 3/4 full. User keys up to 30 bytes (POINT_KEY_INLINE) are stored inline in the bucket. Keys are never removed; the slot holds the newest version, tombstones included
Size accounting user_key.len() + value.len() + 16 per entry, added on insert and subtracted when an older version is removed
Flush test should_flush() compares size with max_size (memtable_size)

(source: src/storage/memtable.rs, MemTable, PointIndex, PointKey, LatestSlot, POINT_SHARDS, POINT_KEY_INLINE, insert_inner, should_flush)

Insert

insert (used by WAL replay and unit tests) and insert_versioned (used by live writes) both call insert_inner(key, value, keep_old):

  1. Walk the skip list over every version of the user key.
  2. If a version with a higher sequence is already present (a racing writer got there first), drop the incoming version.
  3. Otherwise insert the new version into the skip list, then upsert the point index, which also keeps the highest sequence.
  4. Unless keep_old is set, remove every older version and subtract its size.

The new version is published before the old ones are removed, so a concurrent reader always finds at least one version of the key in this memtable; the code comment records that remove-then-insert had let an UPDATE miss its row (source: src/storage/memtable.rs, insert_inner).

insert_versioned(key, value, commit_ts, watermark) sets keep_old = commit_ts > watermark. The watermark is the engine's min_snapshot_ts, which is u64::MAX when no snapshot transaction is live, so in the common case only the newest version of each key is kept. While a snapshot that predates the write is live, older versions stay so a snapshot read can still find them (source: src/storage/memtable.rs, insert_versioned; src/storage/mod.rs, min_snapshot_ts, write_batch_local_opts).

Tested by newer_write_replaces_older_and_size_follows, a_stale_write_never_displaces_a_newer_one, retained_versions_leave_newest_in_index, index_agrees_with_skiplist_across_overwrites_and_deletes and concurrent_writers_leave_highest_sequence (src/storage/memtable.rs, tests), and by hot_key_increments_survive_memtable_rotation and lock_free_point_reads_never_miss_or_regress (tests/memtable_rotation_race.rs).

Reads

Method Use
with_latest, get_latest_shared Point read through the point index, no copy of the value
with_latest_many Batched point reads; prefetches 4 probes ahead (AHEAD) on x86_64
get_range All versions of one user key, range [{k,0,0} ..= {k,MAX,255}]
latest, latest_as_of Newest version per user key in a range, borrowed under an epoch guard; latest_as_of filters by snapshot timestamp
iter Owned iterator for flush; pins an epoch guard per step

(source: src/storage/memtable.rs, with_latest, with_latest_many, get_range, latest, latest_as_of, iter, OwnedIter)

get_range once used {k, 0, 255} as its lower bound, which sorts above a put at sequence 0, so the first write on a fresh engine was invisible to point reads. The bound is now {k, 0, 0} (source: src/storage/memtable.rs, get_range). Tested by first_write_is_visible_to_point_get (src/storage/mod.rs, sequence_zero_point_get_tests). The module doc of src/storage/property_oracle_tests.rs still describes the sequence-0 quirk and works around it with a warm-up write; that description is out of date.

Tested by latest_yields_the_newest_version_per_key_within_bounds, borrowed_rows_outlive_their_removal_while_pinned and batched_lookup_matches_single_lookups_in_order (src/storage/memtable.rs, tests).

Rotation

A writer rotates when, after its insert, the active memtable is over memtable_size or the WAL file is at least wal_segment_cap bytes. wal_segment_cap is max(4 x memtable_size, 1 MiB), capped at max(wal_retention_bytes, 1 MiB) when a retention target is set (source: src/storage/mod.rs, write_batch_local_opts, wal_segment_cap).

lock_wal_for_rotation takes the WAL mutex and waits until no insert is in flight (wal_inserts_in_flight is zero), so every entry in the closing WAL segment is also in the memtable being rotated. rotate_memtable_locked then:

  1. swaps a fresh memtable in and pushes the old one onto immutable_memtables;
  2. seals the WAL file (fsync, trim the zero-filled preallocated tail) and opens wal-<current sequence>.log;
  3. if the immutable queue holds max_memtables (4) or more, flushes the oldest one synchronously on the writer's thread.

(source: src/storage/mod.rs, lock_wal_for_rotation, rotate_memtable_locked; src/storage/wal.rs, seal)

Step 3 is the back-pressure: when flushing falls behind, writers pay for the flush.

Flush

flush_memtable_to_sst writes the memtable to sst-<file number as 16 hex digits>.sst, with the number from manifest.next_file_number(). It builds the file with SSTableWriter and its user-key bloom filter, finishes it (fsync of the file and of the directory), pushes the reader into levels[0], records AddSSTable in the manifest log, raises max_flushed_seq, and only then removes the memtable from the immutable queue. Removing it last means a reader never sees the data in neither place; the code dates this ordering to a 2026-06-11 review fix (source: src/storage/mod.rs, flush_memtable_to_sst, flush_oldest_memtable_locked).

After a flush, cleanup_old_wal_files may reclaim WAL segments (see WAL).

The background loop flushes at most one immutable memtable per 100 ms tick, under flush_lock. StorageEngine::flush() rotates the active memtable and drains the whole queue under the same lock (source: src/storage/mod.rs, background_flush_loop, flush).

Tested by reads_never_go_backwards_while_flush_is_in_flight, flushed_and_compacted_sstables_reopen_intact and writes_across_many_wal_rotations_survive_reopen (src/storage/mod.rs, durability_review_fix_tests).

Not wired

Not wired: ImmutableMemTable (a sorted Vec copy of a memtable, from_memtable) is defined in src/storage/memtable.rs and has no caller outside that file. Rotated memtables are kept as Arc<MemTable> instead (source: src/storage/memtable.rs, ImmutableMemTable; src/storage/mod.rs, immutable_memtables).