Auto-commit vs manual commit in Kafka - Performance Comparison
When working with Kafka consumers, committing offsets is key to tracking what messages have been processed.
We want to understand how auto-commit and manual commit affect the time it takes to process messages as the input grows.
Analyze the time complexity of these two commit styles in Kafka consumer code.
// Auto-commit example
props.put("enable.auto.commit", "true");
props.put("auto.commit.interval.ms", "1000");
// Manual commit example
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
process(record);
}
consumer.commitSync();
}
The first uses automatic commits at intervals, the second commits manually after processing each batch.
Look at what repeats as messages increase.
- Primary operation: Processing each message in the batch (loop over records).
- How many times: Once per message received in both cases.
- Commit operation: Auto-commit runs periodically in background; manual commit runs once per batch after processing.
Processing time grows with the number of messages because each message is handled once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Process 10 messages + few commits |
| 100 | Process 100 messages + few commits |
| 1000 | Process 1000 messages + few commits |
Pattern observation: Processing grows linearly with messages; commits happen less often and do not grow with message count.
Time Complexity: O(n)
This means the time to process messages grows directly with the number of messages, while commit overhead stays small.
[X] Wrong: "Manual commit makes processing slower because it commits every message."
[OK] Correct: Manual commit usually happens once per batch, not per message, so it does not slow down processing linearly.
Understanding how commit strategies affect processing time helps you explain trade-offs clearly in real projects.
What if we commit manually after every single message instead of after the batch? How would the time complexity change?