📖 Scenario: You are setting up a Kafka topic for a small online store. You want to decide how many partitions the topic should have to handle the store's order messages efficiently.
🎯 Goal: Build a simple program that sets the number of partitions for a Kafka topic based on the number of expected orders per second.
📋 What You'll Learn
Create a variable with the expected number of orders per second
Create a variable for the maximum orders per partition per second
Calculate the number of partitions needed using integer division and rounding up
Print the number of partitions
💡 Why This Matters
🌍 Real World
Kafka topics use partitions to spread data load. Knowing how many partitions to create helps keep your system fast and reliable.
💼 Career
Understanding partition count strategy is important for roles like data engineers and backend developers working with Kafka.
Progress0 / 4 steps
1
Set expected orders per second
Create a variable called orders_per_second and set it to 1200.
Kafka
Hint
Think about how many orders your store expects to receive every second.
2
Set max orders per partition
Create a variable called max_orders_per_partition and set it to 300.
Kafka
Hint
This is the maximum number of orders one partition can handle per second.
3
Calculate number of partitions
Create a variable called partitions that calculates the number of partitions needed by dividing orders_per_second by max_orders_per_partition and rounding up to the nearest whole number.
Kafka
Hint
Use integer division and add max_orders_per_partition - 1 before dividing to round up.
4
Print the number of partitions
Write a print statement to display the value of partitions.
Kafka
Hint
Use print(partitions) to show the result.
Practice
(1/5)
1. What does the partition count in a Kafka topic control?
easy
A. The number of parts a topic is divided into for parallel processing
B. The number of messages a topic can hold
C. The size limit of each message in the topic
D. The number of consumers allowed to read the topic
Solution
Step 1: Understand Kafka topic structure
A Kafka topic is divided into partitions to allow parallel processing of messages.
Step 2: Identify what partition count controls
The partition count sets how many parts the topic is split into, enabling multiple consumers to read in parallel.
Final Answer:
The number of parts a topic is divided into for parallel processing -> Option A
Quick Check:
Partition count = number of topic parts [OK]
Hint: Partition count means how many parts a topic has [OK]
4. You tried to create a Kafka topic with 0 partitions using the command: kafka-topics --create --topic test --num-partitions 0 --bootstrap-server localhost:9092. What is the likely issue?
medium
A. Kafka allows 0 partitions, so topic created with no data storage
B. Error because partition count must be at least 1
C. Topic created with default partition count of 1
D. Command ignored the partition count and created topic anyway
Solution
Step 1: Understand partition count constraints
Kafka requires at least one partition per topic; zero is invalid.
Step 2: Identify command behavior
Setting partitions to 0 causes an error because Kafka cannot create a topic without partitions.
Final Answer:
Error because partition count must be at least 1 -> Option B
Quick Check:
Partition count ≥ 1 required [OK]
Hint: Partition count cannot be zero or negative [OK]
Common Mistakes:
Assuming Kafka defaults to 1 partition silently
Thinking zero partitions means no data but topic exists
Ignoring error messages from Kafka CLI
5. You have a Kafka topic with 5 partitions and want to increase parallel processing. Which is the best strategy?
hard
A. Delete the topic and create a new one with 1 partition
B. Decrease the partition count to 3 to reduce complexity
C. Keep partition count at 5 and add more consumers than partitions
D. Increase the partition count to 10 and rebalance consumers
Solution
Step 1: Understand effect of partition count on parallelism
More partitions allow more consumers to read in parallel, increasing throughput.
Step 2: Evaluate options for increasing parallel processing
Increasing partitions to 10 allows better parallelism; rebalance consumers to use new partitions.
Final Answer:
Increase the partition count to 10 and rebalance consumers -> Option D
Quick Check:
More partitions = better parallel processing [OK]
Hint: More partitions mean more parallel consumers [OK]
Common Mistakes:
Reducing partitions thinking it improves speed
Adding more consumers than partitions without increasing partitions
Deleting topic unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.