Current state: Accepted
Discussion thread: here
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Basically, CPU (running time) and I/O (compressed size) are trade-offs in compression. Since the best is use case dependent, lots of compression libraries provide a way to control the compression level with a reasonable default level, which results in a good performance in general. However, Kafka does not provide a way to configure the compression level - it uses the default level only.
This proposal suggests adding the compression level option to the producer, broker, and topic config. Running tests with a real-world dataset (see below), I found that this option improves the producer's message/second rate up to 156%.
This feature introduces a new option, 'compression.level', to the producer, topic, and broker configuration. The type of this option is an integer, with a default value of null.
The table below shows the valid range of compression.level per compression.type. (note: snappy is excluded since it does not support any compression level.) The valid range and default value of the compression level are entirely up to the compression library, so they may be changed in the future.
|Compression Codec||availability||Valid Range||Default|
|gzip||Yes||1 (Deflater.BEST_SPEED) ~ 9 (Deflater.BEST_COMPRESSION)||6|
|lz4||Yes||1 ~ 17||9|
|zstd||Yes||-131072 ~ 22||3|
This option impacts the following processes:
- Producer compresses the user-given messages.
- Broker recompresses the user-given messages with specified compression.type per broker or topic.
- Broker recompresses the messages in the log cleanup process.
Compressing the records with the given compression type and level works like the following:
- If 'compression.type' is none of snappy, 'compression.level' is ignored.
- If 'compression.level' is not in the valid range, it raises an error.
- If 'compression.level' is in the valid range, the producer compresses the records with the given level.
- If 'compression.level' is not set, it falls back to the default level.
To benchmark how compression level affects the producer performance, I ran a small benchmark with a real-world dataset like below:
With the feature implemented on top of the latest trunk (commit ccec9b0), I ran kafka-producer-perf-test.sh on GraalVM Java 8 v21.1.0 with the following parameters:
- Number of records: 100,000
- batch.size: 1048576 (1mb)
- linger.ms: 100
A random sample of 4096 real-world records from this dataset, which consists of 129218 json files with an average size of 55.25kb.
MS Azure Kubernetes Cluster (Seoul Region), consists of 16 nodes of Standard_DS2_v2 (2vCPU, 7GB RAM, Expected network bandwidth of 1500 Mbps.)
Apache Kafka 2.7.0, GraalVM Java 8 (21.1.0), replicaton factor = 3.
|codec||level||produced message / sec||latency (ms)||size (bytes)||description|
It shows the following:
- Codec is the main factor that differentiates the compressed size. However, The compression level makes little impact on it. The maximum improvement is is gzip/1 vs. gzip/9 (8%), and the minimum is lz4/1 vs. lz/17 (1.5%).
- Excepting zstd/-5, when the compression level gets lower, messages/sec increase but latency decreases. Especially, compressing with zstd/1 produces 32.7% more messages per second than zstd/3 (current default), and gzip/1 produces 56.4% than gzip/6 (current default).
- For every compression codec, compression with minimum level (i.e., speed first strategy) resulted in the best messages/second rate.
Linear Write Test
To benchmark how compression level affects the linear write performance, I ran a small benchmark with a real-world dataset like below:
|codec||level||write speed (mb/sec)||description|
The result was almost similar. In general, the minimum compression level (=1) showed the best write speed (except zstd/-5).
Compatibility, Deprecation, and Migration Plan
Since this update follows the default compression level and current buffer size if they are not set, there is no backward compatibility problem.
Alongside the compression level, I am trying additional configuration options, like the following:
Compression buffer size option
At the initial stage, the compression buffer size option was also under consideration. However, during the benchmark, I could not find its positive impacts on produce speed or compressed size. I am still investigating whether it can improve the disk write speed, e.g., Broker-size recompression or compaction.
Long window size with Zstandard
With 1.3.2, Zstandard introduced compression/decompression with long window size. This option can improve the compression/decompression speed for some levels.