Quick revision
The short answer
If we simply add partitions, the default partitioner, which picks hash(key) modulo the partition count, sends three quarters of the customers to a different partition. New events for a moved customer can then be processed before older events still waiting in its old partition, so per-customer ordering breaks. To change safely we either migrate to a new 24-partition topic after draining the old one, or make the change in a quiet window when consumer lag is near zero, and we treat compacted topics and Kafka Streams state as separate migrations.
What to remember
In this answer, we saw that adding partitions changes hash(key) mod N, so going from 6 to 24 moves 75 percent of the customers, and new events for a moved customer can overtake older events still unread in the old partition. The safe paths are a new topic with a drained switch-over, or an in-place change in a quiet window with lag near zero and producers briefly paused. Compacted topics and Kafka Streams applications need their own migration because they depend on keys and co-partitioning, and choosing partition counts with headroom avoids most of this pain.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
If we simply add partitions, the default partitioner, which picks hash(key) modulo the partition count, sends three quarters of the customers to a different partition. New events for a moved customer can then be processed before older events still waiting in its old partition, so per-customer ordering breaks. To change safely we either migrate to a new 24-partition topic after draining the old one, or make the change in a quiet window when consumer lag is near zero, and we treat compacted topics and Kafka Streams state as separate migrations.
Why this question is asked
Running out of partitions is a growth problem most Kafka teams meet sooner or later. The command to add partitions takes seconds, which makes it tempting, but on a keyed topic it silently changes the routing of every key. Interviewers ask this scenario because it checks whether we understand where ordering in Kafka really comes from and whether we can plan an operational change around it. In this answer, we will recap how keys map to partitions, work out exactly what happens when 6 becomes 24, then walk through two safe ways to do it, the extra care that compacted topics and Kafka Streams need, and how to plan partitions so this happens less often.
1A Quick Recap: Where Per-Key Ordering Comes From
Kafka only guarantees order inside a partition. Ordering per customer works because every record for customer-17 has the key customer-17, the producer's default partitioner hashes the key with murmur2 and takes the result modulo the number of partitions, and so all of that customer's records land in the same partition, where one consumer reads them in order.
Notice that the guarantee depends on the partition count staying the same. The formula hash(key) mod N gives a different answer when N changes.
2What Happens When 6 Becomes 24
Because 24 is a multiple of 6, there is a neat way to see the effect. A key's new partition, hash mod 24, still gives the old partition when taken mod 6. So a customer that was in partition 2 now goes to partition 2, 8, 14 or 20, each about equally likely. Only one in four customers stays where it was; 75 percent of them move.
5 messages in order. Play them, or step through them one at a time.
Ordering within each partition is intact; ordering for the customer is not.
Now picture customer-42 with an order event still unread in partition 5, because that partition has a lag of a few thousand records. A new payment event for the same customer goes to partition 11, which is empty and read by an idle consumer, so the payment is processed first. For a few minutes the system sees a payment for an order it has never heard of. That is the ordering break, and it lasts until the old partitions are drained.
3Safe Option 1: Migrate to a New Topic
The cleanest way is to create a new topic, say customer-events-v2 with 24 partitions, and move traffic in a controlled order. First deploy consumers that can read the new topic. Then stop or pause the producers for a short moment, wait until the consumers have read everything in the old topic (lag zero on all 6 partitions), and switch the producers to the new topic. The consumers then switch as well, and since no event for any customer is left behind in the old topic, nothing can be overtaken.
The cost is a short producer pause, often a minute or less, plus some coordination and a new topic name that every client has to learn. Where producers cannot pause, they can buffer in an outbox table for the switch, and we accept that the old topic is kept for a while for replay or audit.
4Safe Option 2: Add Partitions in a Quiet Window
If a new topic is too disruptive, we can add partitions in place but choose the moment. The ordering problem only exists for events that are still in flight, unread in an old partition, when the change happens. So we pick a quiet period, check that the lag is near zero on every partition, ideally briefly pause the producers, and then run kafka-topics --alter --partitions 24. With lag at zero, the old partitions hold nothing waiting, so a moved customer's first new event cannot overtake anything.
Notice that producers pick up the new partition count from metadata within their refresh interval (metadata.max.age.ms, five minutes by default), so for a short time some producers may still use 6. A brief producer pause during the change avoids that mix.
- Choose a quiet windowlow traffic, people on hand
- Lag near zero on all partitions?YesContinueNoWait, or drain first
- Pause producers brieflyavoid old and new counts mixing
- kafka-topics --alter --partitions 24cannot be undone
- Resume producersnew events use the new mapping
- Watch ordering-sensitive checksout-of-order alerts, lag per partition
6 steps in order. Play them, or step through them one at a time.
5Compacted Topics and Kafka Streams
Two kinds of consumers need more than a quiet window. A compacted topic keeps the latest record per key. After the change, a moved key has its old value in the old partition and new values in the new one, and compaction works per partition, so both survive and a reader rebuilding state may see the stale value last. A compacted topic is best migrated to a new topic by replaying the latest value of every key.
Kafka Streams applications are stricter still. A join between two topics requires them to be co-partitioned, with the same partition count and the same partitioner, and the application's state stores and internal changelog and repartition topics were created for 6 partitions. Changing the input to 24 breaks that layout. Such an application is migrated by deploying it under a new application.id, which gives it fresh internal topics and state, reading the new topic from the start, and switching readers to its output once it has caught up.
6Planning Partitions With Headroom
Since partitions can only grow and growing is disruptive, it pays to choose the count with headroom from the start. A common rule is to estimate the throughput needed in two or three years, divide by what one consumer can handle, and round up to a number with many divisors, such as 12, 24 or 48. Partitions are not free, since each one costs broker memory, file handles and recovery time, but going from 6 to 24 later is far more expensive than starting at 24.
7Follow-up questions
How would you migrate a Kafka Streams application that joins this topic?
I would treat it as a new deployment rather than an in-place change. The other side of the join must also have 24 partitions, so either that topic is migrated too or the application repartitions it explicitly. Then I would run the new version of the application with a new application.id, so it creates new internal topics and state stores with 24 partitions, let it read both input topics from the beginning or from a retained point and build its state, compare its output with the old application for a while, and finally switch consumers of its output and retire the old application along with its internal topics.
Why not write a custom partitioner that keeps old keys in their old partitions?
It is possible in theory, but every producer in every language must use exactly the same partitioner, it needs to know which keys existed before, and the old partitions stay as hot as before, so the extra throughput never arrives for existing customers. A planned migration is simpler and gives the full benefit.
8Conclusion
In this answer, we saw that adding partitions changes hash(key) mod N, so going from 6 to 24 moves 75 percent of the customers, and new events for a moved customer can overtake older events still unread in the old partition. The safe paths are a new topic with a drained switch-over, or an in-place change in a quiet window with lag near zero and producers briefly paused. Compacted topics and Kafka Streams applications need their own migration because they depend on keys and co-partitioning, and choosing partition counts with headroom avoids most of this pain.
Open with
Kafka only orders records within a partition, and per-customer ordering works because the default partitioner computes the murmur2 hash of the key modulo the number of partitions. If we go from 6 to 24, that modulo changes.
Then, if they want more
Because 24 is a multiple of 6, a customer in old partition 2 lands in 2, 8, 14 or 20, so about three quarters of customers move. The danger is in-flight events: if a customer has events still unread in its old partition, its next event goes to a new, empty partition and can be processed first.
So I would not just run the alter command. The safest approach is a new topic with 24 partitions: deploy consumers for it, pause producers briefly, let consumers drain the old topic to zero lag, then switch producers and consumers.
If a new topic is too disruptive, I would add partitions in place during a quiet window, after confirming lag is near zero and pausing producers for a moment so they all pick up the new metadata together.
Compacted topics and Kafka Streams need separate care, because compaction is per partition and Streams joins need co-partitioned inputs and state stores built for the old count; I would redeploy Streams with a new application id. And next time I would size partitions with growth headroom from the start.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A topic keyed by customer id has 6 partitions and needs more throughput, so a team wants...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/582aa15f54/