Quick revision
The short answer
Compression works per batch, so we first make batches bigger, for example linger.ms around 20 and batch.size of 64 to 256 KB, and then use zstd for the best ratio or lz4 for the lowest CPU and latency. Repetitive JSON often shrinks 4 to 10 times with zstd at good batch sizes, but we measure on our own data. Leaving the topic's compression.type at producer lets brokers store batches as sent; setting a different codec on the topic forces brokers to decompress and recompress every batch.
What to remember
In this answer, we saw that Kafka compresses whole batches, so we first raised linger.ms and batch.size to give the compressor enough data, then chose zstd for the best ratio on JSON or lz4 for the lowest CPU, with levels configurable in recent clients, and expected roughly 4 to 10 times on repetitive JSON while measuring on real data. Leaving the topic's compression.type at producer keeps brokers from decompressing and recompressing every batch, and producer, broker and consumer metrics confirm the saving.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Compression works per batch, so we first make batches bigger, for example linger.ms around 20 and batch.size of 64 to 256 KB, and then use zstd for the best ratio or lz4 for the lowest CPU and latency. Repetitive JSON often shrinks 4 to 10 times with zstd at good batch sizes, but we measure on our own data. Leaving the topic's compression.type at producer lets brokers store batches as sent; setting a different codec on the topic forces brokers to decompress and recompress every batch.
Why this question is asked
A service sending 50,000 one-kilobyte JSON events per second pushes about 50 MB/s, which becomes 150 MB/s of disk writes with three replicas and a lot of cross-zone traffic. Compression can cut all of that several times, but only if it is set up correctly, and the most common setups get the main lever wrong. Interviewers ask this to see whether we know where compression happens, how the codecs differ, and how to keep brokers from doing extra work. In this answer, we will recap how Kafka compresses, fix batching first, choose a codec and level, keep the broker out of the way, and finish with what to measure.
1A Quick Recap: Compression Is per Batch
The producer compresses a whole record batch at once, not each record. The broker stores the compressed batch, sends it to followers and consumers unchanged, and the consumer decompresses it. So one compression step saves network to the broker, cross-zone replication traffic, disk, page cache space and network to consumers.
Because the unit is the batch, the ratio depends on how much similar data is in each batch. JSON events repeat the same field names and many of the same values, so a batch of 200 events compresses far better than a batch of one, where the compressor has almost nothing to find repetitions in.
2Make Batches Bigger
At 50,000 events per second, the per-partition rate decides batch size. With, say, 48 partitions, each receives about 1,000 events per second, one per millisecond. With the 4.0 default linger.ms of 5, each batch closes after 5 ms holding about 5 events, around 5 KB, which is modest; with older configurations at linger.ms=0, batches can be one or two events and compression achieves little.
With linger.ms=20, each partition collects around 20 events per batch at this rate, and busier partitions fill batches up to the 128 KB limit. The latency cost is up to 20 ms per event, which most event pipelines accept. We raise buffer.memory too, since larger batches mean more bytes waiting in memory.
3Choose the Codec and Level
Kafka supports gzip, snappy, lz4 and zstd. They trade ratio against CPU in different ways.
| Codec | Ratio on JSON | Producer CPU | Notes |
|---|---|---|---|
| zstd | Best, often 4 to 10x | Moderate | Usually the best default for JSON |
| lz4 | Good | Lowest | Lowest latency, very fast to decompress |
| snappy | Good, close to lz4 | Low | Older choice, similar to lz4 |
| gzip | Good to very good | High | Slow; rarely the right choice now |
For this service, where the goals are network and disk savings, zstd is the natural choice. If producer CPU or latency is tight, lz4 gives a smaller but still large saving with less CPU. Recent clients also let us set the level per codec, for example compression.zstd.level, compression.lz4.level and compression.gzip.level (added in 3.8; check your version). Higher zstd levels squeeze a little more at a noticeable CPU cost, so the default level is a sensible start.
To pick between them, we take a few thousand real events, batch them at the planned size, and run each codec over them, or simply run kafka-producer-perf-test.sh with a payload file and compare compression-rate-avg and CPU. Ratios on synthetic data mean very little.
4Keep the Broker From Recompressing
Topics have their own compression.type setting. Its default is producer, which means 'store batches in whatever codec the producer used'. Then the broker writes the compressed batch as it arrived, and replication and fetches send the same bytes. If someone sets the topic to a specific codec that differs from the producer's, for example gzip on the topic while producers send zstd, the broker must decompress every incoming batch and recompress it with the topic's codec. That costs a lot of broker CPU and wastes the producer's effort.
So we leave compression.type=producer on the topic and choose the codec in the producer. The broker may still read through batches to validate them, but it avoids the expensive recompression step.
5What to Measure
On producers, compression-rate-avg (compressed size divided by original, so 0.2 means five times smaller), batch-size-avg and records-per-request-avg tell us whether batching and compression work, and process CPU tells us what it costs. On brokers, bytes in per second should fall by the compression ratio, as should disk growth and cross-zone transfer on the bill. On consumers, decompression adds CPU, usually small with lz4 and zstd, but worth checking on CPU-tight consumers.
6Follow-up questions
Does compression help if each event is already small and unique?
Much less. Compression needs repetition within a batch. Small, high-entropy payloads such as encrypted or already compressed data barely shrink, and the CPU is wasted. For those, we leave compression off or use lz4, which costs little when it does not help.
Would Avro or Protobuf instead of JSON help further?
Yes, often a lot. Binary formats with a schema do not repeat field names in every record, so the uncompressed size drops before compression even starts. Compression still helps on top, but the gain from moving off JSON can be as large as the gain from compression.
7Conclusion
In this answer, we saw that Kafka compresses whole batches, so we first raised linger.ms and batch.size to give the compressor enough data, then chose zstd for the best ratio on JSON or lz4 for the lowest CPU, with levels configurable in recent clients, and expected roughly 4 to 10 times on repetitive JSON while measuring on real data. Leaving the topic's compression.type at producer keeps brokers from decompressing and recompressing every batch, and producer, broker and consumer metrics confirm the saving.
Open with
The first thing I'd say is that Kafka compresses per batch, not per record. The producer compresses the whole batch, the broker stores it as is, replicates it and serves it, and the consumer decompresses.
Then, if they want more
So compression saves network in, cross-zone replication, disk, page cache and network out, all at once, and the ratio depends on how much similar data is in each batch. JSON has repeated field names, so it compresses well, but only in decent-sized batches.
So step one is batching: raise linger.ms to around 20 and batch.size to 64 to 256 KB, and buffer.memory with it. Step two is the codec. zstd usually gives the best ratio, lz4 the lowest CPU and latency, snappy is similar to lz4, and gzip is slow. For this service I would start with zstd.
Repetitive JSON often shrinks 4 to 10 times at good batch sizes, but I'd test on a sample of real events, and newer clients let you set the level per codec. Step three is the broker: leave the topic's compression.type at producer, the default, so batches are stored as sent.
If the topic names a different codec, the broker has to decompress and recompress every batch, which burns broker CPU. Then I'd watch compression-rate-avg and batch-size-avg on the producer, producer CPU, broker bytes in, and consumer CPU for decompression.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Which compression codec and settings would you choose, what ratio can you expect, and...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/bdae574ddf/