Merging Only What Overlaps
published on November 23rd, 2026
Every LSM-tree writes your data more than once. It has to, that’s how it keeps reads fast and gives deleted space back. The question is how much it rewrites each time, and whether it can stop at the parts that actually changed.
A write never changes anything in place. It lands in Level 0, the memtable level, and a memtable is two things combined, a sorted index in memory and a log on disk. The write goes to the log first, so a crash can’t lose it, then into the index. When the memtable is full, the index is written out as a sorted file, a sorted run, its log is removed and a fresh memtable takes over. Sorted runs live in the levels below, Level 1 for fresh ones, then 2, 3 and so on, each larger than the last.
That makes writing fast, but files pile up, and a read may have to check several of them for the newest copy of a key. Compaction is the background work that merges sorted runs down a level, dropping the old copies nobody can read anymore. Too eager and the disk spends all day rewriting data you already wrote, too lazy and reads slow down while deleted data keeps its space.
TidesDB’s compaction builds on Spooky (Dayan et al., PVLDB 2022). In TidesDB it all starts with one memtable shared by every column family. Each key carries its family’s index in front, so the sorted memtable keeps each family’s keys together in one contiguous slot. The commit that fills it seals it onto a queue, still readable, and a fresh one takes over on a new log.
Flush threads each grab the oldest sealed memtable nobody else has taken and build side by side, cutting it into one sorted run per column family for Level 1. They install in the order they grabbed them, each memtable’s runs in a single commit, then its log can go and compaction gets a nudge.
From there every column family grows its own levels below the shared Level 0, starting with just Level 1, sharing only the merge threads.
TidesDB sets a fixed size only for the largest level and sizes the others from what it actually holds, dynamic capacity adaptation in the paper’s terms, adding a level when the largest fills and giving one back when your data goes away.
The hard part is merging into the largest level, and the paper names the two usual answers. A Full Merge compacts a whole level at once, so until it finishes the disk needs room for the old copy and the new, roughly twice your data. A Partial Merge compacts small groups of files, but they rarely line up, so each merge rewrites neighbouring data it didn’t need to. Spooky lines them up. TidesDB picks a dividing level, two above the largest by default, and every merge into it, a dividing merge, cuts its output at the key boundaries the largest level’s files already have. Each stretch of keys between two boundaries is a partition, and a merge only rewrites the partitions its keys fall into.
Those boundaries are also how compaction uses more cores. When every file being merged fits inside one partition, each partition becomes its own job, and with no file shared they all run at once.
The small levels above the dividing level are folded into the smallest level with room, a preemptive merge in the paper’s terms, and no merge writes a level back into itself.
Deletes are where compaction gives space back, and TidesDB has three kinds. A regular delete writes a tombstone, a marker saying the key is gone, which merges carry down to the largest level, where nothing older can hide, and there it disappears with the old value. A single-delete promises the key was written only once since its last delete, so it and that one value vanish the moment a merge sees both, at any level. A range delete rides inside the sorted run it was flushed into and goes away with the merge that finishes it off at the bottom.
Sorted runs left mostly tombstones move down on their own, expired TTL keys go the same way, and only an open snapshot, a reader still looking at an older point in time, holds any of it back.
To see it happen I wrote a small program against TidesDB 10. With a tiny 4MB memtable so the tree has to get deep, it loads 6 million keys (16 byte keys, 160 byte values) in random order, about a gigabyte, lets compaction settle, then deletes nine in ten.
About fifteen seconds in, the third level fills and the next merge creates a fourth. The deletes start at 42 seconds, their tombstones pile up in the small levels, then reach the bottom, and the whole thing drops from 1,055MB to 111MB in about two seconds.
Moving the dividing level (dividing_level_offset) trades many small merges against a few huge ones, and more compaction threads load faster, as the charts show.
Write amplification, the bytes the engine writes for every byte you write, sat around 11 in all those runs. That’s the paper’s promise, the write amplification of a Full Merge without needing twice the disk. With random keys every flushed run holds keys from all over, so every partition of the largest level gets rewritten, as many small jobs instead of one huge one.
In sequential mode the same keys are written in order. Each new run lands after the last one, falls into the newest partition only, and most of the largest level is never touched again.
Write amplification dropped from 11.1 to 4.2, compaction wrote 3.4GB instead of 10.7GB, and the load finished in 15.6 seconds instead of 36. Most real workloads sit somewhere between the two, and the more your writes cluster, the more partitioning saves, while large values separated from their keys skip it all.
Thanks for reading!