Kafka on Kubernetes (Strimzi) - Time & Space Complexity
When running Kafka on Kubernetes using Strimzi, it's important to understand how the system handles tasks as the number of Kafka topics or partitions grows.
We want to know how the time to manage Kafka resources changes as we add more topics or partitions.
Analyze the time complexity of the following Kafka resource reconciliation loop in Strimzi.
for (Topic topic : kafkaCluster.getTopics()) {
reconcileTopic(topic);
for (Partition partition : topic.getPartitions()) {
assignPartition(partition);
}
}
This code loops through all Kafka topics and then through all partitions of each topic to assign them properly in the Kubernetes cluster.
Look at the loops that repeat work:
- Primary operation: Looping through all topics and then all partitions inside each topic.
- How many times: The outer loop runs once per topic, and the inner loop runs once per partition per topic.
As the number of topics and partitions grows, the work grows too.
| Input Size (topics * partitions) | Approx. Operations |
|---|---|
| 10 topics * 5 partitions | 50 operations |
| 100 topics * 5 partitions | 500 operations |
| 1000 topics * 5 partitions | 5000 operations |
Pattern observation: The total work grows roughly in proportion to the number of topics times the number of partitions.
Time Complexity: O(n * m)
This means the time to complete the task grows in direct proportion to the number of topics (n) multiplied by the number of partitions per topic (m).
[X] Wrong: "The time to assign partitions grows only with the number of topics, not partitions."
[OK] Correct: Each partition needs separate handling, so the total work depends on both topics and partitions, not just topics alone.
Understanding how Kafka scales on Kubernetes helps you explain how systems handle growing workloads, a key skill in DevOps roles.
"What if we changed the code to assign partitions in parallel instead of sequentially? How would the time complexity change?"
Practice
Solution
Step 1: Understand Strimzi's role
Strimzi is designed to help run Kafka on Kubernetes by managing Kafka clusters easily.Step 2: Identify the correct purpose
It automates pod management, storage, and recovery, simplifying Kafka operations on Kubernetes.Final Answer:
To simplify and automate Kafka cluster management on Kubernetes -> Option AQuick Check:
Strimzi = Kafka automation on Kubernetes [OK]
- Thinking Strimzi replaces Kubernetes
- Confusing Strimzi with Kafka UI tools
- Assuming Strimzi converts Kafka messages to SQL
kafka-cluster.yaml using Strimzi on Kubernetes?Solution
Step 1: Recall how Kubernetes applies YAML files
Kubernetes useskubectl apply -f filename.yamlto create or update resources.Step 2: Match the command to Strimzi usage
Strimzi uses Kubernetes resources, so applying the Kafka cluster YAML is done withkubectl apply -f.Final Answer:
kubectl apply -f kafka-cluster.yaml -> Option CQuick Check:
Apply YAML with kubectl apply -f [OK]
- Using non-existent 'strimzi create' command
- Confusing kubectl run with apply
- Trying to use kafka CLI for Kubernetes YAML
apiVersion: kafka.strimzi.io/v1beta2
kind: Kafka
metadata:
name: my-cluster
spec:
kafka:
replicas: 3
storage:
type: ephemeral
zookeeper:
replicas: 3
storage:
type: ephemeral
What will be the number of Kafka broker pods created in the Kubernetes cluster?
Solution
Step 1: Identify Kafka replicas in YAML
Thereplicasfield underkafkais set to 3, meaning 3 Kafka broker pods.Step 2: Understand pod creation
Strimzi creates one pod per replica, so 3 Kafka broker pods will be created.Final Answer:
3 -> Option AQuick Check:
Kafka replicas = 3 pods [OK]
- Adding Kafka and Zookeeper replicas together
- Confusing ephemeral storage with pod count
- Assuming default pod count if replicas missing
Solution
Step 1: Check Kafka replicas in YAML
If YAML specifies 3 replicas, pod count should be 3, soreplicas: 1is unlikely.Step 2: Consider Strimzi operator role
Strimzi operator manages Kafka pods. If it is missing or not running, pods won't be created properly.Final Answer:
Strimzi operator is not installed or running -> Option DQuick Check:
Operator missing = pods not managed [OK]
- Blaming Zookeeper replicas for Kafka pod count
- Thinking ephemeral storage stops pod creation
- Assuming YAML replicas value is always correct
Solution
Step 1: Understand Strimzi persistent storage syntax
Strimzi usespersistent-claimtype with avolumeClaimTemplateto define PVC size.Step 2: Identify correct YAML structure
storage: type: persistent-claim volumeClaimTemplate: spec: resources: requests: storage: 10Gi correctly usesvolumeClaimTemplate.spec.resources.requests.storage: 10Gito request 10Gi persistent volume.Final Answer:
storage: type: persistent-claim volumeClaimTemplate: spec: resources: requests: storage: 10Gi -> Option BQuick Check:
Persistent storage needs volumeClaimTemplate with size [OK]
- Using 'size' directly under storage without volumeClaimTemplate
- Using wrong storage type like 'persistent'
- Confusing persistentVolume with persistent-claim
Ready to prove it?
Pass the test and this pattern moves unknown
