On this page
Kafka Improvement Proposal
KIP-101: leader epochs decide where a replica truncates
KIP-101 changed how a Kafka replica cleans up its log after a leader change. Instead of cutting its log back to its own high watermark, a returning replica asks the new leader where their histories diverge, using leader epochs, and removes only the records past that point. That fixed cases where Kafka lost committed records or ended up with replicas holding different data.
- Proposal
- KIP-101, Alter Replication Protocol to use Leader Epoch rather than High Watermark for Truncation
- Status
- Shipped Since Kafka 0.11 (2017), completed by KIP-279 in Kafka 2.0.
- Area
- Cluster and operations
- Proposal page
- KIP-101 on the Apache Kafka wiki
Why it was needed
A follower learns the high watermark one round after the leader, so its own value is often slightly behind. Truncating to it on restart could throw away committed records; if the leader then failed too, those records were gone. In other sequences two replicas ended up with different records at the same offset.
What it changes
Every batch carries the epoch of the leader that wrote it, and every replica keeps a small file mapping each epoch to its start offset. A replica coming back sends OffsetsForLeaderEpoch for its latest epoch, the leader answers with where that epoch ended in its own log, and the replica truncates exactly there. KIP-279 (Kafka 2.0) extended the exchange so replicas walk back until they find an epoch they share.
Where it stands
Shipped in Kafka 0.11 and completed by KIP-279 in Kafka 2.0. It is the basis of truncation in every current Kafka version and needs no configuration.
What comes next
Stable. Eligible Leader Replicas (KIP-966) builds on the same guarantees to elect safe leaders when the ISR is empty.
See it in the simulator
- Kafka log truncation when an old leader returns (Returning replica and log truncation) Step 6: Return → log truncation Open at this step: Kafka log truncation when an old leader returns
- Kafka log truncation when an old leader returns (Returning replica and log truncation) Step 7: Rejoined, logs consistent Open at this step: Kafka log truncation when an old leader returns
Sources
- KIP-101: Alter Replication Protocol to use Leader Epoch rather than High Watermark for Truncation
- KIP-279: Fix log divergence between leader and follower after fast leader fail over