If i set 'compression.type' at topic level and producer level, which takes precedence
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Apache Kafka, a popular distributed streaming platform, message compression helps in reducing the amount of data being transmitted over the network, which can significantly improve performance especially in high-throughput scenarios. Kafka provides flexibility to set compression at two different levels: producer level and topic level. Understanding which setting takes precedence and how they interact can help in optimizing Kafka's performance and resource utilization.
Compression in Kafka
Kafka supports multiple compression codecs, including:
- None: No compression
- GZIP: Effective compression, uses more CPU resources
- Snappy: Fast compression and decompression with reasonable compression rates
- LZ4: Similar to Snappy, provides a slightly better compression rate at similar speeds
- ZSTD: Introduced in Kafka 2.1.0, offers excellent compression ratios with high speeds
Compression settings can be applied in Kafka at the producer level or at the topic level. The producer-level compression is configured on the client side, where the messages are produced before sending to the Kafka topic. In contrast, the topic-level compression is a configuration applied on the Kafka broker and affects how data is stored on disks.
Configuration Precedence
When both levels of compression are configured (producer and topic), the producer-level setting takes precedence. This is because message compression is applied at the producer before the messages are sent to the Kafka broker or topic. By the time the broker receives the message, it has already been compressed, regardless of the topic-level setting.
Example Scenario
Consider the following configurations:
- Producer compression is set to
GZIP. - Topic compression is set to
SNAPPY.
In this case, since the producer-level compression takes precedence, messages sent by this producer will be compressed using GZIP before they are published to the Kafka topic, irrespective of the topic-level setting specifying SNAPPY.
Technical Explanation
When a producer sends a message, it processes the data based on its configuration. This means that if compression is enabled at the producer level, the specific algorithm (e.g., GZIP, Snappy) immediately compresses the message batch before it is sent over the network to the Kafka Broker.
Upon receipt, the Kafka Broker writes the data into the log. If the topic-level configuration were to be respected independently, this would potentially lead to a scenario where the compression algorithm could be applied twice (once by the producer and once by the broker), which is not optimal and does not happen. Instead, the broker stores the data as received.
Why Producer-Level Precedence?
The rationale behind giving precedence to producer-level compression includes:
- Efficiency: Compressing at the producer minimizes the size of data transmitted over the network.
- Scalability: Allowing producers to choose their compression algorithm provides flexibility depending on their computational capabilities and specific requirements.
- Simplicity: It simplifies the broker’s work by avoiding double compression, which would not only be redundant but could also degrade performance.
Summary Table
| Setting Level | Compression Outcome | Priority | Configuration Example |
| Producer | Compressed according to producer setting | High | compression.type=GZIP at Producer |
| Topic | Ignored if producer compression is set | Low | compression.type=SNAPPY at Topic |
Best Practices and Additional Considerations
- Testing Different Compression Types: Given that different types of data compress differently, it's advisable to test various compression algorithms to determine which offers the best trade-off between compression ratio and computational demand for a particular application.
- Monitoring Impact: Always monitor the impact of compression on both producer and broker performance, especially CPU utilization, which can become a significant factor depending on the compression level.
- Version Compatibility: Be mindful of Kafka version compatibilities, especially with newer compression algorithms like
ZSTD, ensuring all brokers and clients support the compression types in use.
In conclusion, applying compression effectively in Kafka setups involves understanding the interactions between different configuration levels and the precedence rules. Producer-level compression significantly influences overall data handling efficiency in Kafka environments, supporting both high performance and scalability.

