Bird
Raised Fist0
Kafkadevops~30 mins

Partition concept in Kafka - Mini Project: Build & Apply

Choose your learning style10 modes available
Understanding Kafka Partition Concept
📖 Scenario: You are working with Apache Kafka, a system that helps send messages quickly and reliably. Kafka splits messages into parts called partitions. Each partition holds a part of the messages for a topic.Think of a topic as a big box of letters, and partitions as smaller boxes inside it. Each smaller box holds some letters. This helps many people read and write letters at the same time without waiting.
🎯 Goal: You will create a simple Kafka topic with partitions, send messages to specific partitions, and read messages from them. This will help you understand how partitions work in Kafka.
📋 What You'll Learn
Create a Kafka topic named my_topic with exactly 3 partitions.
Create a variable partition_key to decide which partition to send messages to.
Send messages to my_topic using the partition_key to select the partition.
Read and print messages from each partition separately.
💡 Why This Matters
🌍 Real World
Kafka partitions help large systems handle many messages quickly by splitting data into parts. This is like dividing work among many helpers to finish faster.
💼 Career
Understanding Kafka partitions is important for roles in data engineering, backend development, and system architecture where message streaming and real-time data processing are used.
Progress0 / 4 steps
1
Create Kafka topic with 3 partitions
Write the Kafka command to create a topic called my_topic with exactly 3 partitions.
Kafka
Hint

Use the kafka-topics command with --create, specify --topic my_topic, and set --partitions 3.

2
Set partition key variable
In your Kafka producer script, create a variable called partition_key and set it to 1 to send messages to partition 1.
Kafka
Hint

Just write partition_key = 1 in your script.

3
Send messages to chosen partition
Write code to send the message 'Hello Kafka' to my_topic using the partition_key to select the partition.
Kafka
Hint

Use kafka-console-producer with --topic my_topic and send the message Hello Kafka. Use key to control partition.

4
Read and print messages from each partition
Write commands to read and print messages from each partition (0, 1, and 2) of my_topic separately.
Kafka
Hint

Use kafka-console-consumer with --partition option for each partition.

Practice

(1/5)
1.

What is the main purpose of partitions in a Kafka topic?

easy
A. To split the topic into smaller parts for better speed and order
B. To encrypt messages for security
C. To store messages permanently on disk
D. To compress messages to save space

Solution

  1. 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.
  2. Step 2: Identify the main benefit of partitions

    Partitions improve speed by enabling parallelism and keep order for messages with the same key.
  3. Final Answer:

    To split the topic into smaller parts for better speed and order -> Option A
  4. Quick Check:

    Partitions = speed and order [OK]
Hint: Partitions split topics to speed up and keep order [OK]
Common Mistakes:
  • Confusing partitions with encryption
  • Thinking partitions store messages permanently
  • Assuming partitions compress data
2.

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 ?
easy
A. --num-partitions 3
B. --partition-count 3
C. --partitions 3
D. --partition 3

Solution

  1. Step 1: Recall Kafka topic creation syntax

    The correct option to specify partitions is '--partitions' followed by the number.
  2. Step 2: Match the correct option

    Among the options, '--partitions 3' is the valid syntax for setting 3 partitions.
  3. Final Answer:

    --partitions 3 -> Option C
  4. Quick Check:

    Partitions flag = --partitions [OK]
Hint: Use --partitions to set number when creating topic [OK]
Common Mistakes:
  • Using --partition-count instead of --partitions
  • Using --num-partitions which is invalid
  • Using singular --partition flag
3.

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?

medium
A. Partition 1
B. Same partition as the first "key1" message
C. Partition 0
D. A random partition different from the first "key1" message

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Same partition as the first "key1" message -> Option B
  4. Quick Check:

    Same key = same partition [OK]
Hint: Same keys always go to the same partition [OK]
Common Mistakes:
  • Assuming keys distribute randomly each time
  • Thinking second message goes to a different partition
  • Ignoring key-based partitioning
4.

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?

medium
A. The key is null, so messages may be sent to random partitions
B. The topic name is missing
C. The message value is null
D. Producer is not closed after sending

Solution

  1. Step 1: Analyze the ProducerRecord parameters

    The key parameter is null, which means Kafka cannot use key-based partitioning.
  2. 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.
  3. Final Answer:

    The key is null, so messages may be sent to random partitions -> Option A
  4. Quick Check:

    Null key = random partition [OK]
Hint: Null key means no fixed partition, messages spread randomly [OK]
Common Mistakes:
  • Thinking topic name is missing (it is present)
  • Assuming message value is null (it's "message")
  • Ignoring partitioning effect of null key
5.

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?

hard
A. Create 4 separate topics, one per customer
B. Send all messages to partition 0 manually
C. Use a random key for each message
D. Use the customer ID as the message key when producing messages

Solution

  1. 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.
  2. 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.
  3. Step 3: Evaluate other options

    Sending all to partition 0 limits scalability; random keys break order; separate topics complicate management.
  4. Final Answer:

    Use the customer ID as the message key when producing messages -> Option D
  5. Quick Check:

    Key = customer ID for order [OK]
Hint: Use customer ID as key to keep order in one partition [OK]
Common Mistakes:
  • Sending all messages to one partition reduces scalability
  • Using random keys breaks message order
  • Creating multiple topics is unnecessary and complex
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard