Compressed Kafka Topics
Text messages may consume quite a lot of space in Kafka topics. However, text may usually be compressed with a high ratio, so sometimes it makes sense to enable topic compression. In this article, I’m going to show how easy it is to set up compression. I’m also going to check out the practical difference between compressed vs. uncompressed topics.
First, let’s generate a 1MB file with some repeating text, like “This is some test data”. It’s very easy using the Linux “yes” command:
> yes This is some test data | head -c 1MB > test.file
Now download Kafka and spin up Zookeeper and Kafka broker, just as described in the documentation, using default settings:
> tar -xzf kafka_2.12-2.5.0.tgz
> cd kafka_2.12-2.5.0
> bin/zookeeper-server-start.sh config/zookeeper.properties
> bin/kafka-server-start.sh config/server.properties
Next, create two topics — test-topic and test-topic-zip. The configuration is the same so far:
> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test-topic
> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test-topic-zip
Update the properties of the second topic to make it use compression. I set the compression.type parameter to gzip, however, snappy, lz4, and zstd values are also supported. You can experiment with them and find what works for you.
> bin/kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name test-topic-zip --alter --add-config compression.type=gzip
Finally, send the generated file to both queues:
> bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic < ~/test-1.file
> bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic-zip < ~/test-1.file
Let’s check the Kafka data files, which are located in /tmp/kafka-logs by default:
> ls -l /tmp/kafka-logs/test-topic-0/*.log
-rw-r--r-- 1 u g 1304114 Jul 5 21:49 00000000000000000000.log
The log file containing the message bodies is around 1,3MB in size, which means that our 1MB message got bloated even more. Now let’s check the compressed topic:
ls -l /tmp/kafka-logs/test-topic-zip-0/*.log
-rw-r--r-- 1 u g 98213 Jul 5 21:49 00000000000000000000.log
The log file with the message body is about 100KB in size, meaning the compression was successfully applied.