0
0
KafkaHow-ToBeginner · 3 min read

How to Increase Partitions of Topic in Kafka

To increase partitions of a Kafka topic, use the kafka-topics.sh --alter --topic <topic-name> --partitions <new-partition-count> command. This command increases the number of partitions but cannot decrease them. Make sure the new partition count is greater than the current count.
📐

Syntax

The command to increase partitions in Kafka uses the kafka-topics.sh tool with the --alter option. You specify the topic name with --topic and the new total number of partitions with --partitions.

  • --alter: Indicates you want to change an existing topic.
  • --topic <topic-name>: The name of the topic to modify.
  • --partitions <new-count>: The new total number of partitions (must be greater than current).
bash
kafka-topics.sh --bootstrap-server <broker-list> --alter --topic <topic-name> --partitions <new-partition-count>
💻

Example

This example shows how to increase partitions of a topic named my-topic from 3 to 5 partitions using a Kafka broker running on localhost:9092.

bash
kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic my-topic --partitions 5
Output
Updated topic "my-topic".
⚠️

Common Pitfalls

  • Trying to decrease partitions will fail because Kafka does not support reducing partitions.
  • Setting the new partition count equal to or less than the current count will cause an error.
  • Not specifying the correct --bootstrap-server can lead to connection failures.
  • After increasing partitions, existing data is not redistributed automatically; new partitions start empty.
bash
## Wrong: Trying to reduce partitions
kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic my-topic --partitions 2

## Correct: Increase partitions only
kafka-topics.sh --bootstrap-server localhost:9092 --alter --topic my-topic --partitions 5
Output
Error: Number of partitions can only be increased. Updated topic "my-topic".
📊

Quick Reference

Remember these key points when increasing Kafka topic partitions:

  • Use --alter with kafka-topics.sh.
  • New partition count must be greater than current.
  • Partitions cannot be decreased.
  • Specify the correct Kafka broker with --bootstrap-server.

Key Takeaways

Use kafka-topics.sh with --alter and --partitions to increase partitions.
New partition count must be greater than the current count.
Kafka does not support decreasing partitions once set.
Specify the correct bootstrap server to connect to Kafka brokers.
After increasing partitions, data is not automatically rebalanced.