Batching and linger configuration in Kafka - Time & Space Complexity
When sending messages in Kafka, batching groups many messages together before sending.
We want to understand how batching and linger settings affect the time it takes to send messages as the number of messages grows.
Analyze the time complexity of this Kafka producer configuration snippet.
props.put("batch.size", 16384); // batch size in bytes
props.put("linger.ms", 10); // wait time before sending batch
// Sending n messages in a loop
for (int i = 0; i < n; i++) {
producer.send(new ProducerRecord(topic, "message-" + i));
}
producer.flush();
This code sends n messages, using batching and linger to group messages before sending.
Look at what repeats as messages are sent.
- Primary operation: Sending messages inside a loop.
- How many times: n times, once per message.
- Batching effect: Messages are grouped, so actual network sends happen fewer times.
As n grows, the number of send calls grows linearly, but actual network sends grow slower due to batching.
| Input Size (n) | Approx. Network Sends |
|---|---|
| 10 | 1 or 2 batches |
| 100 | Few batches, less than 100 sends |
| 1000 | More batches, but much less than 1000 sends |
Pattern observation: Network sends grow slower than messages because batching groups them.
Time Complexity: O(n)
This means the total work grows linearly with the number of messages, but batching reduces the number of network operations.
[X] Wrong: "Batching makes sending messages constant time regardless of n."
[OK] Correct: Batching reduces network calls but you still process each message once, so work grows with n.
Understanding batching and linger helps you explain how Kafka handles many messages efficiently, a useful skill for real-world systems.
What if we set linger.ms to 0? How would the time complexity and network sends change?