SDE & MLE Prep

Database Indexes: B-Tree vs LSM-Tree

  • B-tree (Postgres, MySQL InnoDB): pages updated in place; great reads and range scans; random writes cost more.
  • LSM-tree (Cassandra, RocksDB, LevelDB, HBase): writes go to an in-memory memtable + WAL, flushed as immutable sorted SSTables, merged by compaction. Excellent write throughput; reads may check several SSTables (mitigated by Bloom filters and sparse indexes).
write -> WAL (disk, sequential) + memtable (RAM)
                     | full
                     v
          SSTable L0  SSTable L0      -- compaction -->  L1 (bigger, sorted, non-overlapping) -> L2 ...
read  -> memtable -> bloom filter? -> SSTables newest to oldest

Other indexes: hash (point lookups), inverted index (full-text search: term → posting list), geospatial (geohash, quadtree, S2), composite (order matters: (user_id, created_at) serves "user's latest posts").