Kafka: Compression is applied per batch by the producer

The broker stores the compressed batch as-is, so consumers pay the decompression cost, not the broker.

Code
props.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "zstd");  // or lz4, snappy, gzip
Output
JSON payloads typically compress 5-10x.
lz4:  fastest, moderate ratio
zstd: best ratio, slightly more CPU - usually the right default now
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11