Patterns
35 items
35 items
Write-optimized storage with log-structured merge trees
Log-Structured Merge Trees (LSM Trees) are write-optimized data structures used by databases like Cassandra, RocksDB, LevelDB, and HBase. Writes go to an in-memory buffer (memtable), which is periodically flushed to immutable sorted files (SSTables) on disk. Background compaction merges SSTables to maintain read performance. LSM trees achieve 10-100x better write throughput than B-trees by converting random writes to sequential I/O. The trade-off is read amplification (checking multiple levels) and write amplification (data written multiple times during compaction).
Writes go to memory (memtable) first. No disk seek required. Periodically flush to disk as sequential write. This is why LSM is write-optimized.
Once written, SSTable files never change. Simplifies concurrency (no locks needed for reads). Enables efficient caching. Updates/deletes create new entries, not modifications.
Without compaction, reads must check all SSTables. Compaction merges SSTables, removes obsolete entries, maintains sorted order. Different strategies: leveled, tiered, FIFO.
Write Path: 1. Write to WAL (durability) 2. Insert into memtable (sorted structure, e.g., red-black tree) 3. When memtable full, flush to SSTable 4. Background compaction merges SSTables
Read Path: 1. Check memtable 2. Check each level's SSTables (newest first) 3. Use bloom filters to skip SSTables 4. Binary search within SSTable