Compression is one of those tools everyone uses and almost nobody thinks about. It sits under database files, inside backups, behind HTTP responses and in container layers. Two algorithms dominate the fast end of that world: LZ4 and Zstd. Both are open, both are battle-tested, and both are maintained by people who care about real workloads rather than benchmark toys. Yet they are built for opposite situations, and picking the wrong one is a quiet way to waste either CPU or bandwidth.
LZ4 buys extreme speed by accepting a modest compression ratio, while Zstd buys a much better ratio by spending more CPU and memory, and the right choice depends on which resource is scarce in your system.
That sentence is the whole decision in one line, but the interesting part is why each design ends up there and what it means for a service you actually run.
Part One: What is being compared
LZ4 was designed around a single idea: a compressor should never be much slower than a memory copy. Its format is a simple sequence of literals and match references, and its decoder is a tight loop that a CPU can run at several gigabytes per second per core. There is no entropy coding stage, no complex modelling. The trade is visible in the output: a file that shrinks to 45 or 50 percent of its size under a stronger codec may only reach 60 or 70 percent under LZ4.
Zstd, also from Facebook, keeps the same family of match-based compression but adds a finer search, larger windows and an optional entropy-coding stage called FSE. It exposes a large dial of levels, from negative numbers that are faster than LZ4 on some data to high numbers that rival older general-purpose codecs. The decoder stays fast, often a few gigabytes per second, but the encoder at mid to high levels can be many times slower than LZ4. Crucially, Zstd can train a dictionary on sample data, which makes it unusually good at compressing many small, similar records.
So this is not a contest where one algorithm is simply newer and better. They optimise different points on the same curve.
Part Two: The arithmetic of the trade
Consider a service that writes one terabyte of log data a day. With LZ4 at a 60 percent ratio, that becomes roughly 600 gigabytes on disk. With Zstd at 45 percent, it becomes 450 gigabytes. The saving is 150 gigabytes a day, or about 55 terabytes a year. If object storage costs a few cents per gigabyte per month, the annual storage bill drops by something like two to three hundred dollars, and the backup and replication traffic drops in the same proportion.
Now price the CPU. Compressing one terabyte with LZ4 might take a few minutes of one core. Doing the same with Zstd at a medium level could take several times longer, perhaps tens of minutes spread across cores. On a machine that is already idle, that cost is nearly free. On a hot path where every millisecond of latency shows up in a user-facing metric, it is not free at all. The same trade appears in reverse on decompression: both decoders are fast, so read-heavy workloads tend to favour whichever format gives the smaller file, because fewer bytes means less I/O even if the decoder does a little more work.
A useful rule of thumb is that LZ4 wins when compression sits on a latency-critical path and CPU is expensive, while Zstd wins when bytes are expensive and you can afford the encoder time.
That rule explains why LZ4 shows up inside in-memory caches, live telemetry streams and database page formats, where the compressor runs constantly and latency is king. Zstd shows up in backups, object stores, package registries and log archives, where data is written once and read rarely, so a slower encoder is a good bargain for a smaller file.
Part Three: Where each model breaks
LZ4 becomes the wrong choice when you are paying to move the same compressed bytes many times. A build artefact compressed with LZ4 and downloaded by thousands of developers costs more in network egress than the extra CPU that Zstd would have spent once at build time. The one-time encoding cost is amortised across every download, and the stronger ratio wins by a wide margin.
Zstd becomes the wrong choice when you are compressing tiny, ephemeral values on a hot path, such as individual cache entries on every request. At small sizes the ratio advantage shrinks, the per-call overhead matters, and the encoder's extra cycles show up directly in response time. Zstd's dictionary mode can help with small records, but a dictionary has to be trained and kept stable, which adds operational complexity that a simple LZ4 call does not need.
There is also the question of compatibility. Both formats have stable specifications and wide library support, so a mixed fleet is not usually a problem. But Zstd's level dial means two services can produce very different byte streams from the same input, and a team that does not pin a level may see output sizes drift after an upgrade. LZ4 has far fewer knobs, which makes it predictable in a boring, useful way.
Part Four: A everyday example
Think about a photo backup app on a phone. If it compresses each photo before uploading on a mobile connection, the user cares about upload time and battery. LZ4 might finish the compression quickly but send a larger file, keeping the radio active longer and draining the battery through the network rather than the CPU. Zstd might spend a little more CPU but cut the upload enough to finish sooner overall. Which one is better depends on whether the phone's radio or its processor is the bottleneck that day.
Now think about the same app restoring a backup years later. Decompression speed matters more than ratio because the user is waiting. Both decoders are fast, but the Zstd file is smaller, so it arrives from cloud storage faster. The restore path often favours the stronger codec even when the upload path did not.
What is actually transferable
The real lesson is that compression is not a property of a file but a property of a system. A ratio is only valuable if you pay for the bytes you save, and speed is only valuable if the CPU you spend could not be used for something else. Ask where the compressor runs, how often, and who pays for the output, and the choice usually becomes obvious.
A second lesson is to measure with your own data. Synthetic benchmarks on text say very little about database pages, protobuf messages or already-compressed images. Both libraries are easy to try, so the honest approach is to run both on a representative sample and price the result against your actual storage and bandwidth costs.
LZ4 and Zstd are not rivals in a single race. LZ4 sells speed by spending bytes, and Zstd sells bytes by spending time, and a well-designed system often uses both in different places.
SOL: HCqPmPJM3NVtS6YrmVaTZYnuP8XZ2PFgQZKT94VK1LkS
Creating this takes real effort — thank you for the support.
Top comments (0)