Course contentsShow
← Databases & SQL
Lesson 1401 of 1,95313. Specialized Data StorageFree lesson

Compaction Strategies in LSM-Trees

Tune background processes to manage write amplification and storage efficiency.

In this lesson, you will learn how to tune background compaction processes in Log-Structured Merge-Trees (LSM-trees) to balance write amplification against read performance and disk space efficiency.

The Mechanics of Compaction

In an LSM-tree, incoming data is buffered in an in-memory MemTable and periodically flushed to immutable SSTables on disk. Because updates and deletes are represented as new entries, redundant data accumulates over time. Compaction is the background process that merges these SSTables, discards obsolete versions, and reclaims storage.

If compaction runs too slowly, your database suffers from read amplification (because queries must scan too many files) and disk space bloat. If it runs too aggressively, you suffer from write amplification (burning through SSD write endurance and consuming CPU/IO cycles).

Tuning Strategies

You generally choose between two primary strategies depending on your workload:

  1. Size-Tiered Compaction (STCS): This groups SSTables of similar sizes together. It is excellent for write-heavy workloads because it minimizes the amount of data rewritten. However, it can lead to higher read latency since a key might exist in many different "tiers."
  2. Leveled Compaction (LCS): This organizes SSTables into levels where each level has a strict size limit. It provides much faster read performance because a key is typically found in only one file per level. The cost is significantly higher write amplification, as data is constantly being reorganized into new levels.

Example Configuration Logic

When tuning these, you are essentially adjusting the "throttling" of the background merge process:

-- Conceptual tuning for a compaction background process
-- Adjusting the threshold to trigger compaction earlier
SET compaction_threshold_bytes = '256MB'; 

-- Setting the max concurrent threads to prevent IO starvation
SET max_compaction_threads = 4;

When managing these, monitor your I/O Wait and Disk Queue Depth. If your background threads are saturated, your application's write latency will spike during compaction bursts. Use smaller, more frequent compactions for latency-sensitive applications, or larger, batch-oriented compactions for throughput-heavy analytical workloads.

Key Takeaway: Tune your compaction strategy by balancing the CPU and I/O cost of merging files (write amplification) against the need to minimize the number of files a read must traverse (read amplification).