How to Set Replication Factor in Kafka: Simple Guide
To set the
replication.factor in Kafka, specify it when creating a topic using the --replication-factor option with the kafka-topics.sh command. This controls how many copies of each partition Kafka keeps across brokers for fault tolerance.Syntax
The replication factor is set during topic creation using the Kafka command line tool. The key parts are:
--create: to create a new topic--topic: the name of the topic--partitions: number of partitions--replication-factor: number of copies of each partition--bootstrap-server: Kafka broker address
bash
kafka-topics.sh --create --topic <topic-name> --partitions <num> --replication-factor <num> --bootstrap-server <broker-address>
Example
This example creates a topic named my-topic with 3 partitions and a replication factor of 2, meaning each partition has 2 copies on different brokers.
bash
kafka-topics.sh --create --topic my-topic --partitions 3 --replication-factor 2 --bootstrap-server localhost:9092
Output
Created topic "my-topic".
Common Pitfalls
Common mistakes when setting replication factor include:
- Setting replication factor higher than the number of brokers available, which causes topic creation to fail.
- Trying to change replication factor after topic creation without using proper reassignment tools.
- Not specifying
--replication-factorduring creation, which defaults to 1 and risks data loss if a broker fails.
bash
## Wrong: replication factor higher than brokers kafka-topics.sh --create --topic test-topic --partitions 1 --replication-factor 5 --bootstrap-server localhost:9092 ## Right: replication factor less or equal to brokers kafka-topics.sh --create --topic test-topic --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092
Output
Error: replication factor: 5 larger than available brokers: 3
Created topic "test-topic".
Quick Reference
| Option | Description |
|---|---|
| --create | Create a new Kafka topic |
| --topic | Name of the topic |
| --partitions | Number of partitions for the topic |
| --replication-factor | Number of copies of each partition |
| --bootstrap-server | Kafka broker address |
Key Takeaways
Set replication factor during topic creation with --replication-factor option.
Replication factor must not exceed the number of Kafka brokers.
Replication factor controls data durability by keeping copies on multiple brokers.
Changing replication factor after creation requires partition reassignment tools.
Default replication factor is 1 if not specified, which risks data loss.