Topic creation in Kafka - Time & Space Complexity
When creating a topic in Kafka, it is important to understand how the time taken grows as the number of topics or partitions increases.
We want to know how the creation process scales when we add more topics or partitions.
Analyze the time complexity of the following Kafka topic creation code snippet.
// Create a Kafka topic with a given number of partitions
AdminClient admin = AdminClient.create(props);
NewTopic newTopic = new NewTopic("my-topic", numPartitions, (short) replicationFactor);
admin.createTopics(Collections.singleton(newTopic)).all().get();
admin.close();
This code creates one topic with a specified number of partitions and replication factor.
Look for operations that repeat or scale with input size.
- Primary operation: Creating partitions inside the topic.
- How many times: The number of partitions requested (numPartitions).
As the number of partitions increases, the time to create the topic grows roughly in proportion.
| Input Size (numPartitions) | Approx. Operations |
|---|---|
| 10 | About 10 partition creation steps |
| 100 | About 100 partition creation steps |
| 1000 | About 1000 partition creation steps |
Pattern observation: The work grows linearly as the number of partitions increases.
Time Complexity: O(n)
This means the time to create a topic grows in a straight line with the number of partitions.
[X] Wrong: "Creating a topic always takes the same time, no matter how many partitions it has."
[OK] Correct: Each partition requires setup, so more partitions mean more work and more time.
Understanding how topic creation time grows helps you design Kafka systems that scale well and avoid surprises in production.
"What if we create multiple topics at once instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand Kafka topic role
A Kafka topic is a category or feed name to which messages are published.Step 2: Compare options with topic function
Only To organize messages like folders organize files correctly describes topics as organizing messages like folders organize files.Final Answer:
To organize messages like folders organize files -> Option DQuick Check:
Topic = Organize messages [OK]
- Confusing topics with databases
- Thinking topics store user data
- Mixing topics with program compilation
orders with 3 partitions and replication factor 2?Solution
Step 1: Identify correct Kafka topic creation syntax
The official command uses kafka-topics.sh with --create, --topic, --partitions, --replication-factor, and --bootstrap-server options.Step 2: Match options with correct syntax
kafka-topics.sh --create --topic orders --partitions 3 --replication-factor 2 --bootstrap-server localhost:9092 matches the correct syntax exactly; others use wrong commands or options.Final Answer:
kafka-topics.sh --create --topic orders --partitions 3 --replication-factor 2 --bootstrap-server localhost:9092 -> Option AQuick Check:
Correct syntax = kafka-topics.sh --create --topic orders --partitions 3 --replication-factor 2 --bootstrap-server localhost:9092 [OK]
- Using wrong command names
- Missing --bootstrap-server option
- Incorrect option names like --replicas
kafka-topics.sh --create --topic sales --partitions 5 --replication-factor 1 --bootstrap-server localhost:9092
Solution
Step 1: Analyze command parameters
The command specifies topic 'sales', 5 partitions, replication factor 1, and bootstrap server.Step 2: Understand Kafka topic creation rules
Replication factor 1 is valid (means no replicas), and partitions set to 5 means 5 partitions created.Final Answer:
Creates a topic named 'sales' with 5 partitions and replication factor 1 -> Option CQuick Check:
Partitions=5, Replication=1 [OK]
- Confusing partitions with replication factor
- Assuming replication factor 1 is invalid
- Expecting zookeeper connection with bootstrap-server
kafka-topics.sh --create --topic logs --partitions two --replication-factor 3 --bootstrap-server localhost:9092
What is the error?
Solution
Step 1: Check partitions parameter
The partitions option expects a number, but 'two' is a word, causing a syntax error.Step 2: Verify other parameters
Replication factor 3 and bootstrap server are valid; --create flag is present.Final Answer:
Invalid number for partitions: 'two' should be a number -> Option BQuick Check:
Partitions must be numeric [OK]
- Using words instead of numbers for partitions
- Assuming replication factor 3 is invalid
- Forgetting --create flag
Solution
Step 1: Understand requirements for critical system
High load needs many partitions for parallelism; no message loss needs high replication factor.Step 2: Evaluate options
Partitions: 10, Replication factor: 3 has 10 partitions (high load) and replication factor 3 (high safety). Others have fewer partitions or lower replication.Final Answer:
Partitions: 10, Replication factor: 3 -> Option AQuick Check:
High load + safety = many partitions + high replication [OK]
- Choosing low replication for safety
- Choosing few partitions for high load
- Ignoring replication factor importance
Ready to prove it?
Pass the test and this pattern moves unknown
