Apache Kafka
Message Ordering
Partitioning
Runtime Scaling
Distributed Systems

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.

Practice system design

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 IDKeyOriginal PartitionNew Partition ConfigurationNotes
1A00Consistent order in original partition
2B01 (after reconfiguration)Moves to new partition; starts new order sequence
3A00Continues order in original partition
4A00Order with Key A is maintained
5B-1Follows 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
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