Kafka
Partitioning
Data Management
Distributed Systems
Software Architecture

How does kafka decides the partition if I don't mention any

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is an open-source stream-processing software platform developed by the Apache Software Foundation. In a Kafka cluster, topics are divided into multiple partitions which allow the log to scale and enables multiple consumers to read from a topic in parallel. However, when a producer sends data to a Kafka topic and does not specify a partition, Kafka determines the appropriate partition through a specific strategy. Understanding how Kafka decides the partition in such scenarios is crucial for designing efficient Kafka applications.

Default Partitioning Logic

When a producer sends a message to a topic without specifying a partition, Kafka uses the following strategy to determine which partition the message should go to:

  1. Custom Partitioner: If a custom partitioner is defined, it takes precedence. A custom partitioner allows the specification of an algorithm to determine the partitioning of the data.
  2. Key-Based Partitioning: If the producer specifies a key for the message, Kafka uses a hash function on the key to determine the partition. The default hash function ensures that the same key always goes to the same partition, thus maintaining the order of records with the same key.
  3. Round-Robin Partitioning: If no key is provided, Kafka uses a round-robin algorithm to distribute messages across the available partitions. The round-robin scheduler systematically cycles through all partitions, ensuring a balanced load across them.

Examples

  • Custom Partitioner Example: A custom partitioner can be designed to distribute messages based on specific criteria. For example, if messages contain a field userID and we decide to partition by this field, the custom partitioner might hash the userID and modulo by the number of partitions.
java
1  public class UserIDPartitioner implements Partitioner {
2      public void configure(Map<String, ?> configs) {}
3
4      public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
5          int numPartitions = cluster.partitionCountForTopic(topic);
6          return key.hashCode() % numPartitions;
7      }
8
9      public void close() {}
10  }
  • Key-Based Partitioning Example: When a key is provided, such as in the below code snippet, Kafka will hash this key to choose a partition:
java
  producer.send(new ProducerRecord<String, String>("topicName", "key", "value"));
  • Round-Robin Example: Without a key, Kafka defaults to a round-robin approach among available partitions:
java
  producer.send(new ProducerRecord<String, String>("topicName", null, "value")); // No key provided

Why Partitioning Matters

Partitioning can significantly impact performance and scalability in Kafka. Proper partitioning ensures:

  • Load Balancing: Effective distribution of data across the cluster.
  • High Throughput: Parallel processing of messages by consumers.
  • Fault Tolerance: Messages are replicated across different brokers.

Summary Table

ConditionPartitioning StrategyPurpose/Advantage
Custom Partitioner DefinedCustom Algorithm by DeveloperCustom control over data distribution
Key ProvidedHash Function on KeyEnsures order for specific keyed messages
No KeyRound-Robin DistributionEqual distribution among partitions

Additional Considerations

  • Impact on Consumer: The way data is partitioned affects how consumer groups can parallelize consumption. Consumers in the same group will read from exclusive partitions.
  • Repartitioning: Changing the number of partitions can cause data redistribution and potential temporary unavailability. Use cautiously.
  • Performance: Key hashing and partition calculation add overhead. In high-throughput environments, ensure your partitioning logic can handle the scale.

In conclusion, Kafka provides flexible mechanisms to determine message partitioning, based on the producer's configuration. Understanding and configuring these mechanisms properly ensures Kafka applications are scalable, efficient, and fault-tolerant.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.