Partition count strategy in Kafka - Time & Space Complexity
When working with Kafka, the number of partitions affects how messages are handled.
We want to understand how increasing partitions changes the work Kafka does.
Analyze the time complexity of assigning messages to partitions.
// Simple partition assignment based on key hash
int getPartition(String key, int partitionCount) {
int hash = key.hashCode();
int partition = Math.abs(hash) % partitionCount;
return partition;
}
This code decides which partition a message goes to by using the key's hash and the total partitions.
Look at what repeats when many messages are sent.
- Primary operation: Calculating the hash and modulo for each message.
- How many times: Once per message sent to Kafka.
As the number of messages grows, the work grows linearly.
| Input Size (n messages) | Approx. Operations |
|---|---|
| 10 | 10 hash and modulo calculations |
| 100 | 100 hash and modulo calculations |
| 1000 | 1000 hash and modulo calculations |
Pattern observation: Doubling messages doubles the work, since each message is handled once.
Time Complexity: O(n)
This means the time to assign partitions grows directly with the number of messages.
[X] Wrong: "More partitions means each message takes longer to assign."
[OK] Correct: The partition count only affects the modulo step, which is very fast and constant time. The main cost depends on how many messages you process, not how many partitions exist.
Understanding how partition count affects processing helps you explain Kafka's scalability clearly and confidently.
What if we changed the partition assignment to use a more complex hashing algorithm? How would the time complexity change?