Choose a Compaction Strategy¶
- Size-tiered Compaction Strategy (STCS)
- Leveled Compaction Strategy (LCS)
- Time-window Compaction Strategy (TWCS)
- Date-tiered Compaction Strategy (DTCS)
- Which strategy is best
Scylla implements the following compaction strategies in order to reduce read amplification, write amplification, and space amplification, which causes bottlenecks and poor performance. These strategies include:
- Size-tiered compaction strategy (STCS) - triggered when the system has enough (four by default) similarly sized SSTables.
- Leveled compaction strategy (LCS) - the system uses small, fixed-size (by default 160 MB) SSTables divided into different levels.
- Time-window compaction strategy (TWCS) - designed for time series data; replaced Date-tiered compaction.
- Date-tiered compaction strategy (DTCS) - designed for time series data.
This document covers how to choose a compaction strategy and presents the benefits and disadvantages of each one. If you want more informaiton on compaction in general or on any of these strategies, refer to the Compaction Overview. If you want an explanation of the CQL commands used to create a compaction strategy, refer to Compaction CQL Reference .
The premise of Size-tiered Compaction Strategy (STCS) is to merge SSTables of approximately the same size.
This is a popular strategy for LSM workloads. It results in a low and logarithmic (in size of data) number of SSTables, and the same data is copied during compaction a fairly low number of times. Use the table in Which strategy is best to determine if this is the right strategy for your needs.
This strategy has the following drawbacks (particularly with writes):
- Continuously modifying existing rows results in each row being split across several SSTables, making reads slow, which doesn’t happen in Leveled compaction.
- Obsolete data (overwritten or deleted columns) in a very large SSTable remains, wasting space, for a long time, until it is finally merged.
- Compaction requires a lot of temporary space as the new larger SSTable is written before the duplicates are purged. In the worst case up to half the disk space needs to be empty to allow this to happen.
To implement this strategy
Set the parameters for Size-tiered compaction.
Leveled Compaction Strategy (LCS) uses small, fixed-size (by default 160 MB) SSTables divided into different levels. Each level represents a run of a number of SSTables.
With the leveled compaction strategy, the following benefits are noteworthy:
- SSTable reads are efficient. The great number of small SSTables doesn’t mean we need to look up a key in that many SSTables, because we know the SSTables in each level have disjoint ranges, so we only need to look in one SSTable in each level. In the typical case, only one SSTable needs to be read.
- The other factors making this compaction strategy efficient are that at most 10% of space will be wasted by obsolete rows, and only enough space for ~10x the small SSTable size needs to be reserved for temporary use by compaction.
Use the table in Which strategy is best to determine if this is the right strategy for your needs.
The downside of this method is there is two times more I/O on writes, so it is not as good for workloads which focus on writing mostly new data.
Only one compaction operation on the same table can run at a time, so compaction may be postponed if there is a compaction already in progress. As the size of the files is not too large, this is not really an issue.
To implement this strategy
Set the parameters for Leveled Compaction.
Time-window compaction strategy was introduced in Cassandra 3.0.8 for time-series data as a replacement for Date-tiered Compaction Strategy (DTCS). Time-Window Compaction Strategy compacts SSTables within each time window using Size-tiered Compaction Strategy (STCS). SSTables from different time windows are never compacted together. You set the TimeWindowCompactionStrategy parameters when you create a table usung a CQL command.
- Keeps entries according to a time range, making searches for data within a given range easy to do, resulting in better read performance
- Improves over DTCS in that it reduces the number to huge compactions
- Allows you to expire an entire SSTable at once (using a TTL) as the data is already organized within a time frame
Date-Tiered Compaction is designed for time series data. This strategy was introduced with Cassandra 2.1. It is only suitable for time-series data. This strategy is not recommended and has been replaced by Time-window compaction.
Every workload type may not work well with every compaction strategy. Unfortunately, the more mixed your workload, the harder it is to pick the correct strategy. This table presents what can be expected depending on the strategy you use for the workload indicated, allowing you to make a more informed decision. Keep in mind that best choice for our testing may not be the best choice for your environment. You may have to experiment to find which strategy works best for you.
Compaction Strategy Matrix
|Write-only|| and |
|Overwrite|| and |
|Read-mostly, with few updates|||
|Read-mostly, with many updates|||
|Time Series|| and |
1 When using Size-tiered with write-only loads it will use approximately 2x peak space - SA
2 When using Leveled Compaction with write only loads you will experience 2x writes - WA
3 When using Size-tired with Overwrite loads, SA occurs
4 When using Leveled Compaction with overwrite loads, WA occurs
5 When using Size-tiered with mostly read loads with little updates, SA occurs
6 When using Leveled with mostly read loads with many updates, WA occurs in excess
7 When using Size-tiered with Time Series workloads, SA, RA, and WA occurs.
8 When using Leveled with Time Series workloads, SA and WA occurs.