How to change the number of replicas of a Kafka topic? - Big Data In Real World

How to change the number of replicas of a Kafka topic?

How to calculate median in Hive?
August 27, 2021
How to fix Incompatible clusterIDs error during DataNode startup?
September 1, 2021
How to calculate median in Hive?
August 27, 2021
How to fix Incompatible clusterIDs error during DataNode startup?
September 1, 2021

Changing the number of replicas on an existing topic is a 3 step process.

  1. Get the current information about the topic
  2. Configure new replica assignment in a JSON file
  3. Execute kafka-reassign-partitions to reassign/change replicas of partitions

Get the current information about the topic

Here is the describe command to get the information about the topic named sales

kafka-topics --zookeeper <zookeeper-host>:2181 --topic sales --describe

Topic:sales PartitionCount:3 ReplicationFactor:1 Configs:retention.ms=1000000000 Topic: sales Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: sales Partition: 1 Leader: 1 Replicas: 1 Isr: 1
Topic: sales Partition: 2 Leader: 2 Replicas: 2 Isr: 2

The output describes that sales has 3 partitions with a replication factor of 1. Partition 0 is stored in broker 0, partition 1 is stored in broker 1 and so on. ISR lists the brokers where the replicas are in sync. Since replication factor is one for this topic we only see the broker where the partition is stored.

Configure new replica assignment in a JSON file

In this example we are increasing the replication factor of the partitions of the sales topic to 3. 

In the below JSON file we don’t specify the new replication factor, instead we specify partition 0 will be stored in broker 0, 1 and 2 there by increasing the replication factor to 3.

{
  "version":1, 
  "partitions":[ 
    {"topic":"sales","partition":0,"replicas":[0,1,2]}, {"topic":"sales","partition":1,"replicas":[0,1,2]}, {"topic":"sales","partition":2,"replicas":[0,1,2]} 
  ]
}

Save the JSON file as increase-replication-factor.json

Execute kafka-reassign-partitions to reassign/change replicas of partitions

Use the kafka-reassign-partitions command passing in the increase-replication-factor.json as input. Note that the name of the topic and new partition setup is in the JSON file.

kafka-reassign-partitions --zookeeper <zookeeper-host>:2181 --increase-replication-factor.json --execute

Once done, the replication factor will be changed and you can run the below command to describe the sales topic to confirm the new settings.

kafka-topics --zookeeper <zookeeper-host>:2181 --topic sales --describe

 

Big Data In Real World
Big Data In Real World
We are a group of Big Data engineers who are passionate about Big Data and related Big Data technologies. We have designed, developed, deployed and maintained Big Data applications ranging from batch to real time streaming big data platforms. We have seen a wide range of real world big data problems, implemented some innovative and complex (or simple, depending on how you look at it) solutions.

Comments are closed.

How to change the number of replicas of a Kafka topic?
This website uses cookies to improve your experience. By using this website you agree to our Data Protection Policy.

Hadoop In Real World is now Big Data In Real World!

X