Sub topic 17
Tuning an LSM Engine for Your Workload
This chapter assumes you know the memtable, flush, SSTable read path, Bloom filters, tombstones, both compaction strategies, and write, read, and space amplification. Tuning means choosing which kind of amplification your workload can afford.
Start with the workload, not the knobs
Every setting moves cost between three things: write amplification, read amplification, and space amplification. You can't lower all three at once. First find out which one hurts, then change the setting that targets it.
The main settings
| Setting | Raising it | Cost |
|---|---|---|
| Memtable size | Fewer, larger flushes. Less write amplification. | More memory. Longer write-ahead log replay after a crash. |
| Bloom filter bits per key | Fewer wasted SSTable reads | More memory per key |
| Block cache size | More hot blocks served from memory | Less memory for other uses |
| Block size | Smaller index, better compression | Each lookup reads and decodes more data |
| Level size ratio (leveled) | Fewer levels, so faster reads | More rewriting per level, so higher write amplification |
| Compression | Smaller files | CPU on reads and compactions |
Write-heavy workloads
Choose size-tiered compaction. It rewrites each entry fewer times than leveled compaction. Enlarge the memtable so each flush absorbs more updates to the same keys. Watch for write stalls, which happen when flushes or compactions fall behind and the engine throttles incoming writes. Giving compaction more background threads is usually the first fix.
The cost is space and reads. Size-tiered compaction can keep several copies of a key across tables, and it can temporarily need extra disk space for a large merge.
Read-heavy workloads
Choose leveled compaction. Within each level, keys don't overlap, so a point lookup checks at most one table per level. Add Bloom filter bits next. At about 10 bits per key the false positive rate is near 1%, so almost every table without the key is skipped. Then size the block cache to hold your hot data.
A Bloom filter only answers "is this exact key here?". It does nothing for range scans, which must still merge every overlapping table.
Space-constrained workloads
Leveled compaction keeps stale versions to a small fraction of the data, so it has the lowest space amplification. Enable compression, preferably a fast algorithm on upper levels and a stronger one on the bottom level, where most data lives. Check for tombstones that linger because they haven't reached the bottom level. Delete-heavy workloads can waste space and slow scans until compaction removes them.
Common mistakes
- Making the memtable very large without accounting for recovery time. Replaying a large write-ahead log delays restart.
- Adding Bloom filter bits to fix slow range scans. They don't help.
- Tuning on a small dataset. Compaction costs appear only once data spans several levels.
Exercise
A service stores event logs. It writes constantly, rarely reads, and has plenty of disk space. Which compaction strategy and memtable setting fit best, and what is the main risk?
Show answer
Use size-tiered compaction with a larger memtable. Both reduce write amplification. The main risk is write stalls if compaction can't keep up, plus higher space and read amplification, which this workload can tolerate.