Working of compactions work in YugaByte DB
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
YugaByte DB is a highly scalable, distributed SQL and NoSQL database designed to handle high-throughput applications with low latency requirements. One of the key mechanisms employed by YugaByte DB to maintain performance and efficiency in data storage and retrieval is compaction. Compaction is a process that manages how data is stored on disk in an organized manner, which ultimately impacts database performance and disk space usage.
Understanding Compaction in YugaByte DB
YugaByte DB uses an LSM (Log-Structured Merge-tree) approach as its storage engine, which is crucial for achieving high write throughput and efficient data compression. This approach initially writes inserts and updates sequentially into a memtable (an in-memory data structure). Once the memtable fills up, it is flushed to disk as an SSTable file (Sorted Strings Table), which is immutable.
However, over time, as more data is written and as updates and deletes occur, multiple versions of the same data can exist across different SSTables, and some data may no longer be relevant (due to deletions or updates). Compaction is the process by which YugaByte DB cleans up these inefficiencies.
Types of Compactions in YugaByte DB
Minor Compaction: This type occurs when multiple SSTables on disk are merged into one, helping in improving read performance by reducing the number of SSTable files a read operation may need to scan.
Major Compaction: This involves merging SSTables across multiple levels of the LSM tree, incorporating not just recent flushes from the memtable but more significantly, older data. This compaction is more comprehensive and can also remove deleted records (if their deletion markers, known as tombstones, can be safely purged).
The Compaction Process
The typical process of compaction works as follows:
- Selection of SSTables: The system first selects which SSTables to compact together based on their size and creation time.
- Merging and Sorting Data: Selected SSTables are merged, and their data is sorted. During this step, the system removes outdated data entries (overwritten or deleted data).
- Writing New SSTable: The result of this merged and cleaned data is written back to a new SSTable on disk, and once confirmed, the old SSTables are deleted.
These compaction operations are essential for ensuring that the disk space does not grow unbounded and that the read performance does not degrade over time due to an excessive number of disk seeks.
Performance Impact and Optimization
Compaction can be a resource-intensive process, especially if large amounts of data have accumulated. It can impact the overall performance of the database due to its CPU and I/O use. YugaByte DB optimizes this process by allowing compactions to run in the background and by providing configurations to throttle the compaction speed to mitigate its impact on foreground traffic.
Example of Compaction Configuration in YugaByte DB
Consider the following example command that configures the compaction throughput:
This configures the database to increase the memstore flush size and to limit compaction throughput to 300 MB/s.
Summary Table of Key Points
| Feature | Description |
| LSM Storage | Uses log-structured merge-tree to handle write-heavy workloads |
| Minor Compaction | Merges smaller, more recent SSTables to improve read efficiency |
| Major Compaction | Merges SSTables across multiple levels, removes obsolete data |
| Performance Optimization | Compactions are throttled to prevent interference with live operations |
Conclusion
Compaction plays a critical role in maintaining the efficiency of YugaByte DB. By understanding how it works and how to configure it properly, database administrators can ensure that their systems run optimally with effective disk space usage and minimized read latencies, thereby supporting high-performance applications.oroughly.
Related reading
- Would querying 60 columns from a Snowflake table would cost me more than querying 20 columns?
- Write-through cache Redis
- Write conflict in Dynamo
- Write Loss in Synchronous write in Redis Cluser
- Write to two Kafka topics in a single transaction using Spring Kafka
- Writes on Cassandra Network Partitioned Nodes
- Writes to geographically distributed database
- Wrong count with cassandra-cql

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.