Sink connectors in Kafka - Time & Space Complexity
When using sink connectors in Kafka, it's important to understand how the processing time changes as the amount of data grows.
We want to know how the time to send data from Kafka to another system increases when more messages arrive.
Analyze the time complexity of the following Kafka sink connector code snippet.
public void put(Collection<SinkRecord> records) {
for (SinkRecord record : records) {
// Process and send each record to the target system
sendToTarget(record);
}
}
This code takes a batch of records and sends each one to the target system in order.
Look at what repeats in this code.
- Primary operation: Looping through each record in the batch.
- How many times: Once for every record in the input collection.
As the number of records increases, the time to process grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sends |
| 100 | 100 sends |
| 1000 | 1000 sends |
Pattern observation: Doubling the number of records roughly doubles the work done.
Time Complexity: O(n)
This means the time to process grows directly with the number of records.
[X] Wrong: "Processing a batch of records takes the same time no matter how many records are in it."
[OK] Correct: Each record needs to be handled, so more records mean more work and more time.
Understanding how sink connectors handle data helps you explain how systems scale with load, a key skill in real projects.
"What if the sendToTarget method batches multiple records internally? How would that affect the time complexity?"