Python producer (confluent-kafka) - Time & Space Complexity
When sending messages with a Python Kafka producer, it's important to understand how the time to send grows as you send more messages.
We want to know how the work changes when the number of messages increases.
Analyze the time complexity of the following code snippet.
from confluent_kafka import Producer
p = Producer({'bootstrap.servers': 'localhost:9092'})
for i in range(n):
p.produce('my_topic', key=str(i), value=f'message {i}')
p.flush()
This code sends n messages to a Kafka topic using a loop and then waits for all messages to be delivered.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
producecall inside the loop that sends one message. - How many times: Exactly
ntimes, once per message.
As the number of messages n grows, the total work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 produce calls |
| 100 | 100 produce calls |
| 1000 | 1000 produce calls |
Pattern observation: Doubling the number of messages doubles the work.
Time Complexity: O(n)
This means the time to send messages grows linearly with the number of messages.
[X] Wrong: "Calling produce once sends all messages instantly."
[OK] Correct: Each produce call queues one message, so you must call it for every message, making the total time grow with the number of messages.
Understanding how message sending scales helps you design efficient data pipelines and shows you can reason about performance in real systems.
"What if we batch messages before calling produce? How would the time complexity change?"