The Basic Model
- Each partition has one leader and several followers on other brokers
- All writes and reads (by default) go to the leader
- Followers act like consumers: they fetch from the leader and append to their own log
- The ISR is the set of replicas (including the leader) that are caught up. A follower that lags more than
replica.lag.time.max.ms (default 30 s) is removed from the ISR
text
Partition orders-0, replication factor 3:
Leader b1: [0][1][2][3][4][5][6][7] LEO (log end offset) = 8
Follow b2: [0][1][2][3][4][5][6] LEO = 7
Follow b3: [0][1][2][3][4][5] LEO = 6
High watermark (HW) = smallest LEO among the ISR = 6
Consumers can read only up to offsets BEFORE the HW (records 0..5). Records 6 and 7 are not yet "committed".
| Term | Meaning |
| LEO (log end offset) | Offset after the last record in a replica's log |
| High watermark | Offset up to which all ISR members have the data. Consumers see only records below it |
| Committed record | A record replicated to all current ISR members |
| Leader epoch | A number that increases with each leader change, used to avoid divergence between logs after failovers |
What acks=all Really Means
With acks=all the leader waits until every replica in the current ISR has the record. The safety question is: what if the ISR has shrunk to just the leader? That is why min.insync.replicas exists.
| Setting | Value | Effect |
replication.factor | 3 | Three copies |
min.insync.replicas | 2 | A write with acks=all is rejected unless at least 2 replicas (leader included) are in the ISR |
Producer acks | all | Wait for the ISR |
text
All 3 brokers healthy: ISR={b1,b2,b3} -> write acknowledged after all 3 have it.
b3 dies: ISR={b1,b2} -> still >= 2, writes OK, durable on 2 machines.
b2 also dies: ISR={b1} -> below 2: producer gets NotEnoughReplicas, writes STOP.
Availability is sacrificed to avoid a single copy of new data.
Trade-off: replication.factor=3 with min.insync.replicas=2 survives one broker failure with no data loss and continued writes. It is the standard production choice.
Leader Failure Walk-Through
- Broker b1 (leader of orders-0) crashes
- The controller notices (heartbeat or session loss) and picks a new leader from the ISR, for example b2
- b2 becomes leader with a new leader epoch. Clients refresh metadata (they get
NOT_LEADER errors, then reconnect to b2)
- Records already acknowledged with
acks=all exist on b2, so nothing acknowledged is lost
- Records that b1 had written but never replicated were not acknowledged (with
acks=all). The producer retries them and idempotence prevents duplicates
- When b1 returns, it becomes a follower, truncates any records beyond the new leader's history (using the leader epoch) and catches up, then rejoins the ISR
Unclean Leader Election
If all ISR members are gone, the partition is offline. Kafka offers a choice:
unclean.leader.election.enable | Behaviour | Consequence |
false (default, recommended) | Wait for an ISR replica to return | Partition stays unavailable but no committed data is lost |
true | Allow an out-of-sync replica to become leader | Available again quickly but committed records can be lost and consumers may see offsets go backwards |
Choose by data type: For payments and orders keep it false. For low-value metrics where availability matters more than a few lost events, true can be acceptable on that specific topic.
Preferred Leaders and Balance
- The first replica in the replica list is the preferred leader. With
auto.leader.rebalance.enable=true (default) the controller moves leadership back to preferred leaders periodically after failures
- Spread leaders evenly so that no single broker serves most traffic
- Check with
kafka-topics.sh --describe and use kafka-leader-election.sh --election-type preferred --all-topic-partitions to rebalance manually
Rack Awareness
text
# broker config: broker.rack=us-east-1a (one value per availability zone)
# Kafka then places replicas of a partition in different racks/zones:
orders-0: leader b1 (zone a), follower b4 (zone b), follower b5 (zone c)
With 3 zones and replication factor 3, the cluster survives the loss of a whole zone. Consumers can also read from a nearby follower (client.rack with replica.selector.class=RackAwareReplicaSelector) to save cross-zone network cost.
Under-Replicated and Offline Partitions
| Metric | Meaning | Action |
UnderReplicatedPartitions > 0 | Some followers are out of the ISR (broker down, slow disk or network) | Find the broker or the replica that lags |
UnderMinIsrPartitionCount > 0 | ISR below min.insync.replicas. Producers with acks=all fail | Urgent: restore brokers |
OfflinePartitionsCount > 0 | No leader available | Urgent: bring back an ISR replica |
IsrShrinksPerSec / IsrExpandsPerSec frequent | ISR flapping | Network, GC pauses or overloaded brokers |
bash
bin/kafka-topics.sh --bootstrap-server $BS --describe --under-replicated-partitions
bin/kafka-topics.sh --bootstrap-server $BS --describe --under-min-isr-partitions
bin/kafka-topics.sh --bootstrap-server $BS --describe --unavailable-partitions
Settings Summary for No Data Loss
| Where | Setting |
| Topic | replication.factor=3, min.insync.replicas=2 |
| Broker | unclean.leader.election.enable=false, default.replication.factor=3, offsets.topic.replication.factor=3 |
| Producer | acks=all, enable.idempotence=true, retries on, handle callback errors |
| Consumer | Commit offsets after processing |
| Operations | Rack awareness, alerts on under-replicated partitions, tested broker failure drills |
Conclusion
Followers fetch from leaders, and the ISR plus the high watermark define which records are committed. With replication factor 3, min.insync.replicas=2, acks=all and no unclean elections, acknowledged records survive a broker failure.
Quick Summary: ISR + HW define committed; RF=3, minISR=2, acks=all; no unclean election; rack aware.