00:00

Replication in Apache Kafka

Last updated:

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.

Definition: Kafka replication keeps identical copies of each partition's log on several brokers. One replica is the leader, which serves writes and (by default) reads; the followers copy the leader's log.

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=all and min.insync.replicas, a write is acknowledged only after several brokers have it.

3. How Replication Works

Replication of one partition across three brokersThe producer writes to the leader of partition 0 on broker 1, which has offsets 0 to 5. Followers on broker 2 and broker 3 fetch from the leader. Broker 2 has offsets 0 to 5, broker 3 has 0 to 3. The high watermark is 4, the lowest offset not yet present on every in-sync replica, so consumers can read offsets 0 to 3. producer consumer broker 1: leader P0 [0][1][2][3][4][5]log end offset 6 broker 2: follower [0][1][2][3][4][5]caught up broker 3: follower [0][1][2][3]still fetching 4 and 5 write fetch fetch high watermark = 4offsets 0–3 are on all ISR members reads 0–3
Followers pull from the leader. Consumers can read only up to the high watermark, which every in-sync replica has reached.

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

SettingDefault (Kafka 4.4)Recommendation
default.replication.factor (broker)13 for production clusters
Topic replication factorfrom --replication-factor or the default3
min.insync.replicas12 with replication factor 3
replica.lag.time.max.ms30000Keep the default unless followers are often slow
unclean.leader.election.enablefalseKeep false unless availability matters more than data
broker.racknoneSet 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.

ReplicaInspector.java
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):

Output
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

Terminal
# 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=2 so that an acknowledged payment survives a broker loss.
  • Multi-zone clusters: broker.rack set 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

AdvantagesLimitations
Survives broker and disk failuresStorage and network cost multiply by the replication factor
Rolling upgrades without downtimeacks=all latency depends on the slowest ISR member
Configurable durability per topicReplication 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 all acks=all writes.
  • Leaving default.replication.factor=1 so 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=2 for important topics.
  • Set broker.rack so replicas are spread across failure domains.
  • Alert on UnderReplicatedPartitions and UnderMinIsrPartitionCount broker metrics.
  • Keep internal topics such as __consumer_offsets and __transaction_state at 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