Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Zstd vs LZ4: Buying Ratio With Time or Speed With Bytes

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…

Compression is a ubiquitous tool used in various system components, and two widely used algorithms at the fast end of the spectrum are LZ4 and Zstd. While both are open-source and well-maintained, they cater to different scenarios, and selecting the wrong one can lead to wasted resources, either in terms of CPU or bandwidth. LZ4 prioritizes speed by sacrificing a moderate compression ratio, while Zstd achieves a better ratio by investing more CPU and memory resources.

The choice between the two depends on which resource is scarce in a given system. LZ4 was designed to ensure that compression never lags behind a memory copy. Its decoder is highly efficient, capable of processing several gigabytes per second per CPU core. However, this efficiency comes at the cost of a less impressive compression ratio, typically achieving between 45% and 50% of the file size reduction compared to stronger codecs.

Zstd, also developed by Facebook, adopts a similar match-based compression approach but incorporates a more sophisticated search algorithm, larger windows, and an optional entropy-coding stage called FSE. This additional stage allows Zstd to provide a broader range of compression levels, from faster performance comparable to LZ4 to levels that rival older general-purpose codecs.

The decoder remains swift, typically processing a few gigabytes per second, but the encoder's speed varies significantly depending on the chosen level, sometimes being several times slower than LZ4. Importantly, Zstd can train a dictionary on sample data, making it particularly adept at handling compressing many small, similar records.

In summary, LZ4 and Zstd represent different trade-offs on the compression curve. LZ4 excels in scenarios where compression speed is critical, and CPU resources are limited, such as in-memory caches, live telemetry streams, and database page formats. Conversely, Zstd is more suitable for applications where minimizing the file size is paramount, and the encoder's computational cost is acceptable, like backups, object stores, package registries, and log archives.

The choice between these two algorithms ultimately hinges on the specific needs and constraints of the system in question. LZ4 is often favored in environments where consistent, fast compression is essential, such as within in-memory caches, live telemetry streams, and database page formats. This is because LZ4's speed is crucial in such scenarios where latency is a critical factor.

On the other hand, Zstd finds its niche in settings where achieving a smaller file size is more important, and the computational cost of compression can be justified. Examples include backups, object storage systems, package registries, and log archives, where data is written once and rarely accessed thereafter. The slower encoder of Zstd is considered a worthwhile trade-off for the benefits it provides in terms of reduced storage requirements.

However, both LZ4 and Zstd have their limitations. LZ4 may not be the optimal choice when the compressed data is frequently transferred or distributed, as the benefits of its stronger compression ratio may be diminished by the increased bandwidth costs incurred during each transfer. Conversely, Zstd may not be the best fit for compressing small, ephemeral values in hot paths, such as individual cache entries accessed on every request.

In these cases, the marginal gains in compression ratio offered by Zstd may be outweighed by the increased per-call overhead, which directly impacts response times. Additionally, Zstd's dictionary mode, while beneficial for small records, introduces operational complexity through the need for dictionary training and maintenance, which can be challenging for simpler LZ4 implementations.

Compatibility is another consideration. Both LZ4 and Zstd have well-defined specifications and extensive library support, making it relatively straightforward to integrate them into systems that utilize multiple compression formats. However, the variable compression levels offered by Zstd can lead to inconsistencies in the resulting byte streams when compared to LZ4.

This variability can pose challenges for teams that rely on predictable output sizes, particularly when upgrading to newer versions of the compression libraries. In conclusion, the selection between LZ4 and Zstd should be based on a careful analysis of the system's requirements, balancing factors such as compression speed, file size reduction, CPU utilization, and compatibility.

LZ4 is well-suited for scenarios demanding rapid compression with moderate compression ratios, while Zstd shines in situations where achieving a smaller file size is paramount, and the additional computation can be accommodated. By understanding the strengths and limitations of each algorithm, developers can make informed decisions that optimize their systems' performance and resource utilization.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Are you paying for backups of servers you deleted last year?

Snapshot storage keeps growing when nothing is ever told to expire, and deleting a backup feels riskier than paying for it A starting point for production 7 daily snapshots 4 weekly snapshots Monthly…

  • Unchecked snapshot storage can grow without expiration, causing unnecessary costs.
  • Manual snapshots outlive deleted volumes/disks, billing until manually removed.

I built a model that misbehaves on purpose, and let Kane CLI catch what chat apps get wrong

Your chat UI works because your model is polite. Real models send clean, well-formed streams most of the time, so most chat apps are only ever tested against good behaviour.

  • StreamProof model designed to misbehave on purpose for chat app testing
  • Kane CLI Hackathon by TestMu AI, observes user interactions and server interactions
  • StreamProof catches 14 deliberate bugs in open-source chat apps without false positives

WireGuard vs IPsec: Simplicity as a Security Feature

Virtual private networks are one of those technologies where the user sees a single toggle and has no idea what sits behind it. Under that toggle, two approaches dominate modern deployments.

  • WireGuard simplifies VPNs with a single toggle concealing technology
  • WireGuard fixes cryptographic choices, minimizing configuration decisions
  • IPsec offers flexibility with extensive configuration options for diverse requirements

Tokio vs async-std: Two Philosophies for Async Rust

Async Rust has spent years arguing with itself about what asynchronous code should feel like. Out of that argument came two runtimes that shaped the ecosystem: Tokio and async-std.

  • Tokio emphasizes throughput and performance optimization with explicit runtime configuration.
  • async-std integrates seamlessly with the standard library, offering a gentler learning curve.
  • Runtime choice impacts API design, debugging, and overall runtime behavior for Rust applications.

More from Wednesday 25 November →