Video in progress
What happens to keyed messages if you add partitions to a topic that's already running?
The partition for a key is computed as hash % number_of_partitions, so when the number changes, many keys move to a different partition. Old messages stay where they are and new ones go elsewhere, so ordering by key is temporarily broken.
📖 Answer
Example: with 4 partitions, order-17 used to land in partition 1. You increase to 6, and now it lands in partition 5. The old events of this order are in partition 1, the new ones in partition 5. Two different consumers can process them in parallel, and a "paid" event can be processed before "created".
kafka-topics.sh --alter --topic orders --partitions 6 \
--bootstrap-server localhost:9092
This command runs in a second and gives no warning at all, which is what makes it dangerous.
What you can do:
- create the topic with enough partitions from the start (taking the next 1-2 years of growth into account);
- if you must increase, stop the producers, wait until the lag on the old partitions drops to zero, then increase;
- or create a new topic and migrate.
For topics without keys this isn't a problem; increase freely. Decreasing is not possible at all.
Sign up to mark studied questions and save your progress.