how to compress data in producers when using spring kafka
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Producer-side compression in Spring Kafka is configured through normal Kafka producer properties. The most important setting is compression.type, which tells the producer to compress message batches before sending them to the broker, reducing network usage and often improving throughput for compressible payloads.
The Core Setting
Spring Kafka does not invent its own compression API here. It passes Kafka producer settings through to the underlying client, so you configure compression by setting ProducerConfig.COMPRESSION_TYPE_CONFIG.
Once configured, KafkaTemplate sends compressed batches automatically.
Supported Compression Types
Kafka commonly supports these compression types:
- '
gzip' - '
snappy' - '
lz4' - '
zstd' - '
none'
Each choice trades CPU cost against compression ratio and decompression speed. There is no universally best answer.
Why Compression Usually Works on Batches
Kafka producers compress record batches, not just isolated single records. That means compression becomes more effective when the producer can accumulate multiple records before sending them.
That is why batching-related settings such as linger.ms and batch.size often matter alongside compression.
If messages are sent completely one by one with tiny batches, the compression benefit may be smaller.
Spring Boot Property Style
If you are configuring Spring Kafka through application properties instead of Java config, the same producer option can be set there.
This is often the simplest setup in Spring Boot applications.
Choosing a Compression Type
A practical rule of thumb is:
- '
snappyfor a low-overhead, widely used default' - '
gzipwhen compression ratio matters more than CPU' - '
lz4orzstdwhen you want strong modern performance and should benchmark in your environment'
The only reliable answer for production is measurement. Payload shape, message size, CPU budget, and network constraints all matter.
What Compression Does Not Change
Compression does not remove the need to size your Kafka deployment correctly. It helps with transport and storage efficiency, but it does not fix:
- oversized messages
- poor partitioning
- slow serializers
- overloaded brokers
It is one tuning lever, not a cure-all.
Consumers Read Compressed Data Transparently
You do not usually need special consumer logic just because the producer compressed the records. Kafka handles decompression as part of normal broker and client processing.
Common Pitfalls
The biggest mistake is enabling compression and assuming every workload will improve automatically. If payloads are already small or poorly compressible, the CPU overhead may outweigh the savings.
Another issue is forgetting that compression benefits are tied to batching. Tiny batches reduce the value of the codec.
Developers also sometimes set the property on the wrong side. Producer compression belongs in producer configuration, not in consumer configuration.
Finally, do not choose a codec based only on blog posts. Benchmark with your own payload sizes, throughput targets, and infrastructure constraints.
Summary
- In Spring Kafka, producer compression is configured through Kafka producer properties.
- Set
ProducerConfig.COMPRESSION_TYPE_CONFIGor the equivalent Spring Boot property. - Compression is applied to record batches, so batching settings influence the payoff.
- Common codecs include
gzip,snappy,lz4, andzstd. - Benchmark the chosen codec in your real environment instead of assuming one option is always best.

