Replication, ISR and fault tolerance
- ISR
- in-sync replicas
Mid-level Frequently asked 4 min
On this page
The question
How does replication work in Kafka?
30-second answer
Each partition has a replication factor, usually 3: one copy per broker. One replica is the leader, which takes all writes and, by default, all reads. The followers fetch from the leader to stay in sync. The replicas that are caught up form the ISR, the in-sync replicas. If the leader's broker fails, the controller elects a new leader from the ISR, and clients switch to it.
Understand it
Brokers fail: leader, ISR and writes
ExploreBroker 1 leads. 3 in sync meets min.insync.replicas=2, so acks=all writes succeed on 3 copies.
View as text
A producer with acks=all above three brokers, each holding a replica of partition 0 of the topic payments, with replication factor 3. Each broker shows whether it leads, follows or is down, and whether it is in the ISR, the in-sync replicas. A line under the brokers says whether writes are accepted.
Broker 1 leads. 3 in sync meets min.insync.replicas=2, so acks=all writes succeed on 3 copies.
Broker 1 is down: the controller elects broker 2, an in-sync follower, as leader. 2 in sync meets min.insync.replicas=2, so acks=all writes succeed on 2 copies.
Brokers 1 and 2 are down: broker 3, the last in-sync replica, leads. 1 in sync is below min.insync.replicas=2: acks=all writes fail with NotEnoughReplicas. Reads still work.
Brokers 1 and 2 are down: broker 3, the last in-sync replica, leads. min.insync.replicas=1 still accepts writes, now on one disk only: lose broker 3 and they are gone.
Broker 2 is down and drops out of the ISR. Broker 1 still leads. 2 in sync is below min.insync.replicas=3: acks=all writes fail with NotEnoughReplicas. Reads still work.
A producer with acks=all above three brokers, each holding a replica of partition 0 of the topic payments, with replication factor 3. Each broker shows whether it leads, follows or is down, and whether it is in the ISR, the in-sync replicas. A line under the brokers says whether writes are accepted.
Broker 1 leads. 3 in sync meets min.insync.replicas=2, so acks=all writes succeed on 3 copies.
Broker 1 is down: the controller elects broker 2, an in-sync follower, as leader. 2 in sync meets min.insync.replicas=2, so acks=all writes succeed on 2 copies.
Brokers 1 and 2 are down: broker 3, the last in-sync replica, leads. 1 in sync is below min.insync.replicas=2: acks=all writes fail with NotEnoughReplicas. Reads still work.
Brokers 1 and 2 are down: broker 3, the last in-sync replica, leads. min.insync.replicas=1 still accepts writes, now on one disk only: lose broker 3 and they are gone.
Broker 2 is down and drops out of the ISR. Broker 1 still leads. 2 in sync is below min.insync.replicas=3: acks=all writes fail with NotEnoughReplicas. Reads still work.
Takeaway: Each partition has a leader and followers. When a broker dies, it leaves the ISR, the in-sync replicas, and the controller elects a new leader from them. acks=all writes need at least min.insync.replicas in sync.
In code
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic payments \
--partitions 6 --replication-factor 3 --config min.insync.replicas=2
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic payments
# Topic: payments Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3
# ...after broker 1 stops:
# Topic: payments Partition: 0 Leader: 2 Replicas: 1,2,3 Isr: 2,3process.roles=broker
node.id=4
controller.quorum.voters=1@ctrl-1:9093,2@ctrl-2:9093,3@ctrl-3:9093
# Replicas of a partition go to different zones
broker.rack=zone-a
default.replication.factor=3
min.insync.replicas=2
# Never elect a replica that may miss acknowledged records
unclean.leader.election.enable=false
# A follower this far behind leaves the ISR
replica.lag.time.max.ms=30000try (Admin admin = Admin.create(Map.of("bootstrap.servers", "localhost:9092"))) {
TopicDescription payments = admin.describeTopics(List.of("payments"))
.allTopicNames().get().get("payments");
for (TopicPartitionInfo p : payments.partitions()) {
System.out.printf("P%d leader %s, isr %s%n", p.partition(),
p.leader() == null ? "none" : p.leader().id(),
p.isr().stream().map(Node::id).toList());
}
}// 3 replicas, all 3 required: a rolling restart of one broker rejects every acks=all write
Wrong new NewTopic("payments", 6, (short) 3).configs(Map.of("min.insync.replicas", "3"));
// 3 replicas, 2 required: one broker can be down, and every acknowledged write is on 2 disks
Right new NewTopic("payments", 6, (short) 3).configs(Map.of("min.insync.replicas", "2"));Solve it
Design a payments topic that survives a broker loss without losing an acknowledged payment
The platform runs 3 brokers, one per availability zone. The payments team says an acknowledged payment must never be lost, and writes must keep working while one broker is patched. Choose the topic, broker and producer settings, and show how you would test them.
- Count copies. Replication factor 3 puts a copy in each zone. A copy is only a guarantee if the producer waits
for it, so producers use
acks=all. - Set the floor.
min.insync.replicas=2: with one broker down, 2 replicas are still in sync and writes continue; with two down, writes are refused rather than kept on a single disk. - Forbid unsafe elections. Keep
unclean.leader.election.enable=false, so an out-of-sync replica, which may miss acknowledged payments, never becomes leader. The cost: a partition can stay offline until an in-sync replica returns. - Spread replicas. Set
broker.rackto the zone, so the three replicas of a partition never share one. - Check. Produce steadily with
acks=alland stop one broker: no errors, nothing missing. Stop a second: sends fail with NotEnoughReplicas, and still no acknowledged record is missing after recovery.
kafka-topics.sh --bootstrap-server localhost:9092 --create --topic payments \
--partitions 12 --replication-factor 3 \
--config min.insync.replicas=2 --config unclean.leader.election.enable=false
# Producer (Spring Boot): wait for every in-sync replica; the default since Kafka 3.0
# spring.kafka.producer.acks=allHow to answer
- acks=all waits for every replica in the ISR, so a new leader from the ISR has every acknowledged record.
- Leaders are spread across brokers, so each broker leads some partitions and follows others.
- Consumers only see records up to the high watermark, the offset copied to every in-sync replica.
Common mistakes
- Saying followers serve reads and writes to share the load. Writes always go to the leader.
- Confusing replication with partitioning. Partitions split data; replicas copy it.
Follow-up questions
What is the ISR, and how does a replica fall out of it?
The ISR, in-sync replicas, is the set of replicas that are caught up with the leader, the leader included. A follower that has not caught up to the leader's log end within replica.lag.time.max.ms, 30 seconds by default, or whose broker dies, is removed. It rejoins once it catches up. The ISR decides when acks=all writes count as committed, and who may become leader.
- Replicas lists every assigned copy; ISR lists the ones caught up.
- A shrinking ISR is a key alert: under-replicated partitions.
- The controller keeps the ISR in the cluster metadata.
See it in the infographic: Brokers fail: leader, ISR and writes
What does min.insync.replicas do?
It sets how many replicas must be in sync for an acks=all write to be accepted. If fewer are, the leader rejects the write with NotEnoughReplicas, instead of acknowledging a record held by too few brokers. With replication factor 3 and min.insync.replicas=2, one broker can be down and writes continue, each on at least 2 disks. It has no effect on producers with acks=0 or 1.
- Set it below the replication factor, or one broker restart blocks writes.
- It is a topic setting with a broker default.
- It trades availability for durability when replicas are missing.
See it in the infographic: Brokers fail: leader, ISR and writes
What is KRaft, and why did Kafka replace ZooKeeper?
KRaft is Kafka's built-in metadata quorum: a few controller nodes agree, with the Raft protocol, on cluster metadata such as brokers, topics, partition leaders and ISRs, and store it as an internal log that brokers replicate. Before, an external ZooKeeper cluster held it. KRaft means one system to deploy and secure, faster controller failover, and far more partitions per cluster. Kafka 4.0 removed ZooKeeper support.
- Usually 3 or 5 controllers, so a majority survives one or two failures.
- Brokers learn metadata changes by reading the metadata log.
- Migration from ZooKeeper happens on a 3.x release before moving to 4.0.
See it in the infographic: Brokers fail: leader, ISR and writes
What is the high watermark?
The offset up to which every in-sync replica has the records. Consumers can only read below it, so a record they see cannot vanish when the leader changes. A record above it exists only on some replicas and may be truncated after a failover.
What is unclean leader election?
Electing a replica that is not in the ISR when no in-sync replica is left. The partition comes back online sooner, but acknowledged records missing on that replica are lost. It is off by default, unclean.leader.election.enable=false, which favors consistency over availability.
Check yourself
Show answers
- They fail with NotEnoughReplicas until a broker returns: Correct. Only 1 replica is in sync, below the 2 required.
- They are accepted on the remaining broker: Incorrect. That is what min.insync.replicas=1 would allow.
- The leader keeps them and acknowledges later: Incorrect. The leader rejects them; only the producer retries, until delivery.timeout.ms.
Show answers
- A replica that was in the ISR: Correct. Only in-sync replicas are sure to hold every acknowledged record.
- Any replica that is still alive: Incorrect. That is unclean leader election, off by default.
- The replica on the broker that started most recently: Incorrect. Start time plays no part; being in sync does.