LSM-Trees, Memtables, SSTables, and Compaction
An LSM-tree (Log-Structured Merge-tree) is a database storage design that accepts updates in memory, writes them as sorted immutable files, and periodically merges those files in the background. It is built to make writes efficient, especially when storage is expensive to update in place.
You have probably encountered the consequences in a database metric or incident report: memtable flush, SSTable, compaction backlog, write amplification, or read amplification. These terms describe different stages and costs of the same design.
The problem LSM-trees solve
A traditional update-oriented storage structure, such as a B-tree, keeps records sorted on disk and modifies pages in place. Updating one record may require reading a page, changing it, and writing it back. If writes arrive at scattered keys, those operations produce many small, random storage writes. Random writes are slower and consume more device bandwidth than large sequential writes.
An LSM-tree takes a different approach: do not immediately rearrange the durable data for every update. Collect recent writes in memory, then write a large, sequential, sorted batch to disk. Later, merge that batch with older sorted data.
This improves write throughput, but it does not remove work. It moves some work into reads and background maintenance. The central trade-off is:
LSM-trees make the initial write path cheap by postponing organization; compaction eventually pays for that organization.
The write path
A typical write follows these steps:
- Append to a write-ahead log. The database first records the operation in a sequential log, often called a WAL. A write-ahead log is a durable record used to recover recent operations after a crash. This lets the database acknowledge a write before the in-memory structure has been written as a sorted file.
- Add the update to a memtable. A memtable is an in-memory table holding recent keys and values in sorted-key order. The implementation may use a skip list or another ordered structure; the important property is that it can produce the entries in key order.
- Freeze a full memtable. When the active memtable reaches a configured size, the database replaces it with a new active one. The full one becomes an immutable memtable: it is no longer changed, but readers can still consult it while it is being written out.
- Flush it to an SSTable. The database writes the immutable memtable as an SSTable, short for sorted-string table: an immutable file whose records are ordered by key. The result is usually one or more data files plus indexes and metadata that help locate keys.
The WAL and the SSTable serve different purposes. The WAL protects acknowledged operations during recovery. The SSTable is the organized, queryable form of the data after a memtable flush. Once the flush is safely complete, the database can usually discard the corresponding WAL records.
An update does not need to find and overwrite an older value. If key user:42 changes from active to disabled, the new entry is simply appended to the current write flow. The older value remains in an older file until compaction removes it.
How reads find a value
A read checks the newest possible location first: the active memtable, then any immutable memtable, then recently created SSTables, and finally older SSTables. If it finds several versions of the same key, the newest version wins.
SSTables are immutable, so a read cannot update one while searching it. Instead, each file commonly has a sparse index—metadata mapping key ranges to file offsets—and a Bloom filter, a compact probabilistic structure that can say “this key is definitely not in this file.” These structures avoid unnecessary disk reads, but they do not eliminate the fundamental possibility that one logical lookup must inspect multiple files.
A delete is normally represented by a tombstone, a stored marker saying that a key was deleted. The tombstone must remain visible over older files; otherwise, a read that misses the newer delete could incorrectly return an old value. Compaction can remove the tombstone only when the database knows no older data that matters can still contain the deleted key.
This is read amplification: one user-level read causes work across multiple in-memory tables or files. A database with many unmerged files may have excellent write throughput but slower and less predictable reads.
Compaction turns runs into fewer runs
Compaction is the background process that reads sorted files, merges their entries, and writes new sorted files. During the merge, the database can keep only the newest visible version of a key and discard obsolete values. It can also discard tombstones when doing so is safe.
Compaction strategies differ, but two common patterns are:
- Leveled compaction: files are arranged into levels with size and key-range limits. A file is merged into the next level when that level needs space. This usually keeps the number of files consulted by a read relatively low, at the cost of rewriting data repeatedly.
- Tiered or size-tiered compaction: several similarly sized files are grouped and merged into a larger file. This can reduce immediate write pressure, but more files may coexist, increasing read and storage amplification until a merge occurs.
Compaction itself consumes disk bandwidth, CPU, and temporary space. The total amount of data written can greatly exceed the application’s logical writes. This is write amplification. For example, one record may be written during a memtable flush and then rewritten through several compaction levels.
There is also space amplification: old versions, duplicate entries, and tombstones occupy storage until compaction removes them. A compaction backlog can therefore make a database both slower to read and closer to its disk-capacity limit.
The operational trade-off
LSM-trees are not simply “faster databases.” They exchange one kind of cost for another:
| Benefit | Cost |
|---|---|
| Sequential, batched writes | Background compaction writes |
| High write throughput | Write amplification and device wear |
| No in-place updates to data files | Read amplification across files |
| Old versions can be deferred | Temporary space and space amplification |
The configuration details you see in production—memtable size, flush thresholds, compaction rate, level sizes, and read latency—are all ways of balancing this exchange. If writes arrive faster than compaction can merge files, the backlog grows; eventually the database may throttle or stall writes to protect available memory and disk space.
The useful mental model is simple: an LSM-tree turns many small updates into a sequence of sorted runs, then relies on compaction to keep those runs manageable. Its performance depends not only on the cost of accepting a write, but also on how much deferred work the system can absorb and how quickly it can merge that work back into a compact representation.