Partition concept in Kafka - Commands & Configuration
{
"TopicName": "example-topic",
"Partitions": 3,
"ReplicationFactor": 2
}This JSON file defines a Kafka topic named example-topic with 3 partitions. Each partition allows parallel processing of messages. The ReplicationFactor of 2 means each partition is copied to two brokers for safety.
kafka-topics.sh --create --topic example-topic --partitions 3 --replication-factor 2 --bootstrap-server localhost:9092
--partitions - Sets the number of partitions for the topic--replication-factor - Sets how many copies of each partition exist--bootstrap-server - Specifies the Kafka server to connect tokafka-topics.sh --describe --topic example-topic --bootstrap-server localhost:9092--describe - Shows detailed information about the topic--topic - Specifies which topic to describe--bootstrap-server - Specifies the Kafka server to connect toPartitions split a topic so messages can be processed in parallel and scaled across servers.
Practice
What is the main purpose of partitions in a Kafka topic?
Solution
Step 1: Understand the role of partitions in Kafka
Partitions divide a topic into smaller parts to allow parallel processing and maintain message order within each partition.Step 2: Identify the main benefit of partitions
Partitions improve speed by enabling parallelism and keep order for messages with the same key.Final Answer:
To split the topic into smaller parts for better speed and order -> Option AQuick Check:
Partitions = speed and order [OK]
- Confusing partitions with encryption
- Thinking partitions store messages permanently
- Assuming partitions compress data
Which of the following is the correct way to specify the number of partitions when creating a Kafka topic using the command line?
kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092 --partitions ?
Solution
Step 1: Recall Kafka topic creation syntax
The correct option to specify partitions is '--partitions' followed by the number.Step 2: Match the correct option
Among the options, '--partitions 3' is the valid syntax for setting 3 partitions.Final Answer:
--partitions 3 -> Option CQuick Check:
Partitions flag = --partitions [OK]
- Using --partition-count instead of --partitions
- Using --num-partitions which is invalid
- Using singular --partition flag
Given a Kafka topic with 3 partitions, if messages with keys "key1", "key2", and "key1" are sent in that order, which partition will the second "key1" message go to?
Solution
Step 1: Understand key-based partitioning
Kafka uses the message key to decide the partition. Messages with the same key always go to the same partition.Step 2: Apply to the given keys
Since both messages have key "key1", the second message will go to the same partition as the first "key1" message.Final Answer:
Same partition as the first "key1" message -> Option BQuick Check:
Same key = same partition [OK]
- Assuming keys distribute randomly each time
- Thinking second message goes to a different partition
- Ignoring key-based partitioning
Consider this Kafka producer code snippet in Java:
ProducerRecord record = new ProducerRecord<>("my-topic", null, "message");
producer.send(record);What is the issue related to partitioning in this code?
Solution
Step 1: Analyze the ProducerRecord parameters
The key parameter is null, which means Kafka cannot use key-based partitioning.Step 2: Understand partition assignment without a key
Without a key, Kafka assigns messages to partitions in a round-robin or default way, which can be random.Final Answer:
The key is null, so messages may be sent to random partitions -> Option AQuick Check:
Null key = random partition [OK]
- Thinking topic name is missing (it is present)
- Assuming message value is null (it's "message")
- Ignoring partitioning effect of null key
You have a Kafka topic with 4 partitions. You want to ensure that all messages with the same customer ID go to the same partition to keep their order. Which approach should you use?
Solution
Step 1: Understand how Kafka assigns partitions
Kafka uses the message key to decide which partition a message goes to, ensuring order for messages with the same key.Step 2: Apply to customer ID ordering requirement
Using the customer ID as the key ensures all messages for that customer go to the same partition and keep order.Step 3: Evaluate other options
Sending all to partition 0 limits scalability; random keys break order; separate topics complicate management.Final Answer:
Use the customer ID as the message key when producing messages -> Option DQuick Check:
Key = customer ID for order [OK]
- Sending all messages to one partition reduces scalability
- Using random keys breaks message order
- Creating multiple topics is unnecessary and complex
Ready to prove it?
Pass the test and this pattern moves unknown
