Data Storage Formats & OptimizationCompression Algorithms Trade-offsMedium⏱️ ~3 min

Choosing the Right Codec: Trade offs and Decision Framework

The Three Way Trade: Every compression decision involves trading space, CPU, and latency. A fourth dimension emerges in distributed systems: splittability, which determines whether large files can be processed in parallel.
Speed First Codecs
LZ4/Snappy: 2x ratio, 400 MB/s, 1 ms p99 latency
vs
Ratio First Codecs
XZ/bzip2: 10x ratio, high CPU, seconds per GB
Speed First Codecs (LZ4, Snappy): These prioritize low CPU and low latency, achieving 1.5x to 2.5x ratios but compressing at hundreds of MB/s per core. Engineers pick these for online services, message queues, and intermediate shuffle data because any extra 5 to 10 milliseconds on the hot path can violate a p99 Service Level Agreement (SLA). However, you pay with higher storage and network costs. The decision point: If your service has a 50 millisecond p99 SLA and currently sits at 40 milliseconds, adding even 10 milliseconds of compression overhead pushes you into violation territory under load spikes. Ratio First Codecs (XZ, bzip2): These reach 5x to 10x on text or logs, ideal for cold backups and compliance archives read rarely. The cost is high CPU and compression latencies sometimes reaching seconds for gigabyte scale blobs. This is acceptable when data is written once and read infrequently, but not in interactive analytics where analysts expect sub second or single digit second response times. Balanced Codecs (zlib, Zstd): Classic zlib gives about 3x ratio with moderate CPU. Zstd operates across a wide spectrum of levels: at the same ratio as zlib, it runs 3 to 5 times faster; at the same speed, it shrinks data by an extra 10 to 15 percent. Some teams prefer zlib for ecosystem maturity. Others adopt Zstd when CPU cost becomes a bottleneck or when they want dictionary based gains on small objects. The Splittability Problem: File level codecs like gzip are not splittable. A 1 GB gzip file must be processed by a single worker, which creates stragglers in distributed systems. If an analytics job scans 10 TB as large gzip files, a few workers spend minutes decompressing while others sit idle, dictating overall job completion time. Block aware formats that compress independent blocks solve this but incur slightly worse ratios and more metadata overhead. The trade is worth it for batch processing at scale.
"Choose based on read/write ratio and latency budget. Systems that are write heavy with strict latency SLAs (over 80% writes, under 50 ms p99) need speed first codecs. Read heavy cold storage (written once, read rarely) justifies ratio first codecs."
Decision Framework: For online services with p99 latency SLAs under 100 milliseconds, use Snappy or LZ4. For intermediate data with balanced read and write patterns, use Zstd at low to mid levels. For cold archives accessed infrequently, use Zstd at high levels or XZ. For distributed batch processing, ensure your format uses independent compressed blocks to enable parallelism.
💡 Key Takeaways
Speed first codecs (LZ4/Snappy) achieve 2x ratio at hundreds of MB/s with under 1 ms p99 latency, critical for services with tight SLAs but costing more in storage
Ratio first codecs (XZ/bzip2) reach 5x to 10x but take seconds per gigabyte, suitable only for cold archives written once and read rarely
Splittability matters for distributed batch processing: gzip forces single worker per file creating stragglers, while block compressed formats enable parallel processing
Zstd operates across a wide spectrum: at zlib ratios it runs 3 to 5 times faster, or at same speed achieves 10 to 15 percent better compression
Decision framework: under 50 ms p99 SLA use Snappy, balanced workloads use Zstd mid level, cold archives use Zstd high or XZ, batch systems need splittable formats
📌 Examples
1Online service at 40 ms p99 with 50 ms SLA cannot afford 10 ms compression overhead, must use Snappy or LZ4 to stay within budget
2Analytics job scanning 10 TB as large gzip files hits stragglers when single workers spend minutes decompressing while others idle, fixed by block compression
3Write heavy system (over 80% writes) with 5 indexes sees throughput drop from 50,000 to 8,000 inserts per second, favoring speed over aggressive compression
← Back to Compression Algorithms Trade-offs Overview