How Kafka guarantee the messages order while we increase the partitions in runtime?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is renowned for its performance and durability, crucial for implementing real-time streaming applications and resilient systems. One essential feature of Kafka is its ordering guarantees of messages. However, when it comes to increasing the number of partitions in a Kafka topic at runtime, the dynamics of these guarantees can change, especially in relation to the order of messages.
Kafka's Message Ordering Guarantees
Initially, it’s important to understand that Kafka maintains order at the partition level, not across the entire topic. Messages are appended to partitions in the order they are sent by the producer. As long as the producer sends messages to a specific partition, they will be stored and can be consumed in the exact order in which they were produced.
Effect of Partition Increase
When a Kafka topic's partition count is increased, new partitions are created, but the existing data in old partitions remains unaffected. The challenge in maintaining order arises when producers start sending messages that are routed to these new partitions. Here are the key considerations:
1. Partitioning Strategy
Producers determine to which partition a message is sent. This can be done either:
- Automatically by Kafka: Based on the key of the message. Kafka uses a hash function on the key to assign the partition, ensuring that the same key always goes to the same partition.
- Manually by the Producer: The producer can explicitly specify the partition.
2. Key-based Partitioning and Order
When keys are used, Kafka guarantees that all messages with the same key will go to the same partition. Thus, the order of messages with the same key is maintained. However, this does not necessarily preserve the global order among all messages sent across different keys.
3. Implications of Increasing Partitions
Increasing partitions does not redistribute existing data; it only affects new messages. If a producer starts using new partitions (either via automatic or manual means), those new partitions begin with their own order sequence. Messages pushed to new partitions will not maintain a contiguous order with messages in old partitions.
Example
Consider a topic with 1 partition where messages are sent as follows:
- Message 1 (Key A) -> Partition 0
- Message 2 (Key B) -> Partition 0
- Message 3 (Key A) -> Partition 0
If a second partition is added and the hash function routes Key B to the new partition while Key A continues to go to the original partition, subsequent messages would look like this:
- Message 4 (Key A) -> Partition 0
- Message 5 (Key B) -> Partition 1
In this scenario, all messages with Key A maintain order in Partition 0, and all messages with Key B start a new order sequence in Partition 1.
Table: Message Ordering with Increased Partitions
| Message ID | Key | Original Partition | New Partition Configuration | Notes |
| 1 | A | 0 | 0 | Consistent order in original partition |
| 2 | B | 0 | 1 (after reconfiguration) | Moves to new partition; starts new order sequence |
| 3 | A | 0 | 0 | Continues order in original partition |
| 4 | A | 0 | 0 | Order with Key A is maintained |
| 5 | B | - | 1 | Follows Message 2 in new partition |
Conclusion
Increasing partitions in Kafka is a common strategy for scaling, but it must be handled carefully regarding message ordering. Kafka's ordering guarantees are strictly per partition and not across partitions. When partitions are increased, understanding the partitioning mechanism (key-based or otherwise) is crucial to managing how message order is affected. For a strictly ordered sequence across a topic, careful design of key allocations and partition usage is essential.
Related reading
- How kafka identifies consumers in a group uniquely
- How Kafka leader replica decides to advance Highwater Mark HW when replicating data to follower replicas
- How Kafka Nodes and zookeeper will communicate with each other?
- How load balancer works in RabbitMQ
- How like button is implemented in distributed systems?
- How many producers to create in kafka?
- How Logstash is different than Kafka
- How long the messages will be kept stored in topic/partition using kafka 0.9

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.