Compression is everywhere in a self-hosted stack — web server responses, log rotation, database backups, container image layers, and archive transfers. Choosing the right compression algorithm can dramatically reduce storage costs, speed up transfers, and lower bandwidth usage. But with zstd, brotli, lz4, and gzip all available, which one should you deploy where?

Why Compression Matters for Self-Hosted Servers

Every self-hosted server touches compression at multiple points in its lifecycle. Web servers compress responses like HTML, CSS, JS, and JSON before sending to browsers. Log rotation compresses multi-gigabyte daily logs to save disk space. Backup systems compress database dumps and file archives before storage. Container images use compressed layers for distribution and storage. File transfers benefit from compression when syncing between servers.

The wrong compression choice can mean three times larger backup files, twice as slow web responses, or saturated CPU during log rotation. Understanding the trade-offs between speed, ratio, and CPU usage is critical for production self-hosted environments.

Comparison: zstd vs brotli vs lz4 vs gzip

Featurezstdbrotlilz4gzip
Compression ratioExcellent (2.8-3.5x)Excellent (2.6-3.3x)Good (2.0-2.5x)Moderate (2.3-2.8x)
Compression speedVery fast (400+ MB/s)Moderate (100+ MB/s)Extremely fast (500+ MB/s)Fast (200+ MB/s)
Decompression speedVery fast (1,500+ MB/s)Fast (500+ MB/s)Extremely fast (4,000+ MB/s)Fast (300+ MB/s)
Dictionary supportYes (trainable)Yes (static + shared)NoNo
Streaming supportYesYesYesYes
Browser supportLimited (Chrome 123+)Universal (all browsers)NoneUniversal (all browsers)
Best use caseBackups, logs, DB dumpsWeb content, static assetsReal-time data, cachingGeneral purpose, compatibility
Multi-threadingYes (zstdmt)NoNoVia pigz
GitHub Stars24,000+14,000+10,000+N/A (GNU coreutils)

zstd: The Modern All-Rounder

Zstandard (zstd) from Meta offers the best balance of speed and compression ratio. It’s become the de facto standard for modern infrastructure — used by Linux kernel compression, Docker image layers, RPM packages, and Rsync.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Install zstd
apt install zstd

# Compress with default level (3 of 19)
zstd large-log-file.log          # → large-log-file.log.zst

# Fast compression (level 1) for real-time use
zstd -1 large-log-file.log

# Maximum compression (level 19) for archival
zstd -19 --ultra database-backup.sql

# Multi-threaded compression
zstd -T0 large-dataset.tar       # Uses all CPU cores

# Dictionary-based compression for small files
zstd --train small-files/* -o dict
zstd -D dict small-file.txt
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
version: "3.8"
services:
  logrotate:
    image: alpine:latest
    container_name: logrotate
    volumes:
      - /var/log:/logs
      - ./logrotate.conf:/etc/logrotate.conf:ro
    entrypoint: ["/bin/sh", "-c", "apk add logrotate zstd && crond -f"]
    restart: unless-stopped

zstd’s killer feature is trainable dictionaries. For servers that process many small, similar files such as API responses or JSON payloads, training a dictionary can improve compression ratios by two to three times over the default.

brotli: The Web Content Champion

Brotli from Google was designed specifically for web content compression. It achieves excellent ratios on text-based content including HTML, CSS, JS, and JSON, and is supported by all modern browsers.

1
2
3
4
5
6
7
8
# Nginx with brotli compression
http {
    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/css application/json
                application/javascript text/xml;
    brotli_static on;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
version: "3.8"
services:
  nginx-brotli:
    image: fholzer/nginx-brotli:latest
    container_name: nginx-brotli
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./html:/usr/share/nginx/html:ro
    ports:
      - "80:80"
      - "443:443"
    restart: unless-stopped

Brotli’s primary limitation is that it’s designed for web content — it’s not a general-purpose compression tool like zstd or gzip. For command-line use on servers, brotli shines when pre-compressing static web assets for Nginx brotli_static usage.

lz4: The Speed Demon

LZ4 prioritizes speed above all else. It compresses at over 500 megabytes per second and decompresses at over 4 gigabytes per second on modern hardware — making it the go-to choice for real-time compression where latency matters more than ratio.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Install lz4
apt install lz4

# Fast compression
lz4 large-file.tar large-file.tar.lz4

# Streaming compression (pipe-friendly)
tar cf - /data | lz4 - data-backup.tar.lz4

# Decompress with lz4
lz4 -d data-backup.tar.lz4 data-backup.tar

lz4 is ideal for database streaming replication where it compresses WAL logs without adding latency, real-time log forwarding before sending to central syslog, cache compression in Redis and Memcached, and ZFS compression where lz4 is the default algorithm.

gzip: The Universal Default

Gzip remains the most widely compatible compression format. It’s available everywhere — every browser, every OS, every tool. While not the fastest or the most efficient, its universality makes it the safest choice for interoperability.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Standard gzip compression
gzip large-file.log              # → large-file.log.gz

# Maximum compression
gzip -9 database-dump.sql

# Fast compression
gzip -1 large-dataset.tar

# Multi-threaded with pigz
pigz -p 4 large-file.tar         # → large-file.tar.gz

For self-hosted servers, gzip is the right choice when you’re generating archives that need to be readable by others, when browser compatibility is more important than 20 to 30 percent better compression, or when you’re working in constrained environments where zstd and brotli aren’t available.

Choosing the Right Tool for Your Stack

Here’s a decision matrix for common self-hosted scenarios:

ScenarioRecommended ToolReason
Nginx/Caddy web responsesbrotli (primary) + gzip (fallback)Best web compression, universal fallback
Database backup dumpszstd (level 3-5)Best ratio/speed balance for large files
Log rotation (daily GBs)zstd (level 1-3)Fast enough to not block rotation
Real-time log forwardinglz4Minimum latency, acceptable ratio
Container image layerszstdDocker/OCI default, best balance
Static asset pre-compressionbrotli + gzipDual compression for browser negotiation
File transfer between serverszstdBest combination of speed and ratio
Archive for long-term storagezstd (level 19)Maximum compression for cold storage

Why Self-Host Your Compression Pipeline?

Compression is one of the easiest performance wins in a self-hosted stack. Unlike cloud services where compression happens behind the scenes and you pay for the CPU, self-hosting lets you choose the right algorithm for each workload. Brotli typically delivers 20 to 30 percent smaller responses than gzip, directly reducing your bandwidth costs. Zstd often reduces backup sizes by 40 to 60 percent compared to uncompressed storage, extending the life of your NAS or VPS disk. LZ4’s extreme speed means compression has negligible impact on your application’s CPU budget.

For web server optimization, see our Nginx vs Caddy vs OpenResty guide. For backup strategy comparisons, check our Restic vs Borg vs Kopia guide. For log management strategies, see our log rotation guide.

FAQ

Should I replace gzip with zstd for everything?

Not necessarily. For web content, brotli provides better compression than both for browser-facing responses. For internal server compression like logs, backups, and transfers, zstd is almost always a better choice than gzip — it’s faster and compresses better. Keep gzip for compatibility when you need to share archives with others.

Does brotli work for non-web content?

Technically yes, but it’s not recommended. Brotli was designed and tuned for web content in text-based formats like HTML, CSS, and JS. For general-purpose compression on servers, zstd consistently outperforms brotli in speed while matching or exceeding its compression ratio.

How much CPU does compression actually use?

Modern compression algorithms are remarkably efficient. zstd at level 3 uses about 3 percent CPU on a modern server while compressing at 400 megabytes per second. lz4 uses less than 1 percent CPU at over 500 megabytes per second. The CPU cost is negligible compared to the I/O and storage savings — compressing a 1 GB log file with zstd typically takes 2 to 3 seconds and reduces it to about 300 MB.

Can I use multiple algorithms in my stack?

Absolutely, and this is the recommended approach. Use brotli for web responses, zstd for backups and logs, and lz4 for real-time data streams. Each tool excels at its specific use case, and running multiple compressors adds negligible overhead.

How do I measure real-world compression performance?

Benchmark with your actual data — every dataset is different. Run a quick test by compressing a sample of your actual files with each tool and comparing both the compressed size and time taken. Tools like hyperfine can measure execution time precisely.

What about xz (LZMA) compression?

xz achieves the highest compression ratios but is extremely slow, roughly 10 to 50 times slower than zstd. It’s suitable for one-time archival where compression time doesn’t matter but storage savings are critical. For regular server operations, zstd at higher levels between 15 and 19 approaches xz ratios while being dramatically faster.


💰 想测试你的市场判断力?我用 Polymarket 做预测市场交易——这是全球最大的预测市场平台,从大选结果到加密货币监管,什么都可以押注。和赌博不同,这是真正的信息市场:你懂的信息越多,胜率越高。我靠预测科技事件的走向已经赚了不少。用我的邀请链接注册:Polymarket.com