0
0
KafkaHow-ToIntermediate · 4 min read

How to Tune Kafka Performance: Key Settings and Tips

To tune Kafka performance, adjust key broker settings like num.network.threads and log.segment.bytes, optimize producer configurations such as batch.size and linger.ms, and fine-tune consumer settings like fetch.min.bytes. Monitoring and balancing resource usage, partition count, and replication also help improve throughput and reduce latency.
📐

Syntax

Kafka performance tuning involves configuring settings in three main areas: broker, producer, and consumer. Each setting controls how Kafka handles data flow and resource use.

  • Broker settings: Control server threads, memory, and disk usage.
  • Producer settings: Manage batching, compression, and retries.
  • Consumer settings: Adjust fetch size and session timeouts.
properties
broker.properties example:
num.network.threads=3
num.io.threads=8
log.segment.bytes=1073741824

producer.properties example:
batch.size=32768
linger.ms=5
compression.type=snappy

consumer.properties example:
fetch.min.bytes=50000
max.poll.records=500
session.timeout.ms=10000
💻

Example

This example shows how to configure a Kafka producer for better throughput by increasing batch size and adding compression.

properties
bootstrap.servers=localhost:9092
key.serializer=org.apache.kafka.common.serialization.StringSerializer
value.serializer=org.apache.kafka.common.serialization.StringSerializer
batch.size=65536
linger.ms=10
compression.type=gzip
acks=all
retries=3
Output
Producer sends larger batches with compression, reducing network calls and improving throughput.
⚠️

Common Pitfalls

Common mistakes include setting batch sizes too large, causing latency spikes, or too small, causing inefficient network use. Not tuning linger.ms can lead to either high latency or low throughput. Overloading brokers with too many partitions or insufficient threads can cause slow processing.

Also, ignoring consumer fetch.min.bytes and max.poll.records can reduce consumer efficiency.

properties
Wrong producer config:
batch.size=16384
linger.ms=0

Right producer config:
batch.size=65536
linger.ms=10
📊

Quick Reference

SettingPurposeRecommended Value
num.network.threadsNumber of network threads on broker3-5
log.segment.bytesSize of log segment files1 GB
batch.sizeProducer batch size in bytes32 KB - 64 KB
linger.msProducer wait time to batch messages5-10 ms
compression.typeCompression codec for producersnappy or gzip
fetch.min.bytesMinimum bytes consumer fetches50 KB
max.poll.recordsMax records per consumer poll500

Key Takeaways

Tune broker thread and log segment sizes to optimize resource use.
Increase producer batch size and use compression to improve throughput.
Adjust consumer fetch size and poll records for efficient consumption.
Avoid too large batch sizes or too low linger.ms to balance latency and throughput.
Monitor Kafka metrics regularly to guide tuning decisions.