📖 Scenario: You are working with Apache Kafka to send messages from a producer to a topic. To improve how fast your producer sends messages, you want to optimize the throughput by adjusting some settings.
🎯 Goal: Build a Kafka producer configuration that optimizes throughput by setting batch size and linger time, then send a batch of messages and print a confirmation.
📋 What You'll Learn
Create a Kafka producer configuration dictionary with specific settings
Add a batch size setting to control message batching
Add a linger.ms setting to delay sending to increase batch size
Send multiple messages using the producer
Print a confirmation message after sending
💡 Why This Matters
🌍 Real World
Optimizing Kafka producer throughput helps in real-time data pipelines where sending messages quickly and efficiently is important.
💼 Career
Kafka producer configuration and tuning is a common task for data engineers and backend developers working with streaming data.
Progress0 / 4 steps
1
Create Kafka producer configuration
Create a dictionary called producer_config with these exact entries: bootstrap.servers set to "localhost:9092" and acks set to "all".
Kafka
Hint
Use a Python dictionary with keys as strings and values as strings.
2
Add batch size setting
Add a new entry to producer_config with key batch.size and value 16384 (an integer).
Kafka
Hint
Remember to add a comma after the previous entry.
3
Add linger.ms setting
Add a new entry to producer_config with key linger.ms and value 5 (an integer).
Kafka
Hint
Adding linger.ms helps the producer wait a bit to batch more messages.
4
Send messages and print confirmation
Using the producer_config, create a Kafka producer object called producer. Then send three messages with keys "key1", "key2", "key3" and values "message1", "message2", "message3" to the topic "test-topic". Finally, print "Messages sent successfully".
Kafka
Hint
Use Producer from confluent_kafka, call produce for each message, then flush() to send all.
Practice
(1/5)
1. What is the main benefit of increasing the batch.size setting in a Kafka producer?
easy
A. It guarantees message delivery to all replicas.
B. It compresses messages to save disk space.
C. It allows sending more messages together, improving throughput.
D. It reduces the number of partitions in a topic.
Solution
Step 1: Understand batch.size role
The batch.size controls how many messages the producer groups before sending.
Step 2: Effect on throughput
Grouping more messages reduces network calls, increasing throughput.
Final Answer:
It allows sending more messages together, improving throughput. -> Option C
Quick Check:
batch.size increases throughput [OK]
Hint: More batch size means fewer sends, better throughput [OK]
Common Mistakes:
Confusing batch.size with message compression
Thinking batch.size controls message durability
Assuming batch.size changes topic partitions
2. Which of the following is the correct way to set the linger time to 100 milliseconds in Kafka producer configuration?
easy
A. "linger.ms = 100"
B. "linger_time = 100"
C. "lingerMs: 100"
D. "linger = 100ms"
Solution
Step 1: Identify correct property name
The Kafka producer uses linger.ms to set linger time in milliseconds.
Step 2: Confirm correct syntax
The correct syntax is linger.ms = 100, using dot notation and milliseconds.
Final Answer:
"linger.ms = 100" -> Option A
Quick Check:
linger.ms sets linger time [OK]
Hint: Use exact property names with dots, like linger.ms [OK]