In this lesson, you will learn how to select the right compression codec by balancing your need for small file sizes against the CPU cost of decompressing that data.
The Compression Trade-off
Once we have organized our data into efficient columnar layouts (like Parquet or ORC), we apply a compression codec to the files to reduce their footprint on disk. Every codec exists on a spectrum between storage density (how small the file is) and decompression performance (how fast the CPU can read it).
Snappy vs. Gzip vs. Zstd
In modern data pipelines, you will primarily encounter these three:
- Snappy: Built for speed. It offers a modest compression ratio but consumes very little CPU. It is the gold standard for "hot" data where queries need to run in milliseconds and we want to avoid CPU bottlenecks.
- Gzip: A classic, balanced approach. It provides a higher compression ratio than Snappy, which saves storage costs, but requires more CPU cycles to decompress. It is often a good choice for cold storage or files where network I/O is the primary constraint.
- Zstd (Zstandard): The modern contender. It is highly configurable, allowing you to choose between high-speed decompression (similar to Snappy) and high-density compression (approaching Gzip or better). It is becoming the default choice for many because it offers the best of both worlds.
How to Think About Selection
When configuring your storage, ask yourself two questions:
- Is the data "Hot"? If the data is queried constantly, use
Snappyto minimize latency. - Is the data "Cold"? If you are archiving data to S3 or HDFS for infrequent reporting, prioritize space savings by using
Zstd(with higher compression levels) orGzip.
-- Conceptual configuration in a Spark job
-- Use Snappy for performance
df.write.option("compression", "snappy").parquet("s3://data/hot/")
-- Use Zstd for better storage density
df.write.option("compression", "zstd").parquet("s3://data/cold/")
If you choose a codec that is too computationally expensive for a high-traffic dataset, your analytical queries will stall, not because of disk I/O, but because your CPUs are working too hard to inflate the data.
Key Takeaway: Choose Snappy for high-performance query latency, and Zstd or Gzip for long-term storage efficiency when data density is the priority.