Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain how a log-structured merge tree (LSM tree) stores data: what happens on a write, on a read and during compaction. Name databases or engines that use LSM trees.

An LSM tree writes each change to a write-ahead log for durability and into an in-memory sorted table, the memtable; when the memtable fills, it is written out as an immutable sorted file, an SSTable. A read checks the memtable and then SSTables from newest to oldest, using Bloom filters and sparse indexes to skip files that cannot hold the key. Compaction merges SSTables in the background, keeping the newest version of each key and dropping deleted data when safe; RocksDB, LevelDB, Cassandra, ScyllaDB, HBase and MyRocks all work this way.

Why this question is asked

B+ trees update pages in place, which means random writes all over the disk. Systems that ingest huge write volumes, such as event stores, time series, messaging and metadata services, often choose a different design that turns every write into a sequential one: the log-structured merge tree. Java engineers meet it in Cassandra, in Kafka Streams state stores, which use RocksDB, and in many cloud services. Interviewers ask about it to see whether we understand the trade it makes, fast writes in exchange for more complex reads and background work. In this answer, we will follow a write, then a read, then a compaction, and finish with where LSM trees are used.

1The Idea Behind the LSM Tree

The core idea is never to modify data on disk. Instead of finding the right page and changing it, an LSM tree collects writes in memory, sorts them there, and writes them out in large sorted batches. Files on disk are immutable once written. Updating a key simply writes a newer version, and deleting a key writes a special marker. The cost of keeping all those versions tidy is moved to a background process, compaction.

2What Happens on a Write

A write does two things. First it appends the change to a write-ahead log (in Cassandra, the commit log), a sequential file that exists only so the change can be recovered after a crash. Then it inserts the change into the memtable, an in-memory sorted structure, often a skip list. That is all: the write is acknowledged without touching any data file, which is why LSM writes are fast and steady.

When the memtable reaches its size limit, for example 64 MB, it becomes read-only, a new empty memtable takes its place, and the full one is flushed to disk as an SSTable, a sorted string table. Because the memtable is already sorted, the flush is one long sequential write. After the flush, the corresponding part of the write-ahead log can be discarded.

Step throughA write in an LSM tree
  1. Append to write-ahead logsequential, for crash recovery
  2. Insert into memtablesorted in memory, write acknowledged
  3. Memtable full?for example 64 MB
    YesFreeze it and start a new oneNoKeep accepting writes
  4. Flush frozen memtableone sequential write of a new SSTable
  5. Discard old log segmentthe data is now in an SSTable

5 steps in order. Play them, or step through them one at a time.

3What Happens on a Read

The newest value of a key may be in the memtable or in any SSTable, so a read searches in order from newest to oldest: the active memtable, any frozen memtables, then the SSTables starting with the most recent. The first version it finds is the answer, because anything older has been superseded.

Searching every file would be slow, so each SSTable carries helpers. A Bloom filter is a compact bit array that can say for certain that a key is not in the file, or that it might be; with about 10 bits per key it is wrong only about 1 percent of the time. A sparse index records the first key of each block, for example every 4 KB or 64 KB, so the engine can jump to the one block that could contain the key. A block cache keeps recently read blocks in memory.

Java
get("user:42")
memtable -> not found
SSTable 7 (newest) bloom -> definitely not here, skipped
SSTable 6 bloom -> maybe; sparse index -> block 118; read block -> found {name: "Ana", v: 3}
return v3; SSTables 5..1 are never opened, even if they hold v1 and v2

Notice the trade. A read may have to look in several places, and a key that does not exist at all must be ruled out in every file, although Bloom filters make that cheap. This is called read amplification, and keeping it low is one of compaction's jobs.

4Compaction and Tombstones

Without cleanup, the number of SSTables would grow forever, reads would get slower, and old versions would waste space. Compaction is a background process that picks several SSTables, merges them like the merge step of merge sort, and writes new SSTables. During the merge, when the same key appears in several inputs, only the newest version is kept. Then the input files are deleted.

Deletes are written as tombstones, markers that say this key was deleted at this time. During compaction a tombstone hides older versions of the key, and the tombstone itself can be dropped once no older file could still contain the key and, in distributed systems like Cassandra, once enough time has passed for every replica to learn about the delete. Different strategies decide which files to merge and when; size-tiered and leveled compaction are the two best known.

Compacting two SSTables
KeySSTable 1 (older)SSTable 2 (newer)Result
aa = 1a = 1
bb = 2b = 5b = 5 (newest wins)
cc = 3c deleted (tombstone)dropped, or tombstone kept until safe
dd = 4d = 4
A merge keeps the newest version of each key and lets tombstones remove older data.

5Strengths, Costs and Where It Is Used

LSM trees shine at write-heavy workloads: writes are sequential, files compress well because they are written once and sorted, and there are no half-empty pages. The costs are reads that may touch several files, especially for keys that changed many times, and compaction, which rewrites data repeatedly and consumes disk bandwidth and CPU in the background. If compaction falls behind, reads slow down and disk use grows.

Well-known LSM engines and databases include LevelDB and RocksDB, which are embedded libraries; Apache Cassandra and ScyllaDB, distributed wide-column databases; Apache HBase, built on the Bigtable design; and MyRocks, which plugs RocksDB into MySQL as a storage engine. Kafka Streams uses RocksDB for its local state stores, so many Spring Boot applications run an LSM tree without their authors knowing it.

6Follow-up questions

Why are deletes written as tombstones instead of removing data?

Because SSTables are immutable, the engine cannot remove a key from the older files that contain it, and if it simply forgot the key in the memtable, a read would find the older value in an older SSTable and the deleted data would reappear. The tombstone is a newer version that says deleted, so it wins over older versions in reads and removes them during compaction. In a replicated database it also lets replicas that missed the delete learn about it during repair.

How is an LSM tree different from a B+ tree on writes?

A B+ tree finds the page that holds the key and changes it in place, so each write can mean a random page read and write. An LSM tree only appends to a log and inserts into memory, and writes data files sequentially in large batches, then pays later in compaction.

What happens to the memtable if the server crashes?

The memtable is lost, but every change in it was first appended to the write-ahead log. On restart the engine replays the log to rebuild the memtable, so acknowledged writes are not lost, as long as the log was flushed according to the durability settings.

7Conclusion

In this answer, we saw that an LSM tree writes changes to a write-ahead log and an in-memory sorted memtable, and flushes full memtables as immutable sorted SSTables. Reads search the memtable and then SSTables from newest to oldest, skipping files with Bloom filters and sparse indexes. Compaction merges SSTables in the background, keeping the newest version of each key and using tombstones to remove deleted data when safe. The design gives fast sequential writes at the cost of more read work and background I/O, and it powers RocksDB, LevelDB, Cassandra, ScyllaDB, HBase and MyRocks.

What to say in the interviewAbout 90 seconds aloud

Open with

An LSM tree never updates data files in place. On a write, the change is appended to a write-ahead log, which is only there for crash recovery, and inserted into the memtable, a sorted structure in memory.

Then, if they want more

That is all the write does, so it is fast and sequential. When the memtable reaches its limit, say 64 MB, it is frozen and flushed to disk as an SSTable, an immutable sorted file, in one sequential write, and the old log segment can be dropped.

On a read, the newest version wins, so the engine looks in the memtable first and then in SSTables from newest to oldest. To avoid opening every file, each SSTable has a Bloom filter that can say a key is definitely not there, a sparse index that points to the right block, and there is a block cache.

In the background, compaction merges SSTables like merge sort: for each key it keeps only the newest version, and deletes are tombstones, markers that hide older versions and get dropped once it is safe. The trade-off is fast writes and good compression against read amplification, since a key may be in several files, and the extra I/O of compaction.

Examples are LevelDB and RocksDB as embedded engines, Cassandra and ScyllaDB, HBase, and MyRocks for MySQL, and Kafka Streams uses RocksDB for state stores.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain how a log-structured merge tree (LSM tree) stores data: what happens on a write...": https://scrivio-interview-library.netlify.app/databases/storage-internals/44c84a2c69/