Filesystem Compression: Transparent Space Savings and Performance Trade-offs

10 min

Master transparent filesystem compression with algorithms, compression ratios, and performance implications. Explore compression mechanisms through interactive visualizations.

Best viewed on desktop for optimal interactive experience

What Is Transparent Compression?

Transparent compression = Automatic compression/decompression by the filesystem—invisible to applications.

How it works:

  1. Application writes uncompressed data
  2. Filesystem compresses before writing to disk
  3. Application reads file
  4. Filesystem decompresses on-the-fly
  5. Application never knows compression happened

Benefits:

  • Space savings: 30-70% less disk usage (depends on data)
  • I/O performance: Read/write less data to disk
  • Cost reduction: Smaller storage needs
  • Bandwidth: Faster over slow disks (CPU decompress faster than disk read)

Trade-offs:

  • CPU overhead: Compression/decompression uses CPU
  • Incompressible data: Images/videos get no benefit (already compressed)
  • Memory usage: Decompression buffers

How Compression Works: Interactive Exploration

See transparent compression in action—from write to read, with different algorithms:

Interactive Compression Demo

Transparent Compression: Write and Read Flow

Step 1: Application Writes Uncompressed Data

Application Data:
Size:100KB
[2025-01-10] Log entry 1... [2025-01-10] Log entry 2...
What's happening:
  • Application writes 100KB log data
  • Data arrives at filesystem layer uncompressed
  • Application unaware of compression (transparent)
  • Filesystem will compress before disk write
  • No application changes needed!
Step 1 of 5

Compression Algorithms

Speed vs Ratio Trade-off

Fast (Low CPU) Balanced Slow (High CPU) LZ4 → ZSTD (level 1-3) → ZSTD (level 4-9) → ZLIB → LZMA │ │ │ Low ratio Medium ratio High ratio 10-30% savings 30-50% savings 40-70% savings

LZ4 (Fastest)

Characteristics:

  • Speed: Extremely fast (500+ MB/s compression)
  • Ratio: Low (10-30% savings)
  • CPU: Minimal overhead
  • Use case: SSDs, fast storage, CPU-limited systems

Btrfs:

mount -o compress=lz4 /dev/sda1 /mnt

ZFS:

zfs set compression=lz4 tank/data

Performance:

Compression: 500-700 MB/s Decompression: 1000+ MB/s Ratio: ~25% (text), ~10% (binaries)

Characteristics:

  • Speed: Very fast (200-400 MB/s)
  • Ratio: Good (30-50% savings)
  • CPU: Low-medium overhead
  • Levels: 1-22 (default: 3)
  • Use case: General purpose (best default)

Btrfs:

mount -o compress=zstd /dev/sda1 /mnt # Or specific level mount -o compress=zstd:3 /dev/sda1 /mnt

ZFS:

zfs set compression=zstd tank/data # ZFS uses fixed level, no tuning

Performance by level:

Level 1: 400 MB/s, 30% ratio Level 3: 250 MB/s, 40% ratio (default) Level 9: 100 MB/s, 50% ratio Level 15: 30 MB/s, 55% ratio

ZLIB (High Ratio)

Characteristics:

  • Speed: Moderate (50-100 MB/s)
  • Ratio: High (40-60% savings)
  • CPU: Medium overhead
  • Use case: Archival, cold storage

Btrfs:

mount -o compress=zlib /dev/sda1 /mnt # Levels 1-9 mount -o compress=zlib:9 /dev/sda1 /mnt # Max compression

ZFS:

zfs set compression=gzip tank/archive # gzip = zlib # Levels: gzip-1 to gzip-9 zfs set compression=gzip-9 tank/archive

Performance:

Compression: 80-120 MB/s (level 6) Decompression: 200-300 MB/s Ratio: ~45% (text), ~35% (binaries)

LZMA/XZ (Highest Ratio)

Characteristics:

  • Speed: Slow (10-30 MB/s)
  • Ratio: Very high (50-70% savings)
  • CPU: High overhead
  • Use case: Archival only (too slow for active use)

Not recommended for filesystem compression (too slow)

Algorithm Comparison

AlgorithmSpeedRatioCPUBest For
LZ4⚡⚡⚡⚡⚡⭐⭐LowSSD, real-time
ZSTD-1⚡⚡⚡⚡⭐⭐⭐LowGeneral purpose
ZSTD-3⚡⚡⚡⭐⭐⭐⭐MedDefault (best balance)
ZLIB-6⚡⚡⭐⭐⭐⭐⭐Med-HighCold storage
GZIP-9⭐⭐⭐⭐⭐HighArchival

Filesystem-Specific Compression

Btrfs Compression

Enable at mount:

# Algorithm only (default level) mount -o compress=zstd /dev/sda1 /mnt # Algorithm with level mount -o compress=zstd:3 /dev/sda1 /mnt # Force compress (even if ratio poor) mount -o compress-force=zstd /dev/sda1 /mnt

Heuristics:

# compress (default): Skip if ratio <80% mount -o compress=zstd /dev/sda1 /mnt # compress-force: Always compress (even if grows) mount -o compress-force=zstd /dev/sda1 /mnt

Per-file compression:

# Set compression for file/directory btrfs property set /path/to/file compression zstd # Disable for specific files btrfs property set /var/lib/mysql/data compression none

Defragment with compression:

# Recompress existing files btrfs filesystem defragment -r -czstd /mnt

ZFS Compression

Enable per dataset:

# Algorithm zfs set compression=zstd tank/data # Compression levels (gzip only) zfs set compression=gzip-9 tank/archive # Max # Disable zfs set compression=off tank/raw

Inheritance:

# Set on parent, children inherit zfs set compression=zstd tank # All datasets under tank/ use zstd # Override on child zfs set compression=lz4 tank/fast

Check compression ratio:

zfs get compressratio tank/data # NAME PROPERTY VALUE # tank/data compressratio 2.13x (53% savings)

Compression + dedup:

# Can combine (but expensive) zfs set compression=zstd tank/data zfs set dedup=on tank/data # Use with caution (RAM heavy)

NTFS Compression (Windows)

Enable on Windows:

# Right-click → Properties → Advanced → Compress # Or via command: compact /c /s:C:\path\to\compress

Algorithm:

  • Fixed algorithm (LZNT1 - similar to LZ77)
  • No tuning options
  • Per-file or per-directory

Linux NTFS-3G:

mount -t ntfs-3g -o compression /dev/sda1 /mnt

Performance:

  • Slower than modern algorithms
  • ~30-40% ratio on text
  • Not recommended for active use

ReFS Compression (Windows Server)

New in ReFS:

# Per-file compression (Server 2022+) Set-ItemProperty -Path C:\file.txt -Name Compression -Value LZ4

Algorithms:

  • LZ4 (fast)
  • Uncompressed (default)

When Compression Helps

Compressible Data

High compression (50-70%):

  • Text files (.txt, .log, .csv)
  • Source code (.c, .py, .js)
  • Configuration files (.conf, .xml, .json)
  • Database dumps (.sql)
  • Uncompressed images (.bmp, .tiff)

Medium compression (30-50%):

  • Binary executables (some patterns)
  • Virtual machine images (depends on content)
  • Documents (.doc, .pdf - if not already compressed)

Low/no compression (0-10%):

  • Compressed files (.zip, .gz, .xz)
  • Images (.jpg, .png, .webp)
  • Videos (.mp4, .mkv, .webm)
  • Audio (.mp3, .flac, .opus)
  • Encrypted data (appears random)

Performance Scenarios

Compression HELPS performance when:

Disk bandwidth < (CPU decompression speed) Example: HDD: 100 MB/s read ZSTD: 400 MB/s decompress 2x ratio: Effective 200 MB/s (2x faster!)

Compression HURTS performance when:

Disk bandwidth > (CPU decompression speed) Example: NVMe SSD: 3000 MB/s read ZSTD: 400 MB/s decompress Result: Bottlenecked by CPU (slower!)

Compression Ratios: Real World

Text Data

Source code (Linux kernel): Original: 1000 MB LZ4: 750 MB (25% savings, 1.33x) ZSTD-3: 600 MB (40% savings, 1.67x) ZLIB-6: 550 MB (45% savings, 1.82x)

Database Data

PostgreSQL database dump: Original: 10 GB LZ4: 7 GB (30% savings, 1.43x) ZSTD-3: 5 GB (50% savings, 2.00x) ZLIB-6: 4 GB (60% savings, 2.50x)

Mixed Filesystem

Typical Linux root filesystem: Original: 20 GB LZ4: 17 GB (15% savings, 1.18x) ZSTD-3: 14 GB (30% savings, 1.43x) ZLIB-6: 12 GB (40% savings, 1.67x) Lower than pure text (includes binaries, libs)

Performance Impact

CPU Overhead

Compression (write):

LZ4: 5-10% CPU ZSTD-1: 10-15% CPU ZSTD-3: 15-25% CPU ZLIB-6: 30-50% CPU

Decompression (read):

LZ4: 2-5% CPU ZSTD: 5-10% CPU ZLIB: 10-20% CPU

Modern CPUs: Overhead often negligible (multi-core, idle cores)

Throughput

Sequential read (1GB file):

No compression: HDD: 100 MB/s SSD: 500 MB/s With ZSTD (2x ratio): HDD: 200 MB/s effective (2x improvement!) SSD: 400 MB/s effective (CPU bottleneck)

Random I/O:

  • Compression helps less (can't skip decompression)
  • Smaller chunks = more overhead

Best Practices

1. Choose Right Algorithm

General purpose:

# Btrfs mount -o compress=zstd /dev/sda1 /mnt # ZFS zfs set compression=zstd tank/data

Fast SSD:

# Use LZ4 or disable mount -o compress=lz4 /dev/nvme0n1 /mnt # Or no compression (SSD fast enough)

Archival/cold storage:

# Use high compression mount -o compress=zlib:9 /dev/sda1 /archive zfs set compression=gzip-9 tank/archive

2. Selective Compression

Enable for compressible data:

# Btrfs: Compress /home, logs btrfs property set /home compression zstd btrfs property set /var/log compression zstd # Don't compress videos, photos btrfs property set /media compression none

ZFS datasets:

zfs create -o compression=zstd tank/code # Source code zfs create -o compression=lz4 tank/vm # VM images zfs create -o compression=off tank/media # Videos/photos

3. Monitor Compression Ratio

Btrfs:

compsize /mnt # Output: # Processed 12345 files, 10GB # Compressed size: 6GB # Ratio: 1.67x (40% savings)

ZFS:

zfs get compressratio,used,referenced tank/data # compressratio 2.13x # used 47GB # referenced 100GB (100GB → 47GB on disk)

4. Existing Data

Btrfs recompress:

# Defragment with compression btrfs filesystem defragment -r -czstd /mnt # Or per-file find /mnt -type f -exec btrfs filesystem defragment -czstd {} \;

ZFS: Data compressed on next write only

# Enable compression zfs set compression=zstd tank/data # Rewrite data to compress # (no built-in recompress - must copy/rewrite)

5. Database Considerations

Don't compress database files:

# Databases have own compression # Filesystem compression = double CPU overhead # Btrfs: Disable for databases chattr +C /var/lib/mysql # Also disables CoW btrfs property set /var/lib/mysql compression none # ZFS: Disable zfs set compression=off tank/mysql

Limitations

What Compression Can't Do

  1. Incompressible data: Can't compress JPEG, MP4, ZIP (already compressed)
  2. Random writes: May increase fragmentation
  3. CPU bound: Slow CPUs = performance loss
  4. Memory: Decompression needs RAM buffers

Compression + Other Features

Btrfs:

  • ✅ Compression + CoW: Works well
  • ✅ Compression + Snapshots: Shares compressed blocks
  • ⚠️ Compression + Defrag: Breaks sharing (recompresses)

ZFS:

  • ✅ Compression + CoW: Perfect together
  • ✅ Compression + Snapshots: Deduplicates compressed
  • ⚠️ Compression + Dedup: High memory usage
  • ✅ Compression + Encryption: Compress before encrypt (better)

Advanced: Compression Order

ZFS Compression + Encryption

Order matters:

Write path: 1. Application data (plain) 2. Compress (reduces size) 3. Encrypt (encrypted compressed data) 4. Write to disk Why: Compression needs patterns (encryption destroys patterns)

Enable both:

zfs create -o compression=zstd -o encryption=aes-256-gcm tank/secure # Automatically: compress then encrypt

Deduplication + Compression

Can combine but expensive:

# ZFS: Both enabled zfs set compression=zstd tank/data zfs set dedup=on tank/data # Dedup on compressed blocks # High RAM usage (256 bytes per block in ARC)

Recommendation: Use compression OR dedup, rarely both

Troubleshooting

Check If Compression Active

Btrfs:

# Check mount options mount | grep compress # Check per-file btrfs property get /path/to/file compression # Measure ratio compsize /mnt

ZFS:

# Check dataset property zfs get compression tank/data # Check actual ratio zfs get compressratio tank/data

Poor Compression Ratio

Causes:

  • Incompressible data (JPEG, MP4, ZIP)
  • Already compressed files
  • Encrypted data
  • Random/binary data

Solution:

# Disable for incompressible paths btrfs property set /media compression none zfs set compression=off tank/media

Performance Degradation

If compression slows system:

# Switch to faster algorithm mount -o remount,compress=lz4 /mnt zfs set compression=lz4 tank/data # Or disable entirely mount -o remount,compress=no /mnt zfs set compression=off tank/data

Key Takeaways

  • Transparent: Automatic compression/decompression (invisible to apps)
  • Algorithm: ZSTD recommended (best balance speed/ratio)
  • Space Savings: 30-50% typical (depends on data type)
  • Performance: Can improve I/O on slow disks (HDD)
  • CPU Overhead: Minimal on modern CPUs (5-15%)
  • Selective: Compress text/code, skip media/videos
  • Incompressible: JPEG, MP4, ZIP get no benefit
  • Enable Now: Modern filesystems (Btrfs, ZFS, ReFS)

If you found this explanation helpful, consider sharing it with others.

Mastodon