LSM-Tree Compaction Strategies
LSM-tree based databases (Cassandra, RocksDB, ScyllaDB) don't update data in place. They write immutable SSTables. Over time, these files must be merged to reclaim space and improve reads. This is Compaction.
1. Size-Tiered Compaction Strategy (STCS)
- The Logic: Merge SSTables of similar sizes into one larger SSTable.
- Pros: Maximum write throughput.
- Cons: High Read Amplification. A single key could exist in many different SSTables, forcing the database to check all of them.
2. Leveled Compaction Strategy (LCS)
- The Logic: Organizes SSTables into numbered levels (L0, L1, L2...). Each level is 10x larger than the previous. Data in L1 and above is guaranteed to have no overlapping keys.
- Pros: Excellent read performance. Most reads are satisfied by hitting only one SSTable per level.
- Cons: High Write Amplification. Every update requires multiple background writes as data moves through levels.
3. Which one to choose?
- Use Size-Tiered for logging, telemetry, and write-heavy workloads.
- Use Leveled for standard CRUD apps where read latency is the primary concern.
Summary
Compaction is the engine of LSM-tree performance. By choosing the right strategy, you tune your database for the specific balance of read-to-write ratio required by your application.
