Memory and file cost of thousands of LSM trees per node (phase 0 measurement)¶
The design keeps one LSM tree per key range (Raft group) and 1–4 thousand ranges per node. This is what one phase-0 tree costs, measured, and what that implies.
Measurement¶
OLIVIADB_RANGES=<R> cargo bench -p oliviadb-core --bench lsm_memory: R trees in one process on
the local file system sharing one Node (block cache 64 MiB, at most 1024 open table
files, memtable budget 256 MiB), each with 2000 keys (~40 B keys, 100 B values) flushed to
a table plus 200 keys in its memtable; memtable quota 1 MiB per tree. Apple M3 Max, 14
cores, 36 GiB, internal SSD, macOS 27.2.
| R | RSS, empty tree | RSS, loaded tree | memtable + index per tree | shared block cache | open table files | descriptors per tree | create a tree | reopen |
|---|---|---|---|---|---|---|---|---|
| 1000 | 1.3 KiB | 77.6 KiB | 30.7 KiB | 16.9 MiB | 1000 | 3.00 | 17.8 ms | 188 µs |
| 4000 | 1.0 KiB | 74.7 KiB | 30.7 KiB | 64.0 MiB (cap) | 1024 (cap) | 2.26 | 19.3 ms | 364 µs |
(2026-11-30, two-level table indexes. The day before, with whole indexes resident: 78.1 / 75.7 KiB RSS and 31.9 KiB memtable + index per tree; these trees have one small table each, so the index part was small to begin with.)
One large table (cargo test --release -p oliviadb-core index_memory -- --ignored): 205 MiB,
12 879 data blocks of 16 KiB, 193 index partitions; resident index 19 KiB in format 3
against 1 207 KiB for the same table in format 2 (one index block held whole).
(Phase 0, before the shared node: every tree held its own cache and its whole-table filter, and kept every table file open: 3 descriptors per tree regardless of R.)
What stays per tree now: the memtable (counted bytes; ~34 KiB here for 200 keys) and the
top-level table indexes (one entry per 4 KiB index partition, i.e. per ~60 data blocks).
Index partitions, blocks and Bloom filter partitions live in the node's cache (filters with a
higher CLOCK priority, so random reads do not evict them); table files are opened on
demand through the node's file cache. RSS above the accounting (≈ 45 KiB per tree in
these runs) is the tree's share of the block cache plus allocator overhead of the
memtable's per-entry allocations. Creating a tree costs four syncs (log, manifest,
CURRENT, directory), ~18 ms on this disk.
Model¶
Per range of S bytes with entries of E bytes (N = S / E keys), 16 KiB blocks:
resident per range ≈ Q (memtable quota) + (S / 16 KiB) · (key + 30 B) / 4 KiB · (key + 64 B) (top-level index) + ~1 KiB
shared ≈ block cache (data blocks, index partitions, 1.25 B/key filter partitions), sized for the node
descriptors ≈ 2 per range (log, manifest) + at most the file-cache cap (table files)
Example, 4000 ranges of 256 MiB (1 TiB per node), E = 150 B, keys ~40 B, Q = 1 MiB:
| item | per range | per node |
|---|---|---|
| top-level table indexes (16 k blocks, ~250 partitions) | ~25 KiB | ~100 MiB |
| index partitions | (in the cache) | 4.6 GiB if all were cached; the cache holds the hot ones |
| memtable quota | 1 MiB | 4 GiB |
| Bloom filters | (in the cache) | 8.7 GiB if all were cached; the cache holds the hot ones |
| descriptors | 2 | 8 000 + file-cache cap |
Consequences (open work)¶
- Indexes. Done: two-level indexes (format 3) page the index partitions through the node cache; resident index per range drops ~60-fold (~6 GiB to ~100 MiB per node in the example). The memtable quota is now the dominant resident item.
- Memtable quota and budget. The node budget triggers early flushes locally; with replication the decision must become a logged command (ForceFlush), and the quota a replicated parameter, as the design specifies.
- Descriptors. Two per range remain (log and manifest); the design's node-wide multiplexed log removes the log descriptor per range.
- Creation latency. Four syncs per range creation; split/merge and bulk creation must batch directory syncs.
- CPU. Compaction write amplification and per-range scheduling overhead are still to be measured on traces (with the shared compaction scheduler).