Topic configuration in Kafka - Time & Space Complexity
When working with Kafka topics, it's important to understand how configuration settings affect performance.
We want to see how the time to create or update a topic changes as the topic settings grow.
Analyze the time complexity of creating a Kafka topic with multiple partitions and replicas.
NewTopic topic = new NewTopic("my-topic", numPartitions, replicationFactor);
topic.configs(Map.of(
"cleanup.policy", "compact",
"retention.ms", "604800000"
));
adminClient.createTopics(List.of(topic)).all().get();
This code creates a topic with a given number of partitions and replicas, applying some configuration settings.
Look for operations that repeat or scale with input size.
- Primary operation: Creating partitions and assigning replicas for each partition.
- How many times: Once per partition, so the number of partitions determines repetition.
As the number of partitions increases, the work to create the topic grows roughly in a straight line.
| Input Size (Partitions) | Approx. Operations |
|---|---|
| 10 | 10 partition creations and replica assignments |
| 100 | 100 partition creations and replica assignments |
| 1000 | 1000 partition creations and replica assignments |
Pattern observation: Doubling partitions roughly doubles the work needed.
Time Complexity: O(n)
This means the time to create a topic grows linearly with the number of partitions.
[X] Wrong: "Adding more replicas does not affect creation time much."
[OK] Correct: Each replica must be assigned and synchronized, so more replicas increase the work per partition.
Understanding how topic configuration scales helps you explain system behavior clearly and shows you grasp practical performance considerations.
"What if we increased the replication factor instead of partitions? How would the time complexity change?"