0
0
Kafkadevops~7 mins

Producer throughput optimization in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sending messages quickly to Kafka can be tricky. If your producer sends messages one by one, it slows down. Optimizing throughput means sending many messages efficiently without losing data.
When your app needs to send thousands of messages per second to Kafka.
When you notice your producer is slow and can't keep up with incoming data.
When you want to reduce network overhead by sending messages in batches.
When you want to balance speed and reliability in message delivery.
When you want to tune Kafka producer settings to handle high traffic smoothly.
Config File - producer.properties
producer.properties
bootstrap.servers=localhost:9092
acks=1
batch.size=32768
linger.ms=10
compression.type=snappy
buffer.memory=67108864
max.in.flight.requests.per.connection=5
retries=3

bootstrap.servers: Kafka server address to connect.
acks=1: Wait for leader to acknowledge for faster response.
batch.size: Size in bytes to batch messages before sending.
linger.ms: Time to wait to batch more messages before sending.
compression.type: Compress messages to reduce size.
buffer.memory: Memory for buffering messages.
max.in.flight.requests.per.connection: Number of unacknowledged requests allowed.
retries: Number of retry attempts on failure.

Commands
Starts the Kafka producer using the optimized settings from producer.properties to send messages to 'example-topic'.
Terminal
kafka-console-producer --broker-list localhost:9092 --topic example-topic --producer.config producer.properties
Expected OutputExpected
> This is a test message > Another message
--broker-list - Specifies Kafka server addresses to connect.
--topic - Specifies the topic to send messages to.
--producer.config - Loads producer configuration from a file.
Runs a performance test to send 100,000 messages of 100 bytes each with optimized producer settings to measure throughput.
Terminal
kafka-run-class kafka.tools.ProducerPerformance --topic example-topic --num-records 100000 --record-size 100 --throughput -1 --producer-props bootstrap.servers=localhost:9092 acks=1 batch.size=32768 linger.ms=10 compression.type=snappy
Expected OutputExpected
100000 records sent, 100000 records acknowledged, 0 errors Throughput: 50000 records/sec Average latency: 5 ms
--num-records - Number of messages to send.
--record-size - Size of each message in bytes.
--throughput - Messages per second to send; -1 means unlimited.
Checks the consumer group lag to ensure messages are being consumed timely after high throughput production.
Terminal
kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group example-group
Expected OutputExpected
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID example-topic 0 100000 100000 0 consumer-1 /127.0.0.1 consumer-1
--bootstrap-server - Kafka server address.
--describe - Shows detailed info about the consumer group.
--group - Specifies the consumer group to check.
Key Concept

Batching messages and tuning producer settings like linger.ms and batch.size greatly improve Kafka producer throughput.

Common Mistakes
Setting linger.ms to 0 to send messages immediately.
This disables batching, causing many small requests and lowering throughput.
Set linger.ms to a small positive value like 10 ms to allow batching of messages.
Using a very small batch.size like 1 KB.
Small batches increase network overhead and reduce throughput.
Use a larger batch.size like 32 KB or more to send bigger batches efficiently.
Not enabling compression on messages.
Uncompressed messages use more bandwidth and slow down throughput.
Enable compression.type like 'snappy' to reduce message size and improve speed.
Summary
Configure producer.properties with batch.size, linger.ms, and compression.type for better throughput.
Use kafka-console-producer with the config file to send messages efficiently.
Run kafka-run-class ProducerPerformance to test and measure producer throughput.
Check consumer lag with kafka-consumer-groups to ensure consumers keep up.