0
0
Kafkadevops~10 mins

Compression (gzip, snappy, lz4) in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Compression (gzip, snappy, lz4)
Message Created
Choose Compression Codec
Compress Message
Send Compressed Message
Broker Receives Message
Broker Stores Compressed Message
Consumer Fetches Message
Decompress Message
Consumer Processes Original Message
Messages are compressed before sending using a chosen codec, stored compressed, then decompressed by consumers to get the original message.
Execution Sample
Kafka
Properties props = new Properties();
props.put("compression.type", "gzip");
Producer<String, String> producer = new KafkaProducer<>(props);
producer.send(new ProducerRecord<>("topic", "message"));
This code sets gzip compression for Kafka producer and sends a message compressed with gzip.
Process Table
StepActionCompression CodecMessage Size BeforeMessage Size AfterResult
1Create messagenone100 bytes100 bytesMessage ready to compress
2Choose codecgzip100 bytes100 bytesCompression codec set to gzip
3Compress messagegzip100 bytes40 bytesMessage compressed to smaller size
4Send messagegzip40 bytes40 bytesCompressed message sent to broker
5Broker stores messagegzip40 bytes40 bytesCompressed message stored
6Consumer fetches messagegzip40 bytes40 bytesCompressed message received
7Decompress messagegzip40 bytes100 bytesOriginal message restored
8Process messagenone100 bytes100 bytesConsumer processes original message
💡 Message fully sent, stored, fetched, decompressed, and processed.
Status Tracker
VariableStartAfter Step 3After Step 7Final
message_size100 bytes40 bytes100 bytes100 bytes
compression_codecnonegzipgzipnone (after decompression)
Key Moments - 3 Insights
Why does the message size decrease after compression but return to original size after decompression?
Because compression reduces the message size for sending and storage (see step 3), but decompression restores it back to original size for processing (see step 7).
What happens if the consumer tries to read the message without decompressing?
The consumer would see compressed data, which is unreadable. Decompression (step 7) is necessary to get the original message.
Can different compression codecs be used in Kafka?
Yes, Kafka supports gzip, snappy, lz4, etc. The codec is chosen before compression (step 2) and must be supported by both producer and consumer.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the message size after compression at step 3?
A40 bytes
B100 bytes
C60 bytes
D80 bytes
💡 Hint
Check the 'Message Size After' column at step 3 in the execution_table.
At which step does the consumer restore the original message size?
AStep 4
BStep 5
CStep 7
DStep 2
💡 Hint
Look for the step where 'Message Size After' returns to 100 bytes in the execution_table.
If the compression codec was changed to snappy, which step would be directly affected?
AStep 6 - Consumer fetches message
BStep 2 - Choose codec
CStep 8 - Process message
DStep 1 - Create message
💡 Hint
The codec choice happens at step 2 in the execution_table.
Concept Snapshot
Kafka compression flow:
- Set compression.type (gzip, snappy, lz4)
- Producer compresses message before sending
- Broker stores compressed message
- Consumer decompresses message after fetching
- Compression reduces network and storage size
- Decompression restores original message for processing
Full Transcript
In Kafka, messages can be compressed using codecs like gzip, snappy, or lz4. The producer sets the compression type before sending messages. When a message is created, it starts at its original size. The producer compresses it, reducing its size, then sends it to the broker. The broker stores the compressed message as is. When a consumer fetches the message, it receives the compressed data and must decompress it to restore the original message size before processing. This flow helps reduce network usage and storage space while ensuring consumers get the full original message. The execution table shows each step with message sizes and codec used. Key moments include understanding why size changes and the importance of decompression. The visual quiz tests understanding of these steps and codec choice.