Skip to main content

Command Palette

Search for a command to run...

B-Tree vs LSM-Tree Indexing

Understand the indexing structures behind relational SQL databases and write-heavy NoSQL databases.

Updated
โ€ข1 min readโ€ขView as Markdown

Database engines rely on specialized index structures to speed up reads, but choose different architectures depending on read vs write ratios.

๐Ÿ“Š B-Tree Index (Read-Optimized)

Used in relational databases like PostgreSQL, MySQL, and SQLite.

  • Structure: A self-balancing search tree mapping sorted data pages.
  • Trade-off: Fast O(log N) random reads and writes, but updates require random disk I/O.
                  [ Root Node ]
                 /      |      \
         [ Internal ] [ Internal ] [ Internal ]
          /    |   \    /   |   \    /   |   \
        [Leaf][Leaf][Leaf]... Page Data (On Disk)

๐Ÿ“Š LSM-Tree Index (Write-Optimized)

Used in NoSQL databases like Cassandra, RocksDB, and DynamoDB.

  • Structure: Append-only writes to an in-memory buffer (MemTable), which are flushed periodically to sorted, immutable on-disk files (SSTables).
  • Trade-off: Blazing fast write throughput, but reads are slower due to compaction and checking multiple files.