Which compression codecs does Kafka support?
Solution and explanation
Supported codecs
Set with compression.type on the producer (and optionally on the topic or broker):
| Codec | Compression ratio | CPU cost | Notes |
|---|---|---|---|
none | 1× | none | default on the producer |
gzip | high | high | good for low-volume, bandwidth-limited links |
snappy | moderate | low | fast, from Google |
lz4 | moderate | very low | usually the best default for throughput |
zstd | high (close to gzip) | moderate | Kafka 2.1+; excellent balance, levels configurable since 3.8 |
How it works
The producer compresses a **whole batch**. The broker stores it compressed (no recompression if the topic's compression.type=producer, the default), and the consumer decompresses. Compression therefore saves network bandwidth, broker disk and page cache, and replication traffic.
JSON event payloads with repeated field names often compress 3–10×.
compression.type=zstd # producer
linger.ms=10 # bigger batches compress better
batch.size=131072
Topic-level compression
If a topic sets compression.type=gzip and producers send lz4, the broker must **recompress**, costing CPU. Keep topics at producer unless you need to enforce a codec.
Choosing
- General purpose, high throughput: **lz4** or **zstd**.
- Storage cost matters most (long retention): **zstd**.
- Very CPU-constrained producers: **snappy** or **lz4**.
- Already compressed payloads (images, encrypted data): **none**.
Summary
Kafka supports gzip, snappy, lz4 and zstd, applied per batch by the producer; lz4 and zstd are the usual choices, and larger batches improve the ratio.
