Black Friday: 30% off every plan through November 30. See the prices
SysDesignPrep.com
Study guide

Updated 24 October 2026

Compression in system design

Where compression saves money and latency: HTTP compression with gzip, Brotli and Zstandard, compression in Kafka and databases, columnar and time-series encodings, images and video codecs, ratio versus speed trade-offs, dictionary compression, and when compression hurts.

Reading is half of it. Now try it in an interview.An AI interviewer asks you to design a Metrics and Monitoring System, out loud or typed, and scores the answer for your level. Or watch a sample interview first, free with no sign-up to start.
Start an AI mock

Try it yourself, in a playground:Row vs column storageCompression trade-offs

Compression trades CPU for fewer bytes. At scale, fewer bytes means lower bandwidth bills, less storage, faster transfers and more data in memory. Logs often shrink ten times, metrics even more, and the right video codec halves delivery costs. Mentioning compression at the right place in a design (and its costs) is an easy way to show practical experience, especially in storage-heavy and bandwidth-heavy questions.

The basic trade-off

Every algorithm sits somewhere on a curve of ratio (how small) versus speed (how fast to compress and decompress):

AlgorithmRatioSpeedTypical use
LZ4, Snappymodestvery fastdatabases, Kafka, in-memory and RPC payloads
gzip (DEFLATE)goodmoderateHTTP, files, universal compatibility
Zstandard (zstd)good to very good, tunablefastKafka, storage, logs, general purpose default
Brotlivery good for textslow at high levels, fast to decompressstatic web assets, HTTP
xz, LZMAvery highslowarchives, cold storage

Decompression is usually much faster than compression, so content compressed once and read many times (static assets, archived data) can use slower, stronger settings.

On the web

  • Compress text responses (HTML, CSS, JavaScript, JSON) with Brotli or gzip based on Accept-Encoding; typical savings are 70 to 90 % for text.
  • Precompress static assets at build time at maximum level; compress dynamic responses at a fast level.
  • Do not compress already-compressed formats (JPEG, PNG, MP4, zip): it wastes CPU for nothing.
  • Use Vary: Accept-Encoding so caches store the right variants. See HTTP caching.

Messaging and RPC

  • Kafka compresses record batches on the producer (zstd, LZ4, Snappy or gzip); brokers store and serve them compressed, saving disk and network across the cluster. Larger batches compress better. See how Kafka works.
  • gRPC supports per-message compression; binary formats like Protobuf are already compact, so gains are smaller than with JSON. See schema evolution and serialization.

Databases and storage

  • Storage engines compress pages or blocks (LZ4, zstd), trading a little CPU for more data in cache and fewer disk reads. See storage engines.
  • Columnar formats compress far better than row formats, because a column's values are similar: run-length encoding, dictionary encoding (store each distinct string once), bit packing and delta encoding before general-purpose compression. See data lakes and lakehouses.
  • Time-series databases use specialised encodings: delta-of-delta for timestamps and XOR encoding for floating-point values (from Facebook's Gorilla), often reaching around 1.4 bytes per data point instead of 16. See time-series data and Design a Monitoring System.

Logs

Logs are highly repetitive text and commonly compress 5 to 20 times. Compress on the agent before shipping, store compressed in object storage, and use structured fields so repeated keys compress well. See distributed tracing and structured logging.

Dictionary compression

Small messages (a few hundred bytes) compress poorly on their own, because there is little repetition within one message. Dictionary compression (zstd supports trained dictionaries) shares common patterns across messages, greatly improving ratios for small JSON payloads, cache entries or database rows.

Images and video

Media dominates bandwidth for most consumer products, and its compression is format-specific and lossy:

  • Images: WebP and AVIF are much smaller than JPEG at similar quality; resize to the display size first. See media uploads and processing.
  • Video: H.264 is universal; HEVC, VP9 and AV1 save roughly 30 to 50 % bandwidth at similar quality, at higher encoding cost. Platforms encode popular videos with expensive codecs and settings because the savings multiply over millions of views. See video streaming and Design YouTube.

Deduplication versus compression

Compression removes redundancy within a stream; deduplication removes identical chunks across files and users (content addressing). Large storage systems use both. See hashing and encoding.

When compression hurts

  • CPU-bound services where bandwidth is cheap (inside a data centre with small payloads).
  • Latency-critical tiny messages.
  • Already-compressed or encrypted data (encryption makes data look random; compress before encrypting, and beware of compression side-channel attacks on secrets mixed with attacker-controlled data in HTTPS).

In the interview

Mention compression where bytes dominate: "logs and events are compressed with zstd in Kafka and stored as compressed Parquet, roughly 10x smaller"; "metrics use delta-of-delta and XOR encoding"; "video is encoded with a modern codec for popular titles to cut CDN egress". For a web crawler, compress stored pages; for a message queue, compress batches at the producer.

Checklist

  • Pick algorithms by ratio versus speed; zstd as a strong default.
  • Brotli or gzip for text over HTTP; skip already-compressed media.
  • Producer-side batch compression in Kafka.
  • Columnar and time-series encodings for analytical and metric data.
  • Dictionary compression for small messages.
  • Modern image and video codecs where bandwidth costs dominate.
  • Know when CPU, latency or security make compression a bad idea.

Questions people ask

What is compression in system design?

Compression trades CPU for fewer bytes, and at scale fewer bytes means lower bandwidth bills, less storage, faster transfers and more data in memory. Every algorithm sits on a curve of ratio (how small) versus speed (how fast to compress and decompress). Logs often shrink ten times, metrics even more, and the right video codec halves delivery costs, so compression matters most in storage-heavy and bandwidth-heavy designs.

What is the difference between gzip, Brotli and Zstandard?

Zstandard (zstd) is fast with a good to very good, tunable ratio, and is a strong general-purpose default for Kafka, storage and logs. gzip (DEFLATE) gives a good ratio at moderate speed and is valued for universal compatibility in HTTP and files. Brotli compresses text very well and decompresses fast but is slow at high levels, so it suits static web assets that are compressed once and read many times.

Should you compress HTTP responses?

Yes for text: compress HTML, CSS, JavaScript and JSON with Brotli or gzip based on Accept-Encoding, which typically saves 70 to 90 % of the bytes. Precompress static assets at build time at maximum level and compress dynamic responses at a fast level. Do not compress already-compressed formats such as JPEG, PNG, MP4 or zip, since it wastes CPU for nothing, and send Vary: Accept-Encoding so caches store the right variants.

Why do columnar formats compress better than row formats?

Columnar formats compress far better because the values in one column are similar. That lets them apply run-length encoding, dictionary encoding (storing each distinct string once), bit packing and delta encoding before general-purpose compression. Time-series databases go further with delta-of-delta encoding for timestamps and XOR encoding for floating-point values, often reaching around 1.4 bytes per data point instead of 16.

What is the difference between compression and deduplication?

Compression removes redundancy within a single stream, while deduplication removes identical chunks across files and users through content addressing. In other words, compression makes each piece of data smaller on its own, and deduplication stores a repeated chunk only once no matter how many files or users contain it. Large storage systems use both.

When does compression hurt performance?

Compression hurts in CPU-bound services where bandwidth is cheap, such as small payloads inside a data centre, and for latency-critical tiny messages. It is wasted on already-compressed or encrypted data, because encryption makes data look random, so compress before encrypting. With HTTPS, beware of compression side-channel attacks when secrets are mixed with attacker-controlled data.