Replication in Apache Kafka
Replication is how Apache Kafka keeps data available and durable when brokers fail. Each partition is copied to several brokers, so if one broker stops, another one that has the same data takes over and producers and consumers continue.
This tutorial explains how replicas are organised, how followers copy data from the leader, what the high watermark is, how to choose a replication factor and min.insync.replicas, and how to check replication health. The In-Sync Replicas tutorial covers the ISR list itself in more detail.
1. What Is Replication in Kafka?
Replication is configured per topic with a replication factor: the number of copies of each partition. With a replication factor of 3, every partition has one leader replica and two follower replicas, each on a different broker.
2. Why Replication Is Needed
- Broker failures: disks fail and servers restart. Without replicas, a partition on a failed broker would be unavailable or lost.
- Maintenance: brokers can be upgraded one at a time while their partitions are served by other replicas.
- Durability guarantees: combined with
acks=allandmin.insync.replicas, a write is acknowledged only after several brokers have it.
3. How Replication Works
3.1 Leaders and followers
Producers always write to the partition leader. Followers act like consumers: they send fetch requests to the leader and append what they receive to their own logs. Consumers read from the leader by default; since Kafka 2.4 they can also read from a nearby follower when client.rack and replica.selector.class are configured.
3.2 In-sync replicas
A follower is in sync if it has caught up with the leader within replica.lag.time.max.ms (30 seconds by default). The leader plus all in-sync followers form the ISR. A follower that falls behind is removed from the ISR and added back when it catches up.
3.3 High watermark
The high watermark is the offset up to which every ISR member has the data. Records below it are "committed" in the replication sense and visible to consumers. This prevents a consumer from reading a record that could disappear if the leader failed before the followers copied it.
3.4 Replica placement
When a topic is created, Kafka spreads replicas across brokers. If brokers set broker.rack (for example to an availability zone), replicas are placed in different racks so that losing a whole zone does not lose a partition.
4. Configuration
| Setting | Default (Kafka 4.4) | Recommendation |
|---|---|---|
default.replication.factor (broker) | 1 | 3 for production clusters |
| Topic replication factor | from --replication-factor or the default | 3 |
min.insync.replicas | 1 | 2 with replication factor 3 |
replica.lag.time.max.ms | 30000 | Keep the default unless followers are often slow |
unclean.leader.election.enable | false | Keep false unless availability matters more than data |
broker.rack | none | Set to the availability zone |
With replication factor 3, min.insync.replicas=2 and producer acks=all, the cluster tolerates one broker failure without data loss or rejected writes.
5. Java Example: Checking Replicas with the Admin API
This program creates a replicated topic and prints the leader, replicas and ISR of each partition, the same information shown by kafka-topics.sh --describe.
package com.javabytechie.kafka;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.admin.TopicDescription;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.TopicPartitionInfo;
public class ReplicaInspector {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
try (Admin admin = Admin.create(props)) {
NewTopic topic = new NewTopic("ledger", 3, (short) 3)
.configs(Map.of("min.insync.replicas", "2"));
admin.createTopics(List.of(topic)).all().get();
TopicDescription description = admin.describeTopics(List.of("ledger"))
.allTopicNames().get().get("ledger");
for (TopicPartitionInfo p : description.partitions()) {
System.out.printf("partition %d leader %d replicas %s isr %s%n",
p.partition(), p.leader().id(), ids(p.replicas()), ids(p.isr()));
}
}
}
static String ids(List<Node> nodes) {
return nodes.stream().map(n -> String.valueOf(n.id())).collect(Collectors.joining(",", "[", "]"));
}
}
Output (a local Kafka 4.4 cluster with brokers 0, 1 and 2):
partition 0 leader 1 replicas [1,2,0] isr [1,2,0]
partition 1 leader 2 replicas [2,0,1] isr [2,0,1]
partition 2 leader 0 replicas [0,1,2] isr [0,1,2]
Each partition has three replicas on three different brokers, and each broker leads one partition, which spreads the write load. The first broker in each replica list is the preferred leader. All replicas are in the ISR because the cluster is healthy. Exact broker numbers can differ on your cluster.
5.1 Command-line checks
# describe a topic
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic ledger
# list partitions whose ISR is smaller than the replica list
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitions
# list partitions whose ISR is below min.insync.replicas
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-min-isr-partitions
6. Real-World Scenarios
- Financial ledgers: replication factor 3 with
min.insync.replicas=2so that an acknowledged payment survives a broker loss. - Multi-zone clusters:
broker.rackset per availability zone puts the three replicas in three zones. - Development clusters: replication factor 1 on a single broker is fine for local testing, but never for production data.
7. Advantages and Limitations
| Advantages | Limitations |
|---|---|
| Survives broker and disk failures | Storage and network cost multiply by the replication factor |
| Rolling upgrades without downtime | acks=all latency depends on the slowest ISR member |
| Configurable durability per topic | Replication is within one cluster; other regions need MirrorMaker 2 or similar |
8. Common Mistakes
- Replication factor 3 with
min.insync.replicas=3: any single broker restart blocks allacks=allwrites. - Leaving
default.replication.factor=1so that automatically created topics have no copies. - Ignoring under-replicated partitions, which means the cluster has less protection than you think.
- Treating replication as a backup: a deleted topic or a bad record is replicated too.
9. Best Practices
- Use replication factor 3 and
min.insync.replicas=2for important topics. - Set
broker.rackso replicas are spread across failure domains. - Alert on
UnderReplicatedPartitionsandUnderMinIsrPartitionCountbroker metrics. - Keep internal topics such as
__consumer_offsetsand__transaction_stateat replication factor 3.
10. Conclusion
Kafka replication copies each partition to several brokers, with one leader serving writes and followers that fetch from it. The ISR and the high watermark ensure that consumers only see data that every in-sync replica has. Choose replication factor 3 with min.insync.replicas=2 for production, and see Leader Election in Apache Kafka for what happens when a leader fails.
11. References
Kafka Replication Interview Questions and Answers
Common interview questions on Kafka Replication, from basic to scenario-based, with short answers you can explain in your own words.
Basic questions
Q. What does a replication factor of 3 mean?
Answer: Each partition is stored on three different brokers: one leader and two followers. The topic can lose up to two brokers holding a partition without losing that partition's data, as long as one up-to-date replica remains.
Q. Can the replication factor be larger than the number of brokers?
Answer: No. Each replica of a partition must be on a different broker, so topic creation fails with InvalidReplicationFactorException if the replication factor exceeds the number of available brokers.
Intermediate questions
Q. Can consumers read from follower replicas?
Answer: Yes, since Kafka 2.4 (KIP-392). Brokers set replica.selector.class=org.apache.kafka.common.replica.RackAwareReplicaSelector and broker.rack, and consumers set client.rack to their zone. The leader then tells the consumer to fetch from an in-sync follower in the same rack, which cuts cross-zone network cost. Followers serve only data up to the high watermark, and writes still always go to the leader; reads may see new records slightly later than from the leader.
Q. How do followers replicate data from the leader?
Answer: Followers pull. They send fetch requests to the leader, just like consumers, and append the returned records to their own logs. The offsets in their fetch requests also tell the leader how far each follower has replicated, which the leader uses to maintain the ISR and advance the high watermark.
Tricky and scenario questions
Q. How do you increase the replication factor of an existing topic?
Answer: There is no --alter option for the replication factor. Write a reassignment JSON file listing the desired replicas for each partition and run kafka-reassign-partitions.sh --execute, then check with --verify. Use a throttle (--throttle) for large topics so the extra copying does not saturate the network.
Q. Why is a replication factor of 2 rarely recommended for important topics?
Answer: With two replicas you must choose between availability and safety. With min.insync.replicas=2, every broker restart or failure stops acks=all writes for the affected partitions. With min.insync.replicas=1, writes continue on a single copy, so losing that broker before the follower catches up can lose acknowledged data. Replication factor 3 with min.insync.replicas=2 tolerates one broker down and still keeps two copies of every acknowledged record.