Compaction
MynaDB uses LSM-tree style multi-level compaction to optimize storage efficiency and query performance. Small files from recent writes are progressively merged into larger, more efficient files.
Overview
When data is written to MynaDB, it first goes to an in-memory cache, then gets flushed to disk as small Parquet files (Level 0). Over time, these small files accumulate, which can slow down queries. Compaction solves this by merging files together.
- Fewer files - Reduces file system overhead
- Faster queries - Less I/O, better file scanning
- Better compression - Larger files compress more efficiently
- Reduced storage - Eliminates duplicate and obsolete data
LSM-Tree Architecture
MynaDB implements a Log-Structured Merge-tree (LSM-tree) inspired architecture for managing Parquet files:
Write Request
│
▼
┌─────────────┐
│ In-Memory │ ← Fast writes
│ Cache │
└─────────────┘
│ flush (every 30s)
▼
┌─────────────┐
│ Level 0 │ ← Many small files (1-10 MB)
│ (L0_*.parquet)
└─────────────┘
│ compaction (when ≥4 files)
▼
┌─────────────┐
│ Level 1 │ ← Fewer, larger files (10-50 MB)
│ (L1_*.parquet)
└─────────────┘
│ compaction
▼
┌─────────────┐
│ Level 2 │ ← Even fewer files (50-200 MB)
│ (L2_*.parquet)
└─────────────┘
│
▼
... ← Up to Level 5
Key Properties
| Property | Description |
|---|---|
| Write-optimized | New data appends to L0 without modifying existing files |
| Background merging | Compaction runs asynchronously, doesn't block writes |
| Level-based | Each level contains progressively larger, more optimized files |
| Size amplification | Controlled by level ratios and compaction thresholds |
Compaction Levels
MynaDB supports up to 6 levels (L0-L5):
| Level | File Prefix | Typical Size | Description |
|---|---|---|---|
| L0 | L0_*.parquet |
1-10 MB | Direct flush from cache, many small files |
| L1 | L1_*.parquet |
10-50 MB | First compaction level |
| L2 | L2_*.parquet |
50-200 MB | Second compaction level |
| L3 | L3_*.parquet |
200-500 MB | Third compaction level |
| L4 | L4_*.parquet |
500 MB - 1 GB | Fourth compaction level |
| L5 | L5_*.parquet |
1+ GB | Final level, fully compacted |
File Naming Convention
# Format: L{level}_{timestamp}_{uuid}.parquet
L0_1703251234567_a1b2c3d4.parquet # Level 0, recently flushed
L1_1703251300000_e5f6g7h8.parquet # Level 1, first compaction
L2_1703252000000_i9j0k1l2.parquet # Level 2, second compaction
Compaction Strategies
MynaDB supports two compaction strategies that work together:
1. LSM Compaction (Default, Continuous)
Runs continuously in the background, merging files when thresholds are reached. This is the primary compaction mechanism.
# When L0 has 4+ files:
L0_file1.parquet ─┐
L0_file2.parquet ─┼─► Merge ─► L1_merged.parquet
L0_file3.parquet ─┤
L0_file4.parquet ─┘
# When L1 has 4+ files:
L1_file1.parquet ─┐
L1_file2.parquet ─┼─► Merge ─► L2_merged.parquet
L1_file3.parquet ─┤
L1_file4.parquet ─┘
# And so on up to L5...
2. Full Compaction (Scheduled/Manual)
Merges ALL files at a level into a single file. Useful for aggressive optimization but more resource-intensive.
# Full compaction at L0:
L0_file1.parquet ─┐
L0_file2.parquet ─┤
L0_file3.parquet ─┼─► Merge ALL ─► L1_single.parquet
L0_file4.parquet ─┤
L0_file5.parquet ─┤
L0_file6.parquet ─┘
Configuration
Compaction is configured in config.toml:
[compaction]
# Minimum files at a level before compaction triggers
min_files = 4
# Maximum compaction level (L0-L5)
max_level = 5
# Parallel LSM compaction tasks
max_concurrent_compactions = 8
# Parallel full compaction tasks (lower to avoid OOM)
full_compaction_max_concurrent = 2
# Schedule full compaction at specific hours (0-23)
# Example: Run at 2 AM and 2 PM
full_compaction_hours = [2, 14]
# Enable/disable automatic compaction
enabled = true
Configuration Options Explained
| Option | Default | Description |
|---|---|---|
min_files |
4 | Files needed at a level to trigger compaction |
max_level |
5 | Highest compaction level (L0-L5) |
max_concurrent_compactions |
8 | Parallel LSM compaction workers |
full_compaction_max_concurrent |
2 | Parallel full compaction workers (memory-intensive) |
full_compaction_hours |
[] | Hours (0-23) to run scheduled full compaction |
enabled |
true | Enable/disable automatic compaction |
Full compaction loads all files into memory for merging. Set
full_compaction_max_concurrent to a low value (1-2) to avoid
out-of-memory errors on systems with limited RAM.
Manual Compaction
You can trigger compaction manually via the HTTP API:
Compact Entire Database
# Compact all tables in a database
curl -X POST http://localhost:7000/compact/mydb
# Response
{
"status": "compaction_started",
"database": "mydb"
}
Compact Specific Table
# Compact a specific table
curl -X POST http://localhost:7000/compact/mydb/cpu
# Response
{
"status": "compaction_started",
"database": "mydb",
"table": "cpu"
}
Force Full Compaction
# Force full compaction (merge all files)
curl -X POST http://localhost:7000/compact/mydb?full=true
# Response
{
"status": "full_compaction_started",
"database": "mydb"
}
Monitoring Compaction
Check File Counts
Monitor compaction progress by checking file counts per level:
# Count files per level
find /data/stats/mydb/default/cpu -name "L*.parquet" | \
sed 's/.*\(L[0-9]\).*/\1/' | sort | uniq -c
# Output:
# 12 L0 ← Many L0 files, compaction needed
# 3 L1
# 1 L2
Check Partition Files
# List partitions and file counts via API
curl http://localhost:7000/partitions/mydb/default/cpu
# Response
{
"partitions": [
{
"date": "2024-01-15",
"files": 5,
"size_bytes": 52428800
},
{
"date": "2024-01-14",
"files": 2,
"size_bytes": 104857600
}
]
}
Server Logs
Compaction events are logged for monitoring:
INFO Compaction started: mydb.default.cpu L0 → L1 (4 files)
INFO Compaction completed: mydb.default.cpu L0 → L1 (merged to 1 file, 45MB)
INFO Full compaction started: mydb.default.cpu
INFO Full compaction completed: mydb.default.cpu (12 files → 1 file, 234MB)
Best Practices
Tune min_files
Lower values (2-3) = more aggressive compaction, higher CPU usage.
Higher values (6-8) = less frequent compaction, more files to scan.
Schedule Full Compaction
Run full compaction during off-peak hours using full_compaction_hours.
Example: [2, 3, 4] for 2-4 AM.
Monitor Disk Space
Compaction temporarily uses extra disk space while merging. Keep 20% free space for compaction operations.
Balance Query vs Write
More compaction = faster queries but higher CPU.
Less compaction = faster writes but slower queries.
Recommended Settings by Workload
| Workload | min_files | max_concurrent | Notes |
|---|---|---|---|
| Write-heavy | 6-8 | 4 | Less frequent compaction, prioritize writes |
| Read-heavy | 2-3 | 8 | Aggressive compaction, optimize for queries |
| Balanced | 4 | 8 | Default settings work well |
| Low memory | 4 | 2 | Limit concurrent compactions |
Compaction and Retention
Compaction and retention work together but serve different purposes:
- Compaction - Merges files within a partition for efficiency
- Retention - Deletes entire partitions older than a threshold
When retention deletes old partitions, it also removes any compacted files in those partitions. There's no need to compact data that will soon be deleted.
If you have short retention periods (e.g., 7 days), consider using less aggressive compaction settings since the data will be deleted before compaction provides significant benefits.