Kafka Performance
Performance tuning guide for Apache Kafka clusters.
Performance Dimensions
Section titled “Performance Dimensions”| Dimension | Description | Trade-offs |
|---|---|---|
| Throughput | Messages/bytes per second | May increase latency |
| Latency | End-to-end message delay | May reduce throughput |
| Durability | Data persistence guarantee | Impacts throughput |
| Availability | System uptime | Requires more resources |
Throughput Optimization
Section titled “Throughput Optimization”Producer Throughput
Section titled “Producer Throughput”# High throughput producer configurationbatch.size=131072 # 128KB batcheslinger.ms=20 # Wait for batchingbuffer.memory=67108864 # 64MB buffercompression.type=lz4 # Fast compressionacks=1 # Leader ack only (trade durability)max.in.flight.requests.per.connection=5Throughput Checklist:
- Enable compression (lz4 or zstd)
- Increase batch.size
- Set linger.ms > 0
- Use async sends
- Partition across multiple brokers
Consumer Throughput
Section titled “Consumer Throughput”# High throughput consumer configurationfetch.min.bytes=65536 # Fetch at least 64KBfetch.max.wait.ms=500 # Wait up to 500msfetch.max.bytes=52428800 # 50MB per fetchmax.partition.fetch.bytes=10485760 # 10MB per partitionmax.poll.records=1000 # Records per pollBroker Throughput
Section titled “Broker Throughput”# Broker configurationnum.network.threads=8 # Network I/O threadsnum.io.threads=16 # Disk I/O threadssocket.send.buffer.bytes=1048576 # 1MB send buffersocket.receive.buffer.bytes=1048576 # 1MB receive buffernum.replica.fetchers=4 # Replication threadsLatency Optimization
Section titled “Latency Optimization”Low Latency Producer
Section titled “Low Latency Producer”# Low latency producer configurationbatch.size=16384 # Small batcheslinger.ms=0 # No batching delaycompression.type=none # No compression overheadacks=1 # Leader ack onlymax.block.ms=1000 # Fast failureLow Latency Consumer
Section titled “Low Latency Consumer”# Low latency consumer configurationfetch.min.bytes=1 # Return immediatelyfetch.max.wait.ms=0 # No waitmax.poll.records=100 # Small batchesBroker Latency
Section titled “Broker Latency”# Broker configurationsocket.request.max.bytes=10485760 # Smaller max requestnum.network.threads=16 # More network threadsDurability Configuration
Section titled “Durability Configuration”Maximum Durability
Section titled “Maximum Durability”# Produceracks=allenable.idempotence=trueretries=2147483647max.in.flight.requests.per.connection=5
# Broker/Topicmin.insync.replicas=2unclean.leader.election.enable=falsedefault.replication.factor=3Balanced Durability
Section titled “Balanced Durability”# Produceracks=allenable.idempotence=true
# Topicmin.insync.replicas=2replication.factor=3Compression Tuning
Section titled “Compression Tuning”Compression Comparison
Section titled “Compression Comparison”| Algorithm | CPU Usage | Compression Ratio | Speed |
|---|---|---|---|
| none | 0% | 1.0x | Fastest |
| snappy | Low | 1.5-2x | Fast |
| lz4 | Low | 2-3x | Fast |
| gzip | High | 3-5x | Slow |
| zstd | Medium | 3-4x | Fast |
Compression Selection
Section titled “Compression Selection”# Best for most use casescompression.type=lz4
# Maximum compressioncompression.type=zstd
# CPU constrainedcompression.type=snappyPartition Tuning
Section titled “Partition Tuning”Partition Count
Section titled “Partition Count”Formula:
Partitions = max( throughput / per_partition_throughput, consumer_instances)Guidelines:
- ~10 MB/s per partition typical
- More partitions = more parallelism
- Too many partitions = overhead
Partition Distribution
Section titled “Partition Distribution”# Check leader distributionkafka-topics.sh --bootstrap-server kafka:9092 --describe | \ grep "Leader:" | awk '{print $4}' | sort | uniq -c
# Rebalance leaderskafka-leader-election.sh --bootstrap-server kafka:9092 \ --election-type preferred \ --all-topic-partitionsOS Tuning
Section titled “OS Tuning”Linux Kernel Parameters
Section titled “Linux Kernel Parameters”# Network buffersnet.core.rmem_max=16777216net.core.wmem_max=16777216net.core.rmem_default=16777216net.core.wmem_default=16777216net.ipv4.tcp_rmem=4096 87380 16777216net.ipv4.tcp_wmem=4096 87380 16777216
# File descriptorsfs.file-max=1000000
# Virtual memoryvm.swappiness=1vm.dirty_ratio=80vm.dirty_background_ratio=5
# Page cachevm.vfs_cache_pressure=50File Descriptor Limits
Section titled “File Descriptor Limits”kafka soft nofile 128000kafka hard nofile 128000kafka soft nproc 128000kafka hard nproc 128000Disk Configuration
Section titled “Disk Configuration”Filesystem selection significantly impacts Kafka throughput. XFS is recommended for all log directories.
| Filesystem | Recommendation | Notes |
|---|---|---|
| XFS | Recommended | Best sequential write performance, allocation group parallelism |
| ext4 | Acceptable | Suitable for smaller deployments |
| ZFS | Not recommended | CoW overhead, ARC competes with page cache |
# Mount options for Kafka data (XFS)/dev/nvme0n1 /kafka/data xfs noatime,nodiratime 0 2
# I/O scheduler (for NVMe/SSDs)echo none > /sys/block/nvme0n1/queue/scheduler→ Filesystem Selection Guide - Complete filesystem architecture comparison, page cache tuning, and configuration
JVM Tuning
Section titled “JVM Tuning”Heap Configuration
Section titled “Heap Configuration”# Recommended heap size: 6-8GBexport KAFKA_HEAP_OPTS="-Xms6g -Xmx6g"GC Configuration
Section titled “GC Configuration”export KAFKA_JVM_PERFORMANCE_OPTS="-server \ -XX:+UseG1GC \ -XX:MaxGCPauseMillis=20 \ -XX:InitiatingHeapOccupancyPercent=35 \ -XX:G1HeapRegionSize=16M \ -XX:MinMetaspaceFreeRatio=50 \ -XX:MaxMetaspaceFreeRatio=80"Benchmarking
Section titled “Benchmarking”Producer Benchmark
Section titled “Producer Benchmark”# Throughput testkafka-producer-perf-test.sh \ --topic test-topic \ --num-records 10000000 \ --record-size 1024 \ --throughput -1 \ --producer-props \ bootstrap.servers=kafka:9092 \ batch.size=65536 \ linger.ms=10 \ compression.type=lz4Consumer Benchmark
Section titled “Consumer Benchmark”# Throughput testkafka-consumer-perf-test.sh \ --bootstrap-server kafka:9092 \ --topic test-topic \ --messages 10000000 \ --threads 4End-to-End Latency
Section titled “End-to-End Latency”kafka-run-class.sh kafka.tools.EndToEndLatency \ kafka:9092 \ test-topic \ 10000 \ all \ 1024Performance Metrics
Section titled “Performance Metrics”Key Metrics
Section titled “Key Metrics”| Metric | Description | Target |
|---|---|---|
MessagesInPerSec | Produce rate | Workload dependent |
BytesInPerSec | Bytes produced | Workload dependent |
BytesOutPerSec | Bytes consumed | Workload dependent |
TotalTimeMs (P99) | Request latency | < 100ms |
RequestQueueTimeMs | Queue time | < 10ms |
UnderReplicatedPartitions | Replication health | 0 |
Identifying Bottlenecks
Section titled “Identifying Bottlenecks”| Symptom | Likely Cause | Solution |
|---|---|---|
| High RequestQueueTimeMs | Network threads saturated | Increase num.network.threads |
| High LocalTimeMs | Disk I/O slow | Faster disks, more I/O threads |
| High RemoteTimeMs | Replication lag | More replica fetchers |
| High ResponseQueueTimeMs | Network threads saturated | Increase num.network.threads |
Related Documentation
Section titled “Related Documentation”- Filesystem Selection - Filesystem recommendations and page cache tuning
- Operations Overview - Operations guide
- Capacity Planning - Sizing guide
- Configuration - Configuration reference
- Monitoring - Metrics and alerting