Discuss the impact of Kafka's message compression on producer and consumer performance.
Instruction: Analyze how different compression codecs (e.g., GZIP, Snappy, LZ4) affect Kafka's throughput and latency.
Updated
Official answer
Kafka can compress producer record batches using gzip, snappy, lz4 or zstd, or send them uncompressed. Compression across a batch often finds more repeated structure than compression of isolated small values, so batching and payload shape affect the result.
Smaller batches on the wire can reduce producer-to-broker traffic, replicated bytes and stored log size. The producer spends CPU compressing, and consumers spend CPU decompressing. Brokers may also incur conversion work when the configured topic compression codec differs from the producer's codec; retaining the producer codec avoids that particular conversion.
There is no universal ordering of codecs by throughput, latency or achieved size. A codec/level that compresses repeated JSON well may provide little benefit for already compressed or encrypted values. More aggressive compression can reduce network pressure while increasing CPU time and tail latency. Consumers must have memory for decompressed data and processing copies, not just compressed fetch bytes.
Benchmark representative distributions at equal durability and latency targets. Record uncompressed input bytes, compressed output bytes, records/s, producer/broker/consumer CPU, batch fill, tail latency and memory. For example, reducing a measured 100 MiB batch to 25 MiB is a 4:1 size ratio and 75% byte reduction; it is not evidence of a 4x throughput improvement.
Choose the codec and level from those measurements, then retest under peak load and slower consumers. Avoid claiming a performance gain from a codec name alone.