Lesson 08 of 17 · How Kafka works
Replication and durability
How Kafka keeps data safe when servers fail: leaders and followers, the in-sync replica set, the high watermark, and why replication factor 3 with min.insync.replicas=2 and acks=all is the standard. A hands-on 3-broker KRaft lab stops brokers one by one and shows ISR shrinking, writes continuing, then being refused with NOT_ENOUGH_REPLICAS, and recovery. Also unclean leader election, preferred leaders, racks and the controller quorum.
Leaders, followers and the ISR
Every partition has a replication factor (RF): the number of brokers holding a copy. One copy is the leader; the others are followers:
- Producers write to the leader; consumers normally read from it too.
- Followers continuously fetch from the leader to stay up to date.
- The ISR (in-sync replicas) is the set of replicas that are fully caught up. A follower that falls more than
replica.lag.time.max.ms(30 s) behind is removed from the ISR, and added back when it catches up.
payments, partition 0 RF = 3
broker 1: [0][1][2][3][4][5] leader
broker 2: [0][1][2][3][4][5] follower, in ISR
broker 3: [0][1][2][3] follower, behind → out of ISR
ISR = {1, 2}
The high watermark
The high watermark is the highest offset that every in-sync replica has. Consumers can only read up to the high watermark. That way, a consumer never sees a record that could vanish if the leader died and a follower without it took over.
min.insync.replicas and acks
With acks=all, the leader replies only when every replica in the ISR has the record. min.insync.replicas sets the minimum ISR size for a write to be accepted at all:
| RF | min.insync.replicas | Brokers that can fail and writes continue | Brokers that can fail without losing acknowledged data |
|---|---|---|---|
| 3 | 1 | 2 | 0 (one copy may be all you had) |
| 3 | 2 | 1 | 1 |
| 3 | 3 | 0 | 2, but any failure stops writes |
RF 3 + min.insync.replicas=2 + acks=all is the standard: a broker can fail (or be restarted for an upgrade) with no data loss and no write outage.
Your homework is so important that the teacher wants three copies in three different lockers. The rule: you may only say "done" when at least two lockers have an up-to-date copy. If one locker breaks, you still have two, and you can keep working. If two break, the teacher says "stop, it isn't safe to hand in" rather than pretending one copy is enough.
Lab: break a 3-broker cluster
Start three combined broker-and-controller nodes on one Docker network. This is a learning setup, not a production layout:
$ docker network create kafka-net
$ CID=$(docker run --rm apache/kafka:4.1.0 /opt/kafka/bin/kafka-storage.sh random-uuid)
$ for i in 1 2 3; do docker run -d --name kafka-$i --network kafka-net \
-e KAFKA_NODE_ID=$i -e KAFKA_PROCESS_ROLES=broker,controller \
-e KAFKA_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka-$i:9092 \
-e KAFKA_CONTROLLER_LISTENER_NAMES=CONTROLLER \
-e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT \
-e KAFKA_CONTROLLER_QUORUM_VOTERS=1@kafka-1:9093,2@kafka-2:9093,3@kafka-3:9093 \
-e KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT -e CLUSTER_ID=$CID \
-e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=3 -e KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR=3 \
-e KAFKA_TRANSACTION_STATE_LOG_MIN_ISR=2 -e KAFKA_DEFAULT_REPLICATION_FACTOR=3 -e KAFKA_MIN_INSYNC_REPLICAS=2 \
apache/kafka:4.1.0; done
Check the controller quorum, then create a replicated topic:
$ docker exec kafka-1 /opt/kafka/bin/kafka-metadata-quorum.sh --bootstrap-server kafka-1:9092 describe --status
ClusterId: hxP6Iz0nQAKguG11rGvdZQ
LeaderId: 1
LeaderEpoch: 1
HighWatermark: 45
MaxFollowerLag: 0
MaxFollowerLagTimeMs: 0
CurrentVoters: [{"id": 1, "endpoints": ["CONTROLLER://kafka-1:9093"]}, {"id": 2, "endpoints": ["CONTROLLER://kafka-2:9093"]}, {"id": 3, "endpoints": ["CONTROLLER://kafka-3:9093"]}]
CurrentObservers: []
$ docker exec kafka-1 /opt/kafka/bin/kafka-topics.sh --bootstrap-server kafka-1:9092 --create --topic payments \
--partitions 3 --replication-factor 3 --config min.insync.replicas=2
$ docker exec kafka-1 /opt/kafka/bin/kafka-topics.sh --bootstrap-server kafka-1:9092 --describe --topic payments
Topic: payments TopicId: XUzaircATbqoFDf-_Lm4OA PartitionCount: 3 ReplicationFactor: 3 Configs: min.insync.replicas=2
Topic: payments Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3 Elr: LastKnownElr:
Topic: payments Partition: 1 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1 Elr: LastKnownElr:
Topic: payments Partition: 2 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2 Elr: LastKnownElr:
Leadership is spread: each broker leads one partition.
Stop one broker. Partition 2 loses its leader; broker 1 takes over, and every ISR shrinks to two:
$ docker stop kafka-3
$ docker exec kafka-1 /opt/kafka/bin/kafka-topics.sh --bootstrap-server kafka-1:9092 --describe --topic payments
Topic: payments Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2 Elr: LastKnownElr:
Topic: payments Partition: 1 Leader: 2 Replicas: 2,3,1 Isr: 2,1 Elr: LastKnownElr:
Topic: payments Partition: 2 Leader: 1 Replicas: 3,1,2 Isr: 1,2 Elr: LastKnownElr:
$ echo "p1:ok" | docker exec -i kafka-1 /opt/kafka/bin/kafka-console-producer.sh --bootstrap-server kafka-1:9092 \
--topic payments --producer-property acks=all --property parse.key=true --property key.separator=:
The write succeeds: two in-sync copies meet min.insync.replicas=2. --describe --under-replicated-partitions now lists all three partitions.
Stop a second broker. Only one copy is left:
$ docker stop kafka-2
$ echo "p2:x" | docker exec -i kafka-1 /opt/kafka/bin/kafka-console-producer.sh --bootstrap-server kafka-1:9092 \
--topic payments --producer-property acks=all --producer-property delivery.timeout.ms=8000 \
--producer-property request.timeout.ms=4000 --property parse.key=true --property key.separator=:
… NOT_ENOUGH_REPLICAS …
org.apache.kafka.common.errors.NotEnoughReplicasException: Messages are rejected since there are fewer in-sync replicas than required.
Kafka refuses the write rather than accept it with a single copy. (With two of the three controllers down, the KRaft quorum has also lost its majority, so no metadata changes are possible either; the Elr column you may see lists replicas still eligible to become leader safely.)
Bring both back. They catch up and rejoin the ISR:
$ docker start kafka-2 kafka-3
$ docker exec kafka-1 /opt/kafka/bin/kafka-topics.sh --bootstrap-server kafka-1:9092 --describe --topic payments
Topic: payments Partition: 0 Leader: 1 Replicas: 1,2,3 Isr: 1,2,3 Elr: LastKnownElr:
Topic: payments Partition: 1 Leader: 1 Replicas: 2,3,1 Isr: 1,2,3 Elr: LastKnownElr:
Topic: payments Partition: 2 Leader: 1 Replicas: 3,1,2 Isr: 1,2,3 Elr: LastKnownElr:
All data is safe, but broker 1 now leads every partition: it does all the work. The first replica in the list is the preferred leader. Kafka moves leadership back automatically every few minutes (auto.leader.rebalance.enable), or you can do it now:
$ docker exec kafka-1 /opt/kafka/bin/kafka-leader-election.sh --bootstrap-server kafka-1:9092 \
--election-type PREFERRED --all-topic-partitions
Successfully completed leader election (PREFERRED) for partitions payments-2, payments-1
Clean up with docker rm -f kafka-1 kafka-2 kafka-3 && docker network rm kafka-net.
Unclean leader election
If every in-sync replica is gone and only an out-of-date replica survives, Kafka can either wait (the partition is unavailable) or make the stale replica leader (unclean election), silently losing every record it missed, including acknowledged ones. unclean.leader.election.enable=false is the default; keep it, except for data you can truly afford to lose.
Racks and zones
Three copies in one data centre zone do not survive losing the zone. Set broker.rack on each broker to its availability zone; Kafka then places a partition's replicas in different racks. Clients can also read from the nearest follower (client.rack with a rack-aware replica selector) to cut cross-zone traffic costs.
The controller quorum
The KRaft controllers need a majority to change metadata (create topics, elect leaders). Three controllers tolerate one failure; five tolerate two. In production, use dedicated controllers spread across zones, separate from the brokers that carry traffic.
Under-replicated partitions is the alert that matters
A non-empty --under-replicated-partitions list means you are one failure away from refusing writes (or worse). Alert on it, and on offline partitions (no leader at all), before anything else.
Going deeper
- Rolling restarts are safe only if the ISR is full before you stop the next broker. Operators such as Strimzi check this; scripted restarts must too.
- Replication traffic is a large share of network use: RF 3 means every byte produced is sent over the network at least twice more. Budget for it in capacity and cloud cost.
min.insync.replicascan be set per topic: 2 for important data, 1 for data where availability matters more.
Recap
- Each partition has a leader and followers; the ISR is the up-to-date set.
- Consumers read only up to the high watermark.
- RF 3, min.insync.replicas 2, acks=all survives one broker loss without data loss or write outage.
- With too few replicas, Kafka refuses writes (
NOT_ENOUGH_REPLICAS) instead of risking data. - Keep unclean election off, spread replicas across zones, and alert on under-replicated and offline partitions.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.