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?"
Practice
retention.ms configuration control in a Kafka topic?Solution
Step 1: Understand retention.ms meaning
Theretention.mssetting defines the time in milliseconds that Kafka retains messages before deleting them.Step 2: Compare with other options
Other options like partitions, replication factor, and message size are controlled by different configurations.Final Answer:
How long messages are kept before deletion -> Option AQuick Check:
retention.ms = message retention time [OK]
- Confusing retention.ms with replication factor
- Thinking retention.ms controls partition count
- Assuming retention.ms limits message size
Solution
Step 1: Identify correct option name
The correct option for replication factor is--replication-factorwithout equals sign.Step 2: Check syntax correctness
kafka-topics.sh --create --topic myTopic --replication-factor 3 --partitions 1 --bootstrap-server localhost:9092 uses the correct syntax with spaces and proper option names; others have wrong option names or equals sign which is invalid here.Final Answer:
kafka-topics.sh --create --topic myTopic --replication-factor 3 --partitions 1 --bootstrap-server localhost:9092 -> Option AQuick Check:
Use --replication-factor with space, no equals [OK]
- Using --replication instead of --replication-factor
- Adding equals sign (=) after option name
- Misspelling replication-factor option
kafka-topics.sh --alter --topic logs --config retention.ms=60000 --bootstrap-server localhost:9092What will be the effect on the topic
logs?Solution
Step 1: Understand the command's purpose
The command alters the topiclogsby settingretention.msto 60000 milliseconds (1 minute).Step 2: Interpret retention.ms effect
This means messages older than 1 minute will be deleted automatically.Final Answer:
Messages will be retained for 1 minute before deletion -> Option DQuick Check:
retention.ms=60000 means 1 minute retention [OK]
- Thinking retention.ms controls message count
- Confusing retention.ms with replication factor
- Assuming retention.ms disables deletion
kafka-topics.sh --alter --topic data --config cleanup.policy=delete,compact --bootstrap-server localhost:9092But it failed. What is the likely cause?
Solution
Step 1: Check cleanup.policy valid values
Kafka allows cleanup.policy to be either 'delete' or 'compact' or both separated by comma, but the syntax must be exact and supported by the Kafka version.Step 2: Identify common error
Some Kafka versions or setups do not support combined policies or require no spaces; if the command failed, likely the value was invalid or syntax incorrect.Final Answer:
The cleanup.policy value is invalid; it must be either 'delete' or 'compact', not both -> Option BQuick Check:
cleanup.policy must be valid single or combined value [OK]
- Using unsupported combined cleanup policies
- Trying to alter topic with wrong bootstrap server
- Assuming topic names cannot be altered
events with 4 partitions, replication factor 2, and a retention time of 2 hours. Which command correctly sets all these configurations?Solution
Step 1: Verify partitions and replication factor
The topic requires 4 partitions and replication factor 2, so options with these exact values are correct.Step 2: Calculate retention.ms for 2 hours
2 hours = 2 * 60 * 60 * 1000 = 7,200,000 milliseconds. Only kafka-topics.sh --create --topic events --partitions 4 --replication-factor 2 --config retention.ms=7200000 --bootstrap-server localhost:9092 uses this correct retention.ms value.Final Answer:
kafka-topics.sh --create --topic events --partitions 4 --replication-factor 2 --config retention.ms=7200000 --bootstrap-server localhost:9092 -> Option CQuick Check:
Retention in ms for 2 hours = 7200000 [OK]
- Mixing partitions and replication factor values
- Using retention.ms in seconds or minutes instead of ms
- Incorrect retention.ms calculation
Ready to prove it?
Pass the test and this pattern moves unknown
