Deep dive into Kafka's performance architecture and optimization techniques.
For complete log segment structure, indexes, and retention policies, see Storage Engine.
Kafka appends all writes to the end of log segments, achieving sequential disk access.
| Access Pattern | HDD Performance (illustrative) | SSD Performance (illustrative) |
|---|
| Random 4KB | ~100 IOPS | ~100K IOPS |
| Sequential | ~100 MB/s | ~500 MB/s |
| Kafka advantage | Order-of-magnitude on HDD | Smaller delta on SSD |
// Kafka uses FileChannel.transferTo()
// which maps to sendfile() system call
fileChannel.transferTo(position, count, socketChannel);
| Condition | Zero-Copy Available |
|---|
| Plaintext | Yes |
| TLS/SSL enabled | No (encryption requires user-space) |
| Compression | Yes (independent of compression) |
TLS Impact
Enabling TLS disables zero-copy; throughput impact depends on CPU and workload.
| Parameter | Default | Effect |
|---|
batch.size | 16384 | Maximum bytes per batch |
linger.ms | 0 | Time to wait for more records |
buffer.memory | 33554432 | Total memory for batching |
Without batching (1000 messages):
High overhead per message
With batching (1000 messages in 10 batches):
# Maximum data per request
| Algorithm | Indicative Ratio (varies by data) | CPU Usage | Speed |
|---|
| none | 1.0x | None | Fastest |
| gzip | ~5-8x | High | Slow |
| snappy | ~2-3x | Low | Fast |
| lz4 | ~3-4x | Low | Fastest |
| zstd | ~4-6x | Medium | Fast |
# Topic-level compression (broker will recompress if different)
compression.type=producer # Keep producer compression
compression.type=gzip # Force specific compression
| Use Case | Recommended |
|---|
| High throughput, low latency | lz4 |
| Balanced | zstd |
| Maximum compression | gzip |
| Minimal CPU | snappy |
| Already compressed data | none |
# Maximum in-flight requests
max.in.flight.requests.per.connection=5
| Setting | Ordering | Throughput |
|---|
max.in.flight=1 | Guaranteed | Lower |
max.in.flight=5 | May reorder on retry | Higher |
max.in.flight=5 + idempotent | Guaranteed | Higher |
| Parameter | Default | Recommendation |
|---|
num.network.threads | 3 | CPU cores / 4 |
num.io.threads | 8 | CPU cores |
num.replica.fetchers | 1 | 2-4 for high partition counts |
num.recovery.threads.per.data.dir | 1 | 2-4 for faster recovery |
JVM Heap: 6 GB (enough for metadata)
Page Cache: 56 GB (for log data)
If hourly throughput = 50 GB
Page cache covers ~1 hour of data
kafka-producer-perf-test.sh \
bootstrap.servers=kafka:9092 \
kafka-consumer-perf-test.sh \
--bootstrap-server kafka:9092 \
kafka-run-class.sh kafka.tools.EndToEndLatency \
| Metric | Example Range |
|---|
RequestsPerSec | Depends on workload |
TotalTimeMs (P99) | < 100 ms |
RequestQueueTimeMs | < 10 ms |
LocalTimeMs | < 50 ms |
RemoteTimeMs | < 50 ms |
ThrottleTimeMs | 0 |
| Symptom | Likely Bottleneck |
|---|
| High RequestQueueTimeMs | Network threads saturated |
| High LocalTimeMs | Disk I/O or I/O threads saturated |
| High RemoteTimeMs | Replication lag |
| High ResponseQueueTimeMs | Network threads saturated |