Kafka Replication Strategies
Replication in Apache Kafka is crucial for fault tolerance and high availability. Here's an overview of key concepts:
- Replication Factor: Defines the number of replicas of each partition.
- Leader and Followers: One replica is the leader, and others are followers.
- Replication Strategy: Includes rack awareness and preferred leader election.
Code Example
1. Create a Kafka Topic with a Replication Factor Using Command-Line Tool
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-topic
2. Create a Kafka Topic Using Java Admin Client
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import java.util.Collections;
import java.util.Properties;
public class KafkaTopicCreator {
public static void main(String[] args) {
Properties props = new Properties();
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
try (AdminClient adminClient = AdminClient.create(props)) {
NewTopic newTopic = new NewTopic("my-topic", 1, (short) 3);
adminClient.createTopics(Collections.singleton(newTopic)).all().get();
System.out.println("Topic created successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}