Inside Apache Cassandra’s LSM-Tree Storage Engine: Turning Random Writes into Sequential I/O

Modern distributed databases built for high-velocity ingestion—such as Apache Cassandra, ScyllaDB, and RocksDB—rely on the Log-Structured Merge-tree (LSM-tree) storage architecture. Unlike traditional B-Tree engines that perform in-place page updates (causing random disk I/O and fragmentation under heavy concurrency), an LSM-tree buffers mutations in memory and flushes them to disk as immutable, sorted files.

This architectural choice places Cassandra squarely in the high-write, append-heavy quadrant of the storage engine decision matrix.


Why Choose an LSM-Tree? The Storage Engine Decision Matrix

Every storage engine navigates trade-offs between Read Amplification, Write Amplification, and Space Amplification (often summarized by the RUM Conjecture). Rather than updating existing data blocks on disk, LSM-trees append new state sequentially and reconcile versions asynchronously.

Architectural Dimension LSM-Tree (Cassandra, ScyllaDB, RocksDB) Traditional B-Tree (MySQL / WiredTiger)
Primary Optimization High-write, append-heavy workloads Balanced reads and in-place updates
Disk Write Pattern Sequential append (CommitLog + SSTable flush) Random I/O (in-place page modification)
Write Acknowledgment Immediate after CommitLog + Memtable write After page update and WAL sync
Read Path Complexity Merges Memtable + relevant SSTables via Bloom filters Direct tree traversal to leaf page
Background Maintenance Periodic merge-sort compaction & tombstone purge Page splitting, balancing, and vacuuming

Where Is the “Tree”? RocksDB’s Levels of Sorted Files

The original LSM-tree design used multiple tree-based components, with a small in-memory tree and larger disk-resident trees. Modern implementations do not necessarily form one branching tree with parent/child pointers like a B-Tree.

In modern RocksDB, leveled compaction is better pictured as levels of sorted files:

MemTable       Mutable data in RAM
    | flush
    v
Level 0        [SST] [SST] [SST]     Key ranges may overlap
    | compaction
    v
Level 1        [A-F] [G-M] [N-Z]     Non-overlapping key ranges
    | compaction
    v
Level 2        Larger total capacity, more sorted files

The letter ranges are illustrative key ranges under the database’s key comparator. Within each level above Level 0, file key ranges do not overlap under leveled compaction, but ranges can overlap across levels. The same key can therefore have newer and older versions in different levels at the same time; reads resolve the visible version, and compaction removes obsolete versions when safe.

This diagram describes RocksDB’s leveled compaction, not every LSM implementation or compaction strategy. Cassandra’s SSTable organization depends on its configured compaction strategy. The shared idea is to buffer writes, flush sorted data, and merge it in the background.


How Cassandra Implements the LSM-Tree

Each Cassandra node operates an independent local storage engine. When a write arrives at a node, Cassandra converts what would otherwise be a random disk seek into fast sequential I/O through a five-stage lifecycle:

flowchart LR
    A[Client Write] --> B[1. CommitLog / WAL<br/>Sequential Disk Append]
    A --> C[2. Memtable<br/>Sorted In-Memory Buffer]
    C -->|Acknowledge Client| A
    C -->|Threshold Reached| D[Immutable Memtable]
    D -->|3. Sequential Flush| E[SSTables on Disk<br/>Data.db + Filter.db]
    E -->|4. Read Path| F[Bloom Filter Check<br/>Skip Non-Matching Files]
    E -->|5. Background Thread| G[Compaction<br/>Merge-Sort & Purge Tombstones]

1. CommitLog / WAL (Write-Ahead Log)

When a mutation enters a Cassandra node, it is first appended sequentially to an on-disk CommitLog for crash recovery and durability. Because the CommitLog is append-only (segmented by commitlog_segment_size, defaulting to 32 MiB in Apache Cassandra), it avoids random disk seeks. Once all data in a CommitLog segment has been flushed to SSTables, that segment can be recycled or deleted.

2. Memtable (In-Memory Buffer)

Simultaneously, Cassandra writes the mutation into an in-memory sorted structure called a Memtable. At this point—without touching the main data tables on disk—the write is immediately acknowledged to the client. Memtables also serve recent reads directly from RAM, keeping write latency consistently low.

3. Flushing to SSTables

When a Memtable reaches its configured memory threshold (or when the CommitLog approaches its size limit), it transitions into an immutable Memtable and flushes to persistent storage as a read-only SSTable (Sorted String Table). Because keys are already maintained in sorted token order in memory, flushing is a continuous sequential streaming write rather than random I/O.

Each SSTable is composed of specialized component files on disk, including:

  • Data.db: Row contents sorted by partition token and clustering key.
  • Filter.db: An off-heap Bloom filter of partition keys.
  • Partitions.db / Index.db: Maps partition keys to positions in Data.db.
  • Statistics.db: Metadata covering timestamps, tombstones, TTLs, and compaction state.
  • CompressionInfo.db & Digest.crc32: Compression chunk offsets and CRC-32 integrity checksums.

4. Bloom Filters on the Read Path

Because updates to a partition key can span multiple immutable SSTables on disk, a naive read would have to scan every SSTable. To prevent unnecessary disk seeks, Cassandra pairs each SSTable with an off-heap Bloom filter (Filter.db).

Before touching disk, the read path queries each SSTable’s Bloom filter:

  • “Definitely not present”: Cassandra skips that SSTable entirely, saving a disk seek.
  • “Possibly present”: Cassandra checks the SSTable index and reads the partition from Data.db.

Operators can tune bloom_filter_fp_chance (typically between 0.01 and 0.10) to balance off-heap RAM consumption against disk I/O—lowering the false-positive rate from 0.10 to 0.01 roughly triples Bloom filter memory usage while eliminating most false-positive disk reads.

5. Compaction & Tombstone Purging

As Memtables continuously flush, SSTables accumulate on disk. Background compaction threads periodically merge multiple sorted SSTables together using a streaming merge-sort:

  • Reconciles updates by retaining only the latest timestamped value for each column/row.
  • Deduplicates records scattered across older and newer SSTables.
  • Purges tombstones (deletion markers) once gc_grace_seconds has elapsed, reclaiming disk space and keeping read latency predictable.

Compaction & Read-Path Data Flow

The sequence below illustrates how Cassandra coordinates writes, reads, and background compaction without blocking incoming client traffic:

sequenceDiagram
    autonumber
    participant Client
    participant CommitLog as CommitLog (Disk WAL)
    participant Memtable as Memtable (RAM)
    participant Filter as Bloom Filter (Off-Heap)
    participant SSTable as SSTables (Disk)

    Client->>CommitLog: Append mutation sequentially
    Client->>Memtable: Insert sorted key/value
    Memtable-->>Client: Immediate Write ACK

    Note over Memtable,SSTable: Memtable reaches threshold
    Memtable->>SSTable: Sequential flush to immutable SSTable

    Client->>Memtable: Read Request (Partition Key)
    Client->>Filter: Check SSTable Bloom Filters
    Filter-->>Client: Skip misses / Identify candidate SSTables
    Client->>SSTable: Read matching Data.db + merge versions

    Note over SSTable: Background Compaction
    SSTable->>SSTable: Merge-sort SSTables & purge tombstones

Key Operational Takeaways

  • Write-Path Speed: Converting random mutations into sequential CommitLog appends plus in-memory Memtable inserts enables linear write scaling across nodes.
  • Memory vs. Disk Tuning: Tuning bloom_filter_fp_chance directly trades off-heap RAM for reduced read amplification on disk.
  • Compaction Budget: Because compaction rewrites SSTables in the background, sizing disk IOPS headroom for compaction is essential to prevent read latency spikes under sustained write load.