Bird
Raised Fist0
Kafkadevops~10 mins

Partition concept in Kafka - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the number of partitions when creating a Kafka topic.

Kafka
new NewTopic("my-topic", [1], (short) 1);
Drag options to blanks, or click blank then click option'
A0
B3
C-1
D"3"
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or negative numbers for partitions.
Using a string instead of an integer.
2fill in blank
medium

Complete the code to send a message to a specific partition in Kafka.

Kafka
producer.send(new ProducerRecord<>("my-topic", [1], "key", "value"));
Drag options to blanks, or click blank then click option'
A1
B"partition"
C"1"
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for partition.
Passing null when a specific partition is required.
3fill in blank
hard

Fix the error in the code to correctly create a Kafka topic with partitions.

Kafka
NewTopic topic = new NewTopic("my-topic", [1], (short) 1);
Drag options to blanks, or click blank then click option'
A"3"
B0
C3
D-3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer.
Using zero or negative numbers.
4fill in blank
hard

Fill both blanks to create a ProducerRecord that sends a message to partition 2 with key "user1".

Kafka
ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", [1], [2], "message");
Drag options to blanks, or click blank then click option'
A2
B"user1"
C"user2"
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping partition and key positions.
Using integers for keys or strings for partitions.
5fill in blank
hard

Fill all three blanks to create a NewTopic with name "events", 4 partitions, and replication factor 2.

Kafka
NewTopic topic = new NewTopic([1], [2], (short) [3]);
Drag options to blanks, or click blank then click option'
A"events"
B4
C2
D"4"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings for numbers.
Mixing up partitions and replication factor.

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