Consider a Kafka topic with 3 partitions and 2 consumers in a consumer group. Given the following partition assignment logic, what partitions will each consumer receive?
partitions = [0, 1, 2] consumers = ['consumer1', 'consumer2'] assignment = {c: [] for c in consumers} for i, p in enumerate(partitions): assignment[consumers[i % len(consumers)]].append(p) print(assignment)
Partitions are assigned in a round-robin way based on the index modulo the number of consumers.
The code assigns partitions 0 and 2 to consumer1 because 0 % 2 = 0 and 2 % 2 = 0, and partition 1 to consumer2 because 1 % 2 = 1.
Which statement best describes the effect of increasing the number of partitions in a Kafka topic on throughput?
Think about how partitions allow multiple consumers to read in parallel.
More partitions allow more consumers to read in parallel, increasing throughput, but too many partitions can add overhead.
What error will this Kafka partition assignment code raise?
partitions = [0, 1, 2] consumers = ['c1', 'c2'] assignment = {c: [] for c in consumers} for i in range(len(partitions)+1): assignment[consumers[i % len(consumers)]].append(partitions[i]) print(assignment)
Check the loop range and how it accesses the partitions list.
The loop runs one iteration too many (len(partitions)+1), causing partitions[i] to access an invalid index.
Choose the correct Kafka CLI command to create a topic named 'logs' with 5 partitions.
Check the exact option name for partitions in Kafka CLI.
The correct option to specify partitions is '--partitions'. Other options are invalid and cause errors.
You want to achieve a throughput of 1 million messages per second. Each partition can handle 100,000 messages per second. How many partitions should your Kafka topic have at minimum?
Divide the target throughput by the capacity per partition.
1,000,000 / 100,000 = 10 partitions needed to meet the throughput.