Inferensys

Glossary

Log-Structured Merge-Tree (LSM Tree)

A Log-Structured Merge-Tree (LSM Tree) is a high-performance storage engine data structure that batches writes in a memory-resident component (memtable) before merging them into sorted, immutable files on disk.
Developer reviewing semantic search engine results on laptop, relevance scores visible, technical search demo.
MEMORY UPDATE AND EVICTION

What is Log-Structured Merge-Tree (LSM Tree)?

A Log-Structured Merge-Tree (LSM Tree) is a high-performance storage engine data structure that batches writes in a memory-resident component (memtable) before merging them into sorted, immutable files on disk.

A Log-Structured Merge-Tree (LSM Tree) is a write-optimized data structure used in storage engines for databases and key-value stores. It achieves high write throughput by batching updates in an in-memory memtable and sequentially flushing them to disk as sorted, immutable SSTables (Sorted String Tables). This design minimizes random disk I/O, making it ideal for workloads with frequent inserts and updates, such as in time-series data or agentic memory systems managing rapid state changes.

Reads query multiple levels of SSTables, potentially requiring a merge of results. A background compaction process continuously merges and rewrites these files into larger, consolidated levels, applying tombstones for deletions and enforcing the chosen eviction policy for old data. This tiered, log-structured approach is fundamental to systems like Apache Cassandra, RocksDB, and Google LevelDB, providing the durable, high-ingestion backend for agentic memory persistence and update operations.

ARCHITECTURE

Key Components of an LSM Tree

An LSM Tree is a high-performance storage engine that optimizes write throughput by batching updates in memory before merging them into sorted, immutable files on disk. Its architecture is defined by several core components that work in concert.

01

Memtable

The memtable is a mutable, in-memory data structure (typically a balanced tree like a Red-Black Tree or a skip list) that serves as the primary write buffer. All incoming writes (inserts, updates, deletes) are first appended to a write-ahead log (WAL) for durability and then applied to the memtable. When the memtable reaches a configured size threshold, it becomes immutable and is flushed to disk as a Sorted String Table (SSTable). This design allows for extremely high write throughput, as writes are sequential appends to the WAL and in-memory updates.

02

Sorted String Table (SSTable)

An SSTable is an immutable, sorted file on disk that stores key-value pairs. It is the persistent representation of a flushed memtable. Key characteristics include:

  • Sorted by Key: Enables efficient range queries and merging.
  • Immutable: Once written, it is never modified in place.
  • Indexed: Contains a sparse in-memory index (e.g., a Bloom filter and block offsets) for fast point lookups.
  • Compressed: Often compressed to reduce storage footprint. SSTables are organized into levels (L0, L1, L2...), with each level having a size limit. Newer data resides in smaller, higher levels (like L0), and older data is gradually merged into larger, lower levels.
03

Compaction

Compaction is the background process that merges multiple SSTables from one level into a new, larger SSTable at the next level. It is the core mechanism for:

  • Space Reclamation: Removing obsolete or deleted values (tombstones).
  • Read Optimization: Reducing the number of files that must be checked for a read.
  • Write Amplification Control: Managing the cost of rewriting data. Common compaction strategies include Leveled Compaction (strict size tiers, better read performance) and Size-Tiered Compaction (merging similarly sized files, better write performance). The choice involves a trade-off between write amplification and read amplification.
04

Write-Ahead Log (WAL)

The Write-Ahead Log (WAL) is a sequential, append-only log file on persistent storage. Every write operation is durably written to the WAL before being applied to the memtable. This provides crash consistency: if the system fails, the memtable can be reconstructed by replaying the WAL. Once a memtable is successfully flushed to an SSTable, its corresponding WAL segments can be safely deleted. The WAL is critical for the durability guarantee of the LSM Tree, ensuring no acknowledged writes are lost.

05

Bloom Filter

A Bloom filter is a probabilistic, memory-efficient data structure used to accelerate point reads. Each SSTable has an associated Bloom filter in memory. When checking for a key, the Bloom filter can definitively say "key is definitely not present" or "key is probably present" with a very low, configurable false-positive rate. This prevents expensive disk I/O for the vast majority of queries seeking non-existent keys. For example, a 1% false-positive rate means only 1 in 100 negative lookups will incur an unnecessary disk seek, dramatically improving read performance for workloads with many misses.

06

Manifest File

The manifest file is a persistent metadata log that tracks the schema of the LSM Tree. It records:

  • The sequence of SSTable files.
  • The level to which each SSTable belongs.
  • Key range boundaries for each SSTable.
  • Compaction history and status. This file is essential for recovery and consistency. When a database starts, it reads the manifest to reconstruct its in-memory view of the SSTable hierarchy. The manifest is updated atomically during major events like flushes and compactions to maintain a consistent view of the storage state.
MEMORY UPDATE AND EVICTION

How an LSM Tree Works: The Write and Compaction Cycle

A Log-Structured Merge-Tree (LSM Tree) is a high-performance storage engine data structure that optimizes write throughput by batching writes in memory before merging them into sorted, immutable files on disk. Its core operational cycle consists of two fundamental phases: the write path and the background compaction process.

The write path begins with incoming data being sequentially appended to an in-memory buffer called a memtable. This provides extremely fast write latency. Once the memtable reaches a size threshold, it is flushed to disk as a new, immutable Sorted String Table (SSTable) file. Concurrently, a new memtable is created to accept writes, ensuring continuous operation. This design transforms random disk writes into sequential ones, which is the primary source of the LSM tree's high write performance.

The compaction cycle is the background process that merges and reorganizes these immutable SSTable files. As more SSTables accumulate, reads become slower because a key might exist in multiple files. Compaction selects several SSTables, merges their sorted entries, discards obsolete or deleted values (marked by tombstones), and writes out new, consolidated SSTables. This process reduces the total number of files, improves read performance, and reclaims disk space. Major compaction strategies include size-tiered and leveled compaction, which balance write amplification and read efficiency.

STORAGE ENGINE COMPARISON

LSM Tree vs. B-Tree: A Performance Trade-off Analysis

A technical comparison of two foundational data structures for high-performance storage engines, focusing on their core operational trade-offs for write-heavy, read-heavy, and mixed workloads.

Architectural Feature / MetricLog-Structured Merge-Tree (LSM Tree)B-Tree (and variants like B+ Tree)

Core Write Pattern

Append-only, batched writes to immutable sorted files (SSTables).

In-place updates within balanced tree nodes on disk.

Core Read Pattern

Multi-level search across memtable and multiple SSTable files.

Traversal of a single, balanced tree structure from root to leaf.

Write Amplification

High (4-10x typical). Caused by repeated compaction/merging of SSTables.

Low (theoretically ~1x). Each key update writes only its node and ancestors.

Read Amplification

Potentially high. Requires checking multiple SSTable levels; optimized with Bloom filters.

Low and predictable. Bounded by tree height (O(log n)).

Write Throughput

Extremely high for sequential inserts/updates. Ideal for write-heavy workloads (e.g., logging, time-series).

Lower, limited by random I/O for in-place updates. Can bottleneck on disk seeks.

Read Latency (Point Query)

Good to excellent with Bloom filters. Can degrade without compaction or with many SSTable levels.

Excellent and consistent. Predictable latency from tree traversal.

Range Query Performance

Good, but requires merging iterators from multiple SSTables. Sequential scan of SSTables is efficient.

Excellent. Leaf nodes are linked, enabling fast sequential traversal of sorted keys.

Space Amplification

Can be high due to multiple copies of data during compaction and tombstones for deletes.

Low. Data is stored primarily in one location (leaf nodes).

Background I/O

Heavy, periodic, and bursty due to compaction operations. Can impact foreground latency.

Minimal and integrated. Page splits/merges occur during foreground operations.

Concurrency Control

Simpler. Writes go to single memtable; reads are lock-free from immutable SSTables.

More complex. Requires fine-grained locks (latches) on tree nodes for in-place updates.

Optimization for SSDs

Excellent. Sequential writes and large block I/O align well with SSD characteristics.

Good, but random writes can cause write amplification on SSDs (wear leveling).

Crash Recovery

Simple via replay of Write-Ahead Log (WAL) for memtable and SSTable manifest.

Complex, requiring careful WAL replay and potential tree structure repair.

Primary Use Cases

Write-optimized databases (Cassandra, RocksDB, LevelDB), time-series, logging systems.

Read-optimized and general-purpose databases (MySQL InnoDB, PostgreSQL, traditional RDBMS).

LOG-STRUCTURED MERGE-TREE

Frequently Asked Questions

A Log-Structured Merge-Tree (LSM Tree) is a write-optimized data structure fundamental to modern storage engines. These questions address its core mechanics, trade-offs, and role in agentic memory systems.

A Log-Structured Merge-Tree (LSM Tree) is a high-performance storage engine data structure that optimizes for high-throughput write operations by batching and sequentially writing data to disk. Its core mechanism involves two primary components: a memory-resident memtable and a hierarchy of sorted, immutable files on disk called SSTables (Sorted String Tables).

  1. Write Path: All incoming writes (inserts, updates, deletes) are first appended to a write-ahead log (WAL) for durability, then applied to the in-memory memtable, which is typically a balanced tree like a skip list.
  2. Flush: When the memtable reaches a size threshold, it is frozen, and its sorted contents are written to disk as a new SSTable at Level 0 (L0).
  3. Compaction: A background process called compaction merges these SSTables from one level to the next, combining data, discarding overwritten values, and deleting tombstoned entries. This creates larger, sorted SSTables at higher levels (L1, L2, etc.).

This design transforms random writes into sequential disk I/O, making it exceptionally fast for write-heavy workloads common in logging, time-series data, and agentic memory systems where state updates are frequent.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.