At-most-once delivery in Kafka - Time & Space Complexity
When using Kafka's at-most-once delivery, we want to know how the time to send messages changes as we send more messages.
How does the system handle more messages without slowing down too much?
Analyze the time complexity of the following Kafka producer code snippet.
producer.send(record);
producer.flush();
This code sends a message and immediately flushes it to ensure at-most-once delivery.
Look at what repeats when sending many messages.
- Primary operation: Sending and flushing each message one by one.
- How many times: Once per message sent.
As you send more messages, the time grows directly with the number of messages.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sends and flushes |
| 100 | 100 sends and flushes |
| 1000 | 1000 sends and flushes |
Pattern observation: Doubling messages doubles the work because each message is sent and flushed separately.
Time Complexity: O(n)
This means the time to send messages grows in a straight line with the number of messages.
[X] Wrong: "Flushing after every message is fast and does not affect performance."
[OK] Correct: Flushing each message forces waiting for confirmation every time, making the total time grow linearly and slowing down sending many messages.
Understanding how sending messages one by one affects time helps you explain trade-offs in message delivery guarantees clearly and confidently.
What if we batch multiple messages before flushing? How would the time complexity change?