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?"
Practice
Solution
Step 1: Understand MirrorMaker 2 role
MirrorMaker 2 is designed to replicate data from one Kafka cluster to another to ensure data safety and availability.Step 2: Compare options with MirrorMaker 2 function
Only To copy data between Kafka clusters for backup and availability describes copying data between clusters, which matches MirrorMaker 2's purpose.Final Answer:
To copy data between Kafka clusters for backup and availability -> Option AQuick Check:
MirrorMaker 2 purpose = Data replication [OK]
- Confusing MirrorMaker 2 with monitoring tools
- Thinking it creates topics automatically
- Assuming it compresses messages
Solution
Step 1: Identify source cluster connection
MirrorMaker 2 uses consumer configs to read from the source Kafka cluster.Step 2: Match config key to source cluster
The correct config key for source cluster is consumer.bootstrap.servers, which tells MirrorMaker 2 where to consume data from.Final Answer:
consumer.bootstrap.servers -> Option DQuick Check:
Source cluster = consumer.bootstrap.servers [OK]
- Using producer.bootstrap.servers for source cluster
- Confusing config keys with mirror.bootstrap.servers
- Assuming replica.bootstrap.servers is valid
{
"source.cluster.bootstrap.servers": "source:9092",
"target.cluster.bootstrap.servers": "target:9092",
"topics": "topicA,topicB",
"num.streams": 3
}How many parallel streams will MirrorMaker 2 use to copy data?
Solution
Step 1: Locate num.streams setting
The config sets "num.streams" to 3, which controls how many parallel streams MirrorMaker 2 uses.Step 2: Understand effect of num.streams
More streams mean more parallelism in copying data, so MirrorMaker 2 will use 3 streams here.Final Answer:
3 -> Option AQuick Check:
num.streams = 3 [OK]
- Assuming default 1 stream if not read carefully
- Confusing topics count with streams count
- Ignoring num.streams config
topics = "topic1,topic2" num.streams = 0
What is the problem with this configuration?
Solution
Step 1: Check num.streams value validity
num.streams controls parallel streams and must be at least 1 to work properly.Step 2: Identify error in config
Setting num.streams to 0 disables streams, causing MirrorMaker 2 to fail or not copy data.Final Answer:
num.streams cannot be zero; it must be at least 1 -> Option BQuick Check:
num.streams ≥ 1 required [OK]
- Thinking topics must be single only
- Assuming num.streams can be zero
- Believing num.streams must be even
Solution
Step 1: Understand topic filtering in MirrorMaker 2
MirrorMaker 2 supports filtering topics by regex using the "topics.regex" config.Step 2: Match regex to desired pattern
To match all topics starting with "logs-", the regex "logs-.*" correctly matches any topic beginning with "logs-".Step 3: Compare options
"topics": "logs-*" uses "topics" with wildcard which is not supported; "topics": "logs-" matches exact topic name; "topics.regex": "^logs$" matches only "logs" exactly.Final Answer:
"topics.regex": "logs-.*" -> Option CQuick Check:
Use topics.regex with proper regex for filtering [OK]
- Using wildcard * in topics instead of regex
- Confusing exact match with regex match
- Using incorrect regex that matches wrong topics
Ready to prove it?
Pass the test and this pattern moves unknown
