arthrop0d

Brief articles on varied topics.

LSM-Trees, Memtables, SSTables, and Compaction

databasesalgorithms

An LSM-tree (log-structured merge-tree) is a storage design that accepts writes in memory and periodically flushes them into sorted, immutable files on disk. Compaction then merges those files, removing obsolete versions and keeping future reads manageable.

Why use an LSM-tree?

Updating a record in a traditional disk-oriented data structure often means finding the record’s location, reading a page, changing it, and writing the page back. Random writes are expensive, especially when many small updates are scattered across the key space. Repeated updates can also write much more data than the application supplied; this is called write amplification.

An LSM-tree takes a different approach: collect many small writes in memory, then write them together in a large sequential operation. Sequential I/O is generally cheaper and easier for storage systems to handle efficiently.

The tradeoff is that data is initially spread across multiple places. A read may need to check memory and several files, and background work must periodically merge those files. The design exchanges expensive random writes for read work and compaction work that can be controlled by the storage engine.

The “log-structured” part refers to writing new data in an append-oriented way. The “merge” part refers to repeatedly combining sorted runs of data.

The write path

Suppose an application writes user:42 = active.

  1. Durability log: The database commonly appends the operation to a write-ahead log (WAL), a sequential file that allows the operation to be recovered after a crash. Depending on its durability setting, the database may wait for the log to reach stable storage before acknowledging the write.
  2. Memtable: The operation is inserted into a memtable, an in-memory data structure that keeps recent entries ordered by key. The memtable may be implemented as a skip list or another ordered structure; the exact choice is an implementation detail.
  3. Rotation: When the active memtable reaches a size threshold, the database makes it immutable—no longer accepting changes—and creates a new active memtable. The immutable memtable remains available for reads while it is being written out.
  4. Flush: The database writes the immutable memtable as an SSTable (Sorted String Table), an immutable disk file whose records are sorted by key. Despite the name, an SSTable usually contains binary records, not necessarily strings.

The WAL protects data that has been acknowledged but has not yet made it into an SSTable. After the immutable memtable has been successfully flushed, its corresponding WAL records can usually be discarded or reused, subject to the engine’s recovery rules.

An SSTable normally contains more than a raw sequence of records. It may include a sparse index that narrows the file position for a key, block-level checksums, and a Bloom filter, a compact probabilistic structure that can say “this key is definitely not in this file” or “it might be.” These features reduce unnecessary disk reads without changing the file’s immutable nature.

How reads find a value

A read first checks the newest state, because it may contain an update not yet flushed to disk. It then checks the immutable memtable and SSTables from newest to oldest. The first applicable record wins.

For example, an older SSTable might contain:

user:42 = pending

A newer memtable might contain:

user:42 = active

The read returns active and does not need the older value. Storage engines commonly attach an internal sequence number, a monotonically increasing ordering value, to versions so that “newest” remains well-defined even when files overlap.

Deleting a key does not immediately remove every older copy from disk. Instead, the engine writes a tombstone, a record that says the key was deleted. Reads treat the tombstone as the newest result and hide older values. The tombstone must remain until compaction has proved that no older data that it could override still matters.

This lookup process creates read amplification: one logical read may inspect several memtables or SSTables. Indexes, Bloom filters, caching, and careful file organization reduce that cost, but they do not eliminate the need for background maintenance.

Compaction keeps the tree usable

Without compaction, every memtable flush would create another SSTable. Reads would have more files to check, deleted data would remain indefinitely, and storage usage would grow even when the application repeatedly updates the same keys.

Compaction reads several sorted inputs and produces one or more new sorted SSTables. Like the merge step in a merge sort, it can process each input in key order rather than repeatedly searching randomly. During the merge it can:

The old input files are deleted only after the new files are safely written and readers no longer need the old ones. This makes compaction a background rewrite, not an in-place edit of an SSTable.

Common organization strategies

In a size-tiered arrangement, similarly sized SSTables accumulate and are merged together. This can make writes efficient, but it may leave many files and overlapping key ranges for reads.

In a leveled arrangement, SSTables are organized into levels. Files in a higher level generally cover non-overlapping key ranges, while lower levels receive new flushes and are compacted into larger levels. A lookup can then avoid many files, at the cost of rewriting data during compaction.

Real databases vary, and some combine these strategies. Their configuration often exposes thresholds for memtable size, level size, compaction concurrency, or write-rate limits.

The operational tradeoff

Compaction consumes CPU, memory, and disk bandwidth. If incoming writes arrive faster than compaction can process them, files accumulate and the engine may apply backpressure, slowing or blocking new writes to prevent unbounded memory or storage growth. During heavy compaction, latency can rise because foreground reads and writes compete with background I/O.

This is why an incident report may mention “pending compaction,” “too many SSTables,” “flush stalls,” or “write amplification.” These are not separate storage models: they are visible consequences of the same LSM-tree pipeline—buffer writes in memtables, flush sorted immutable files, search newer state first, and continuously merge files to keep the result practical.

← All articles