Amazon MSK in Kafka - Time & Space Complexity
When working with Amazon MSK, it's important to understand how the time to process messages grows as the number of messages increases.
We want to know how the system handles more data and how that affects processing time.
Analyze the time complexity of the following Kafka consumer code using Amazon MSK.
consumer.subscribe(Collections.singletonList("my-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
process(record.value());
}
}
This code subscribes to a topic and continuously polls for new messages, processing each one as it arrives.
Look at what repeats in this code:
- Primary operation: Looping over all messages received in each poll.
- How many times: The outer loop runs indefinitely, and the inner loop runs once for each message in the batch.
As the number of messages increases, the time to process them grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Processes 10 messages |
| 100 | Processes 100 messages |
| 1000 | Processes 1000 messages |
Pattern observation: If you double the number of messages, the processing time roughly doubles too.
Time Complexity: O(n)
This means the time to process messages grows linearly with the number of messages received.
[X] Wrong: "Processing time stays the same no matter how many messages arrive."
[OK] Correct: Each message needs to be handled, so more messages mean more work and more time.
Understanding how message processing scales helps you design systems that handle growth smoothly and shows you can think about real-world performance.
"What if we batch process messages instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand Amazon MSK's purpose
Amazon MSK is a managed service that runs Kafka clusters for you.Step 2: Compare options with MSK features
Only It manages Kafka servers for you, so you don't have to. correctly states that MSK manages Kafka servers, removing manual server management.Final Answer:
It manages Kafka servers for you, so you don't have to. -> Option AQuick Check:
Managed Kafka service = It manages servers [OK]
- Thinking MSK replaces Kafka
- Assuming manual server setup is needed
- Believing MSK only works with Lambda
Solution
Step 1: Identify MSK cluster endpoints format
Amazon MSK provides bootstrap servers as DNS names with ports, like in "b-1.mskcluster.abcd1234.kafka.us-east-1.amazonaws.com:9092,b-2.mskcluster.abcd1234.kafka.us-east-1.amazonaws.com:9092".Step 2: Eliminate incorrect formats
"localhost:9092" is localhost, not MSK. "127.0.0.1:2181" is Zookeeper port, not Kafka. "msk://cluster-endpoint" is not a valid Kafka bootstrap server format.Final Answer:
"b-1.mskcluster.abcd1234.kafka.us-east-1.amazonaws.com:9092,b-2.mskcluster.abcd1234.kafka.us-east-1.amazonaws.com:9092" -> Option DQuick Check:
MSK bootstrap servers = DNS names with ports [OK]
- Using localhost instead of MSK endpoints
- Confusing Zookeeper port with Kafka port
- Using invalid URI schemes like msk://
producer.send(new ProducerRecord<>("my-topic", "key1", "Hello MSK"));
System.out.println("Message sent successfully");Solution
Step 1: Understand producer.send behavior
The send method is asynchronous but the print statement runs immediately after.Step 2: Check output after send call
Since no error handling is shown and send is called, the print statement will output "Message sent successfully".Final Answer:
Message sent successfully -> Option BQuick Check:
Producer send + print = Message sent successfully [OK]
- Assuming send blocks until confirmed
- Expecting error without exception handling
- Thinking no output means success
Properties props = new Properties();
props.put("bootstrap.servers", "b-1.mskcluster.kafka:9092");
props.put("group.id", "test-group");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("my-topic"));
consumer.poll(Duration.ofMillis(100));
The consumer throws a runtime error about deserializer class not found. What is the likely cause?Solution
Step 1: Analyze the error message
The error says deserializer class not found, so the problem is with deserializer class names.Step 2: Check deserializer class names in code
They must be fully qualified and spelled exactly. Any typo causes class not found error.Final Answer:
The deserializer class names are incorrect or misspelled. -> Option CQuick Check:
Deserializer class error = wrong class name [OK]
- Ignoring class name typos
- Assuming bootstrap server causes deserializer error
- Missing group.id but error is about deserializer
Solution
Step 1: Understand Kafka consumer filtering options
Kafka consumers do not support filtering by key prefix via configuration properties.Step 2: Identify correct filtering method
Kafka Streams allows processing and filtering messages by key prefix programmatically.Step 3: Evaluate other options
There is no Kafka consumer property like "key.filter.prefix". Subscribing to topics named "user-*" filters by topic name, not message keys. Producer configuration controls what is sent, not what the consumer processes.Final Answer:
Use a Kafka Streams application with a filter on the key prefix. -> Option AQuick Check:
Filter keys after consuming = Kafka Streams filter [OK]
- Expecting consumer config to filter keys
- Filtering by topic name instead of message key
- Trying to filter on producer side only
Ready to prove it?
Pass the test and this pattern moves unknown
