MirrorMaker 2 concept in Kafka - Time & Space Complexity
When using MirrorMaker 2, it is important to understand how the time it takes to replicate messages grows as the amount of data increases.
We want to know how the work done changes when more messages need to be copied between Kafka clusters.
Analyze the time complexity of the following MirrorMaker 2 replication loop.
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
producer.send(new ProducerRecord<String, String>(targetTopic, record.key(), record.value()));
}
producer.flush();
}
This code continuously reads messages from a source Kafka cluster and sends them to a target cluster, replicating data.
Look at what repeats as the program runs.
- Primary operation: Looping over all messages received in each poll.
- How many times: Once for every message batch fetched, and inside that, once per message.
As the number of messages to replicate grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 send operations |
| 100 | About 100 send operations |
| 1000 | About 1000 send operations |
Pattern observation: The number of send operations grows directly with the number of messages.
Time Complexity: O(n)
This means the time to replicate messages grows in a straight line with the number of messages.
[X] Wrong: "The replication time stays the same no matter how many messages there are."
[OK] Correct: Each message must be sent individually, so more messages mean more work and more time.
Understanding how MirrorMaker 2 scales helps you explain real-world data replication challenges clearly and confidently.
"What if MirrorMaker 2 batches messages before sending? How would that affect the time complexity?"