Performance & Tuning
The idea in one sentence
Section titled “The idea in one sentence”Tuning Kafka is mostly about choosing where you sit on the throughput-versus-latency curve — bigger producer batches, compression, more partitions, and stronger acks each push one way or the other, so you tune toward your workload rather than chasing a single “fast” setting.
Producer levers: batching, compression, acks
Section titled “Producer levers: batching, compression, acks”The producer is where most throughput is won or lost. Three settings dominate:
- Batching —
linger.msandbatch.sizelet the producer wait a few milliseconds and pack many records into one request. Larger batches mean far higher throughput at the cost of a little latency per record. - Compression —
compression.type(lz4,zstd,snappy,gzip) shrinks each batch, cutting network and disk usage. It trades CPU for throughput and is almost always worth it under load. - acks —
acks=allwaits for the ISR to persist the record (durable, higher latency);acks=1waits for only the leader (faster, weaker guarantee). This is the classic durability-versus-latency dial.
# Throughput-leaning producer: wait a little, batch bigger, compress, stay durablelinger.ms=10batch.size=65536compression.type=lz4acks=allflowchart LR batch["Bigger batches + linger.ms"] --> tp["Higher throughput"] comp["Compression (lz4 / zstd)"] --> tp acksall["acks=all"] --> dur["Stronger durability"] acksall --> lat["Higher latency"] small["Small batches + acks=1"] --> low["Lower latency"]
Sizing partitions to throughput
Section titled “Sizing partitions to throughput”Partitions are your parallelism unit: a partition is consumed by at most one consumer in a group, so the partition count is the ceiling on consumer parallelism. Size it from a target:
partitions ≈ target throughput ÷ per-consumer (or per-partition) throughput
If one consumer instance handles 10 MB/s and you need 100 MB/s, you need at least 10 partitions to let 10 consumers run in parallel. Round up and leave headroom for growth and for uneven key distribution.
Broker-side: page cache, disk, network
Section titled “Broker-side: page cache, disk, network”Brokers are deliberately simple, and their speed comes from the operating system:
- Page cache — Kafka does not maintain its own big in-process cache. It writes to the OS page cache and relies on the kernel to serve recent reads from memory. Leave plenty of free RAM for the page cache; do not give the JVM a huge heap that starves it.
- Disk — sequential writes make spinning disks and especially SSDs efficient. Use fast local disks, and spread load with multiple log directories if needed. Disk throughput and
fsyncbehavior bound your durability and speed. - Network — replication and consumer fan-out are network-bound. A
replication-factorof 3 means every produced byte is written roughly three times across the network, so provision NICs accordingly.