← Back to list

Understanding LSM(Log Structured Merge) Trees

Explore inner workings of LSM data structure

Ayush Gupta · 2023-06-25 18:27 · 123 claps · 4.6 min read paywalled
#lsm-tree #nosql #distributed-systems #cassandra #riak
Open on Medium ↗
Wiki topics: GEN · Genomics & Sequencing 💻 · Programming

Understanding LSM(Log Structured Merge) Trees

Explore the inner workings of LSM Tree

Not a Medium Member, read this article using this link.

LSM (Log-Structured Merge) Tree is a data structure commonly used in modern distributed and NoSQL databases like Cassandra, and Riak to manage and organize data efficiently. This article explores the inner workings of LSM data structures and explains how they work.

What is an LSM Tree Data Structure?

The LSM Tree was first introduced by Patrick O’Neil and Edward Cheng in 1996. This data structure is designed to optimize write-intensive workloads, where the number of write operations significantly exceeds the number of read operations. It achieves this optimization by separating the write and read operations into separate components and leveraging a disk-based storage system.

The key characteristics and components of LSM Tree include

  • Write-Optimized Architecture: LSM structures prioritize write operations, enabling quick writes to an in-memory buffer.
  • MemTable: It is a data structure used to store recently modified data in memory. The primary purpose of the MemTable is to provide fast write performance by leveraging the speed of main memory. This helps in providing efficient and low-latency write operations.
  • SSTable: Sorted String Tables are immutable on-disk data structures that store key-value pairs sorted by their keys, making them an efficient data format for range queries. They are append-only data structures, meaning that once data is written to an SSTable, it cannot be modified. Any updates or deletions result in a new SSTable being created.
  • Bloom Filters: Bloom filter is a probabilistic data structure that quickly tests whether an element is a member of a set. It helps determine if a particular key exists in the SSTables, reducing the number of disk reads when searching for data. LSM structures often employ Bloom filters to quickly determine if a key exists in the SSTables, reducing disk reads

[embed]Understanding Bloom Filters: A Probabilistic Data Structure for Efficient Set Membership medium.com

Let’s dive into the different operations and internals of an LSM Tree in more detail.

Write Operation

When a write operation occurs in an LSM data structure, the following steps are typically involved:

  • Write to MemTable: Initially, the data is written to a MemTable, which resides in memory. The MemTable is a sorted, in-memory data structure that allows for quick writes. It can be implemented using a skip list, a red-black tree, or any other suitable data structure. The write operation appends the new key-value pair to the MemTable.
  • Memory Size Threshold: As the MemTable grows, it eventually reaches a size threshold. Once this threshold is crossed, the MemTable is considered full and needs to be flushed to disk as an SSTable.
  • SSTable Creation: When the MemTable is flushed, it is persisted to disk as an immutable SSTable. The SSTable is a sequential file that contains the sorted key-value pairs. Each SSTable typically represents a range of keys. This ensures durability and prevents data loss.

Read Operation

When a read operation occurs in an LSM Tree, the following steps are typically involved

  • Search in MemTable: The read operation first searches for the desired key in the MemTable, which resides in memory. If the key is found in the MemTable, the corresponding value is returned, and the operation completes.
  • Search in SSTables: If the key is not found in the MemTable, the search continues in the on-disk SSTables. The LSM data structure employs a multi-level organization of SSTables, often referred to as “levels.”
  • Leveling: The levels are organized based on their size or creation time, with the highest level containing the most recent data. The lower levels contain compacted SSTables, where redundant or overlapping key-value pairs are eliminated. This organization helps improve read performance by reducing the number of SSTables to search through.
  • Bloom Filters: Before searching the SSTables, LSM data structures often use Bloom filters. It helps determine if a particular key exists in the SSTables without needing to read each SSTable individually. This reduces the number of disk reads during the search process.
  • Search in SSTables: Starting from the highest level, the LSM data structure searches the SSTables in each level, from the most recent to the oldest. It sequentially reads the SSTables and performs key lookups until it either finds the desired key or reaches the end of the SSTables. If the key is found, the corresponding value is returned.

Merge Process

To optimize the read performance and manage disk space, the LSM tree periodically performs a merge process that compacts and merges SSTables. This process involves the following steps:

  • Merge Selection: The merge process selects a set of SSTables to merge based on criteria such as their size, age, or overlapping key ranges.
  • Duplicate Elimination: During the merge process, duplicate keys are eliminated. When multiple SSTables contain the same key, only the most recent value is retained.
  • Sorting: The merged SSTable resulting from the merge process is sorted by key to ensure efficient read operations.
  • Level Assignment: After the merge, the merged SSTable is assigned to an appropriate level based on its size or creation time. This helps maintain the multi-level organization of the SSTables.

Compaction

Compaction is the process of compacting and eliminating redundant or obsolete data in the LSM Tree. It occurs during the merge process and helps manage disk space efficiently. Compaction involves the following steps:

  • Overlapping Key Ranges: During the merge process, SSTables with overlapping key ranges are identified. These overlapping ranges are resolved to eliminate redundant data.
  • Tombstones: In some LSM Trees, tombstones are used to mark keys that have been deleted. During compaction, SSTables containing tombstones can be safely discarded, freeing up disk space.

By performing periodic merges and compacting SSTables, LSM data structures maintain efficient read performance by reducing the number of SSTables to search through and eliminating redundant data.

LSM data structures have revolutionized data management by efficiently handling write-intensive workloads while ensuring high-performance read operations. Their write-optimized architecture, leveraging MemTables and SSTables, along with the merge process and compaction, make them an ideal choice for applications that require fast writes and efficient data retrieval. With use cases spanning distributed and NoSQL databases, LSM data structures remain at the forefront of modern data management systems, enabling scalable and robust solutions.

Want to know how data durability and resiliency are maintained in Cassandra using Write-Ahead Logging, Check this article

[embed]Write-Ahead Log (WAL): What and How it works? How does WAL provide resiliency to Kafka, Cassandra & Zookeeper?ayushgupta2959.medium.com

Thank you for reading! 😊 Don’t forget to follow me on Medium for more insightful articles & stories📚✨

[embed]All My Technical Articles Here is the list of all the technical articles I have medium.com


메타데이터
post_id
4bf77777fef4
slug
understanding-lsm-log-structured-merge-trees-4bf77777fef4
url
https://medium.com/@ayushgupta2959/understanding-lsm-log-structured-merge-trees-4bf77777fef4
canonical_url
https://medium.com/@ayushgupta2959/understanding-lsm-log-structured-merge-trees-4bf77777fef4
author_url
https://medium.com/@ayushgupta2959
status
ok
fetched_at
2026-07-25 14:52:35