The Compression Trade-off
gzip vs LZ4 vs Zstd vs XZ vs Brotli - ratio vs speed, measured
The measured result

Zstd-3 dominates gzip: same compression ratio, ~5× faster to compress and 2× faster to decompress. LZ4 is for raw speed (819 MB/s decompress); XZ and Brotli reach the best ratios (~6.2×) but compress at 1–3 MB/s.
5×
Zstd-3 vs gzip compress speed
6.2×
best ratio (Brotli-11)
100%
reproducible (Docker)
What's inside
- The three costs of compression: ratio, compress speed, decompress speed - and why they trade off.
- How gzip, LZ4, Zstd, XZ, and Brotli work, and why they land where they do.
- A measured ratio-vs-speed comparison from a reproducible Docker harness.
- A workload-driven framework for choosing a codec and level, with four worked settings.
- Practical details that decide real deployments: dictionaries, block size, threads.
- A benchmarking protocol and full references to the codec specs.
Table of contents
- Preface
Preface
Frames the book's purpose - helping engineers choose a compression codec deliberately rather than by habit - and lays out its scope (gzip, LZ4, Zstandard, XZ, and Brotli), audience, and chapter organization.
- Chapter 1
The Compression Problem
Establishes that every lossless codec involves three competing costs - compression ratio, compression throughput, and decompression throughput - and explains why pushing ratio higher costs compression speed disproportionately at the top of the range.
- Chapter 2
Inside the Codecs
Walks through the shared two-stage design (LZ77-style dictionary matching followed by entropy coding) behind gzip, LZ4, Zstandard, XZ, and Brotli, and shows how each codec's window size and entropy-coding choices produce its position on the trade-off curve.
- Chapter 3
The Measured Comparison
Reports the book's own benchmark of all five codecs and multiple levels on a 10.4 MB real source-code corpus, presenting ratio, compress throughput, and decompress throughput in a table and a log-scale ratio-vs-speed plot, and concludes that Zstandard has largely displaced gzip on the frontier.
- Chapter 4
Choosing a Codec
Converts the measured trade-off into a decision framework built around three questions (hot-path latency, write-once/read-many, and scarce resource), applied via a decision diagram and a table of four representative deployment settings.
- Chapter 5
Compression in Practice
Covers the operational details that determine real-world results beyond codec choice: sweeping levels on your own data, using trained dictionaries for small records, block size and multithreading trade-offs, and matching the codec to whether a workload is read-heavy or write-heavy.
- Chapter 6
Conclusion: Measure Your Own Data
Summarizes the five codecs' relative positions on the trade-off curve and closes with a five-step protocol for benchmarking compression on the reader's own data, plus common benchmarking mistakes to avoid.
Who this is for
- Backend, data, or platform engineers who need to choose a compression codec for a storage format, message bus, or API
- Engineers currently picking a codec 'by habit' who want to make the choice deliberately, with reasons
- Anyone deciding among gzip, LZ4, Zstandard, XZ, or Brotli for log pipelines, columnar data files, RPC payloads, backups, or web responses
- Readers who want to run a reproducible benchmark harness on their own data rather than rely on codec folklore
- Working engineers without a background in information theory who still need to reason about compression trade-offs
The preface states the book assumes a working engineer who moves or stores data, and explicitly notes that no background in information theory is required, since the mechanisms are built up from first principles where they matter.
How this was measured
The benchmark harness (in the book's bench/ directory) compresses one fixed corpus - 10.4 MB of concatenated Python 3.11 standard-library source code - with each of five codecs (gzip/DEFLATE, LZ4, Zstandard, XZ/LZMA, Brotli) across several levels per codec, single-threaded (-T1 where supported). It runs on an AMD Ryzen 7 5700X under Docker Desktop using the debian:12-slim image, dated 2026-06-30, and is reproducible via a documented Docker command. Compression ratio (input size divided by output size), compress throughput, and decompress throughput (both in MB/s, from wall-clock time) are measured directly by the harness, not cited from external sources; the book notes ratio is hardware-independent and fully reproducible while throughput is machine-specific.
From the preface
Almost every system compresses something: log pipelines, columnar data files, RPC payloads, backups, web responses. The choice of codec is usually made once, by habit, and then quietly taxes the system forever, either in CPU spent compressing and decompressing, or in storage and bandwidth spent on bytes that a better codec would have removed. This book is about making that choice deliberately.
Frequently asked
What exactly does the benchmark measure?
For gzip (DEFLATE), LZ4, Zstandard, XZ (LZMA), and Brotli across several levels each, the harness measures compression ratio, compression throughput, and decompression throughput on a single fixed corpus - 10.4 MB of real Python standard-library source code - run single-threaded on an AMD Ryzen 7 5700X inside Docker.
Do I need to run the benchmark harness myself?
It's not required to read the book, but it's recommended for any real decision: the book states that running the harness on your own data is 'the only comparison that should decide a production choice,' since ratio depends heavily on your actual data and throughput depends on your actual hardware. The harness lives in the bench/ directory and is reproducible with a single Docker command.
Is this beginner-friendly?
The preface says the book assumes a working engineer who moves or stores data, and explicitly states that no background in information theory is required, since the underlying mechanisms are built up from first principles where they matter.
Which codecs and workloads does the book cover?
It compares gzip (DEFLATE), LZ4, Zstandard, XZ (LZMA), and Brotli across multiple levels, for lossless general-purpose compression of real data. Domain-specific schemes such as columnar encodings or image and audio codecs are explicitly out of scope, though the book notes the same trade-off reasoning applies to them.
Are the numbers in the book real measurements or estimates?
The book states plainly that the benchmark figures 'are measured by the harness in the bench/ directory and documented in the appendix; they are not estimates.' It notes compression ratio is hardware-independent and fully reproducible, while throughput is machine-specific, which is why the exact test environment (AMD Ryzen 7 5700X, Docker, debian:12-slim, dated 2026-06-30) is stated.
What's the book's main practical takeaway?
That there is no single best codec, only a position on a ratio-versus-speed curve, and that the choice should follow whichever resource - CPU time, storage, or bandwidth - the workload can least afford to spend. The measured data specifically shows Zstandard largely displacing gzip on that curve.
Convinced? Get the full book.