Sub topic 18
LSM Trees in LevelDB, RocksDB, and Cassandra
This chapter assumes you know the memtable, write-ahead log, SSTable, compaction, and amplification ideas from earlier chapters. Here you map those ideas onto three engines you are likely to meet in practice.
Same design, different defaults
LevelDB, RocksDB, and Cassandra all follow the pattern you built in TinyLSM. Writes go to a log and a memtable. Full memtables flush to immutable sorted files. Background compaction merges those files and drops tombstones. The engines differ in where they run and in how much they let you tune.
LevelDB
LevelDB is a small embedded key-value library. It is linked into your program and stores data in a local directory. It uses leveled compaction: level 0 holds freshly flushed files, and each deeper level is a sorted, non-overlapping run about ten times larger than the one above. Each SSTable has a block index and an optional Bloom filter. LevelDB is a good engine to read if you want to study a complete LSM tree, because the code is compact.
Its limits are also worth knowing. It has few tuning options and basic compaction concurrency, so heavy write loads can stall while compaction catches up.
RocksDB
RocksDB started as a fork of LevelDB and keeps the same core design. It adds features aimed at large, busy servers:
- Multiple compaction threads that run in parallel.
- A choice of compaction styles, including leveled, size-tiered (called universal in RocksDB), and FIFO, which simply deletes the oldest files.
- Column families, which are separate keyspaces that share one write-ahead log.
- Many tuning options for memtable size, Bloom filter bits per key, block size, and caching.
Other systems often embed RocksDB as their storage layer, so you may use it without configuring it directly.
Cassandra
Cassandra is a distributed database. Each node keeps its own LSM tree, with a commit log (the write-ahead log), memtables, and SSTables. Cassandra spreads data across nodes by partition key, so the LSM tree handles only the data stored on one node.
Cassandra lets you pick a compaction strategy per table. Size-tiered compaction suits write-heavy tables. Leveled compaction suits read-heavy tables where you want fewer files per lookup. A time-window strategy groups files by time, which fits data that expires on a schedule.
Tombstones need extra care in Cassandra. A node that was offline may still hold an old value, so tombstones are kept for a grace period before compaction may drop them. Without that wait, a deleted value could reappear when the stale node syncs.
Comparing the three
| Engine | Form | Compaction options | Tuning surface |
|---|---|---|---|
| LevelDB | Embedded library | Leveled | Small |
| RocksDB | Embedded library | Leveled, universal, FIFO | Large |
| Cassandra | Distributed database | Size-tiered, leveled, time-window | Per-table settings |
Exercise
Why does Cassandra keep tombstones for a grace period instead of dropping them as soon as a compaction covers the key, as compact_all does in TinyLSM?
Show answer
compact_all is safe because one process holds every older table, so no older value can remain. Cassandra replicates data across nodes. A node that missed the delete may still hold the old value. If the tombstone were dropped too early, repair could copy that old value back and the deleted data would reappear. The grace period gives replicas time to receive the tombstone first.