What does `kafka-topics.sh --alter --partitions` allow you to do, and what is the key directionality constraint?
answer
- --alter --partitions = increase only
- no in-place shrink ever
- createPartitions admin API
- shrink = new topic + republish
- increase still breaks key routing
basics
~10 sIt can only INCREASE the partition count of an existing topic, never decrease it. To shrink, you must create a new topic with fewer partitions and republish the data; Kafka has no in-place shrink.
solid answer
~40 s`kafka-topics.sh --alter --topic <t> --partitions <N>` changes a topic's partition count, but only upward: N must be greater than the current count. Kafka rejects any attempt to lower it because shrinking would require deciding what to do with the data in the partitions being removed and would break offset/log semantics. There is no `--partitions` value below current that the broker will accept. If you truly need fewer partitions, the only path is to create a brand-new topic with the desired count and republish (mirror) the data into it, then cut consumers over. Also be aware that increasing partitions changes key-to-partition routing for new records, so keyed ordering guarantees are disrupted from the moment of the increase.
go deeper
Memorize: --alter --partitions only goes up, never down.
Explain WHY shrink is impossible (offsets, independent logs) and the republish workaround.
Connect the increase to broken key routing and the operational steps to repartition safely.
Frame partition count as a hard-to-reverse architectural decision and design topics with headroom and key-aware sizing up front.
## What the command does ``` kafka-topics.sh --bootstrap-server localhost:9092 \ --alter --topic orders --partitions 12 ``` This tells the broker to expand topic `orders` to 12 partitions. The admin API behind it is `AdminClient.createPartitions(...)`. ## Increase-only constraint Kafka **only supports increasing** the partition count. If the topic currently has 12 partitions and you pass `--partitions 6`, the broker rejects it with an error (`InvalidPartitionsException` / "Topic currently has N partitions, which is higher than the requested N"). The same value or a lower value is invalid; the new count must be strictly greater. ### Why no shrink? - Each partition is an independent log with its own offsets and committed consumer positions. Removing a partition would orphan its data and its committed offsets. - Producers and consumers cache partition metadata; silently dropping partitions would create correctness hazards. - The log files, segments, and replicas for a partition are physical; collapsing them into others has no well-defined, safe semantics. So Kafka simply forbids it rather than offering a lossy operation. ## The real cost of increasing Increasing is allowed but not free of consequences: - **Key routing changes.** The default partitioner maps a key by `hash(key) % numPartitions`. When `numPartitions` changes, the same key can now map to a different partition. Records for a key written before and after the change can live in different partitions, breaking per-key ordering and any consumer assuming a key always lands in one partition. - **No rebalancing of existing data.** Old records stay where they were; only new writes use the new partition count. ## Shrinking: republish pattern The supported way to reduce partitions is **repartition-by-republish**: 1. Create a new topic with the target (smaller) partition count. 2. Run a consumer/producer (or MirrorMaker/Streams/Connect) that reads the old topic and writes to the new one. 3. Migrate consumers, then producers, to the new topic. 4. Retire the old topic. This is also how you'd change the *partitioning key* or fix a too-high partition count.
- You ran --alter to go from 6 to 6 partitions by mistake. What happens?It is rejected — the new count must be strictly greater than the current count. Equal or lower values are invalid (InvalidPartitionsException).
- Your team needs to go from 50 partitions down to 10 because per-partition overhead is hurting the cluster. What's the procedure?Create a new topic with 10 partitions and republish all data into it (consumer→producer bridge, MirrorMaker, or Streams), migrate consumers and producers over, then delete the old 50-partition topic. There is no in-place shrink.
saying these in an interview costs you the question
- Believing `--alter` can lower the partition count.
- Thinking increasing partitions redistributes existing records across the new partitions.
- Assuming an increase preserves key-to-partition mapping.