Connector configuration in Kafka - Time & Space Complexity
When setting up a Kafka connector, it's important to understand how the configuration process scales as you add more settings.
We want to know how the time to apply configurations grows when the number of configuration entries increases.
Analyze the time complexity of the following code snippet.
Map<String, String> config = new HashMap<>();
config.put("connector.class", "FileStreamSourceConnector");
config.put("tasks.max", "1");
config.put("file", "/tmp/test.txt");
config.put("topic", "test-topic");
for (Map.Entry<String, String> entry : config.entrySet()) {
applyConfig(entry.getKey(), entry.getValue());
}
This code sets up a connector configuration map and applies each configuration entry one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each configuration entry to apply settings.
- How many times: Once for each configuration key-value pair.
As the number of configuration entries grows, the time to apply them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 applyConfig calls |
| 100 | 100 applyConfig calls |
| 1000 | 1000 applyConfig calls |
Pattern observation: The time grows directly with the number of configuration entries.
Time Complexity: O(n)
This means the time to apply configurations increases in a straight line as you add more settings.
[X] Wrong: "Applying configurations takes the same time no matter how many settings there are."
[OK] Correct: Each configuration entry requires a separate step, so more entries mean more time.
Understanding how configuration time grows helps you design connectors that scale well and avoid slow startups.
"What if the applyConfig function itself loops over all existing configurations each time it is called? How would the time complexity change?"
Practice
Solution
Step 1: Understand Kafka Connect role
Kafka Connect uses connectors to move data between Kafka and other systems.Step 2: Identify configuration purpose
The connector configuration tells Kafka Connect how to perform this data movement.Final Answer:
To specify how data moves between Kafka and external systems -> Option AQuick Check:
Connector configuration = data movement setup [OK]
- Confusing connector config with Kafka broker settings
- Thinking it configures Kafka topic partitions
- Mixing up consumer group ID with connector config
Solution
Step 1: Recall Kafka Connect JSON key format
Kafka Connect uses keys with dot notation, e.g., "connector.class".Step 2: Match correct key name
Only "connector.class": "org.apache.kafka.connect.file.FileStreamSourceConnector" uses the correct key "connector.class" with quotes and colon.Final Answer:
"connector.class": "org.apache.kafka.connect.file.FileStreamSourceConnector" -> Option BQuick Check:
Correct key is "connector.class" [OK]
- Using hyphens instead of dots in keys
- Using camelCase keys instead of dot notation
- Omitting quotes around keys or values
{
"name": "my-source-connector",
"connector.class": "org.apache.kafka.connect.file.FileStreamSourceConnector",
"tasks.max": "2",
"file": "/tmp/input.txt",
"topic": "my-topic"
}What does the
tasks.max setting control?Solution
Step 1: Understand tasks.max meaning
In Kafka Connect, tasks.max sets how many parallel tasks the connector can run.Step 2: Eliminate other options
It does not control topic count, batch size, or retry count.Final Answer:
Maximum number of tasks to run in parallel for this connector -> Option DQuick Check:
tasks.max = parallel task limit [OK]
- Confusing tasks.max with topic count
- Thinking it controls batch size
- Assuming it sets retry attempts
{
"name": "my-sink-connector",
"connector.class": "org.apache.kafka.connect.file.FileStreamSinkConnector",
"tasks.max": 1,
"topics": "my-topic"
}What is wrong with this configuration?
Solution
Step 1: Check required properties for FileStreamSinkConnector
This connector needs a "file" property to specify the output file path.Step 2: Validate other properties
"tasks.max" can be a number or string, "topics" is valid plural, and "connector.class" is correct.Final Answer:
Missing "file" property to specify output file -> Option CQuick Check:
FileStreamSinkConnector requires "file" property [OK]
- Assuming "tasks.max" must be string
- Confusing "topic" and "topics" keys
- Changing connector.class unnecessarily
Solution
Step 1: Understand topic properties in connectors
Source connectors typically use "topic" (singular) to specify the Kafka topic to publish data to.Step 2: Differentiate from other keys
"topics" is common for sink connectors; "topics.regex" is for pattern matching; "kafka.topic" is not standard.Final Answer:
"topic" -> Option AQuick Check:
Use "topic" to specify Kafka topic for sources [OK]
- Using plural "topics" instead of "topic"
- Confusing "topics.regex" with direct topic specification
- Inventing non-standard keys like "kafka.topic"
Ready to prove it?
Pass the test and this pattern moves unknown
