Performance Tuning

Throughput optimization, latency reduction, and capacity planning.

Intermediate 35 min read 📨 Kafka

Producer Tuning

Producer throughput depends on three main factors: batch size, compression, and acknowledgment mode. Here are the key settings:

SettingDefaultFor ThroughputFor Latency
batch.size16384 (16KB)65536+ (64KB)Keep default
linger.ms010-100ms0 (send immediately)
compression.typenonelz4 or zstdlz4 (fast)
acksall1 (if loss acceptable)1
buffer.memory32MB64MB+Keep default

Consumer Tuning

SettingDefaultFor ThroughputFor Latency
fetch.min.bytes110000+1 (return immediately)
fetch.max.wait.ms500500-1000100
max.poll.records5001000+100-500
max.partition.fetch.bytes1MB5MB1MB

Benchmarking

# Producer benchmark: send 1M messages of 1KB each
kafka-producer-perf-test.sh --topic perf-test \
  --num-records 1000000 --record-size 1024 \
  --throughput -1 --producer-props bootstrap.servers=localhost:9092 \
  acks=all compression.type=lz4 batch.size=65536 linger.ms=10
Output
1000000 records sent, 245098.0 records/sec (239.36 MB/sec), 12.3 ms avg latency, 89 ms max latency.
# Consumer benchmark
kafka-consumer-perf-test.sh --bootstrap-server localhost:9092 \
  --topic perf-test --messages 1000000 --threads 1
Key Takeaway: Always benchmark before and after tuning. Use kafka-producer-perf-test.sh to find your cluster's limits. The biggest wins usually come from compression (lz4) and batching (linger.ms=10).

Practice Exercises

Medium Build a Mini Project

Combine concepts from this tutorial to build a small utility or tool.

Medium Debug Challenge

Introduce a bug in one of the code examples and practice finding and fixing it.

Hard Refactoring Exercise

Rewrite one example using a different approach and compare the tradeoffs.