Bird
Raised Fist0
Kafkadevops~10 mins

Partition concept in Kafka - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Partition concept
Message Produced
↓
Partitioner Decides Partition
↓
Message Stored in Partition
↓
Consumer Reads from Partition
↓
Message Processing
A message is produced, the partitioner decides which partition it goes to, the message is stored there, and consumers read messages from partitions for processing.
Execution Sample
Kafka
producer.send(topic, key, value)
// Partitioner uses key to pick partition
// Message stored in that partition
// Consumer reads from assigned partitions
This code sends a message with a key to a Kafka topic; the partitioner uses the key to decide the partition where the message is stored; consumers read messages from partitions.
Process Table
StepActionKeyPartition ChosenMessage StoredConsumer Reads From
1Produce messageuser123Partition 2Partition 2Partition 2
2Produce messageuser456Partition 0Partition 0Partition 0
3Produce messageuser789Partition 1Partition 1Partition 1
4Produce messageuser123Partition 2Partition 2Partition 2
5Produce messagenullPartition 0 (round-robin)Partition 0Partition 0
6Stop producing----
💡 No more messages to produce; processing stops.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
KeyN/Auser123user456user789user123nullnull
Partition ChosenN/A201200
Messages in Partition 00011122
Messages in Partition 10001111
Messages in Partition 20111222
Key Moments - 2 Insights
Why does the same key always go to the same partition?
Because the partitioner uses the key's hash to pick a partition, so the same key maps to the same partition every time, as shown in steps 1 and 4 in the execution_table.
What happens when the key is null?
When the key is null, Kafka uses round-robin to assign partitions, so messages without keys are distributed evenly, as seen in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which partition does the message with key 'user789' go to?
APartition 0
BPartition 1
CPartition 2
DPartition 3
💡 Hint
Check step 3 in the execution_table under 'Partition Chosen'.
At which step does the partitioner assign a partition using round-robin because the key is null?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the row where the key is 'null' in the execution_table.
If the key 'user123' changed to 'user999', what would likely happen to the partition chosen?
AIt would change to a different partition
BIt would stay the same partition 2
CIt would cause an error
DIt would be assigned randomly every time
💡 Hint
Since partition depends on key hash, changing key changes partition, see variable_tracker for key and partition relation.
Concept Snapshot
Kafka Partition Concept:
- Messages are sent to topics divided into partitions.
- Partitioner uses message key to pick partition (same key → same partition).
- Messages without keys are assigned partitions round-robin.
- Consumers read messages from assigned partitions.
- Partitions enable parallelism and ordering per key.
Full Transcript
In Kafka, when a message is produced, the partitioner decides which partition the message goes to based on the message key. The same key always maps to the same partition, ensuring order for that key. If the key is null, Kafka assigns partitions in a round-robin way to balance load. Consumers read messages from partitions to process them. This flow helps Kafka handle many messages efficiently and keep order per key.

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