0
0
Kafkadevops~5 mins

MirrorMaker 2 concept in Kafka - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: MirrorMaker 2 concept
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of messages to replicate grows, the work grows too.

Input Size (n)Approx. Operations
10About 10 send operations
100About 100 send operations
1000About 1000 send operations

Pattern observation: The number of send operations grows directly with the number of messages.

Final Time Complexity

Time Complexity: O(n)

This means the time to replicate messages grows in a straight line with the number of messages.

Common Mistake

[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.

Interview Connect

Understanding how MirrorMaker 2 scales helps you explain real-world data replication challenges clearly and confidently.

Self-Check

"What if MirrorMaker 2 batches messages before sending? How would that affect the time complexity?"