Scrivio Interview library
InternalsIntermediate6 min read · updated 13 November 2026

A follower restarts after a crash and must decide whether the end of its log matches the new leader's log.

Explain how leader epochs and the leader-epoch-checkpoint file prevent the lost or divergent records that truncating to the high watermark could cause.

Every leadership change starts a new leader epoch, every batch is stamped with the epoch of the leader that wrote it, and each replica records the first offset of each epoch in its leader-epoch-checkpoint file. A restarting follower asks the leader where its own latest epoch ended (OffsetsForLeaderEpoch, or divergence information in fetch responses in newer versions) and truncates only beyond that point. The old rule of truncating to the follower's own high watermark, which lags the leader's, could lose committed records or leave replicas holding different records at the same offset after quick successive failures.

Why this question is asked

This is one of the deepest questions about Kafka replication, and it explains why a replica's log can be trusted after a crash. The high watermark looks like a natural truncation point, and Kafka used it for years, until real-world failure sequences showed it could lose acknowledged data or make replicas silently disagree. Interviewers ask it to see whether we understand replication beyond the happy path. In this answer, we will recap the problem a restarting follower has, walk through the two failures the old approach caused, see how leader epochs are recorded and used, and see how clients use epochs too.

1A Quick Recap of the Follower's Problem

A follower copies the leader's log by fetching. When it restarts after a crash, its log may end with records that the current leader does not have, for example records it fetched from a previous leader that was later replaced. Before it fetches anything new, it must cut off any such tail, because a follower must hold exactly the leader's records at each offset.

The question is where to cut. Cut too much and we throw away records that are committed and might be needed if this follower becomes leader. Cut too little and we keep records that differ from the leader's, so two replicas have different data at the same offset.

2Why the High Watermark Was the Wrong Cut

Before leader epochs, a restarting follower truncated its log to its own high watermark and fetched from there. The trouble is that a follower learns the high watermark from the leader one fetch later, so its own high watermark usually lags the real one. Two failure sequences, both seen in practice, show the damage.

Data loss: replica A leads and B follows, both have offset 0. A receives m1 at offset 1, B fetches it, and A advances its high watermark to 2 and acknowledges the producer. Before B fetches again and learns the new high watermark, B restarts; it truncates to its own high watermark, 1, and loses m1. Now A fails, B is elected, and B does not have m1. When A returns it follows B and truncates m1 too. An acknowledged record is gone.

Divergence: A leads and writes m1 at offset 1. B fetches it, so A's high watermark moves to 2, but B's copy is still only in its page cache. Both lose power, and B's unflushed m1 is lost. B starts first and becomes leader, then receives m2 and writes it at offset 1. A comes back, its stored high watermark says 2, so it keeps its offset 1, which is m1. Now A has m1 and B has m2 at the same offset, and nothing will ever fix it.

The divergence case, step by step
StepReplica AReplica B
A leads, writes m1 at offset 10: m0, 1: m10: m0, 1: m1 (not flushed)
Power cut, B restarts first, leadsdown0: m0 (m1 lost from page cache)
B writes m2 at offset 1down0: m0, 1: m2
A restarts, keeps up to its old HW0: m0, 1: m10: m0, 1: m2
With epochs, A truncates first0: m0, then fetches 1: m20: m0, 1: m2
Truncating to the high watermark leaves offset 1 meaning two different records.

3How Leader Epochs Are Recorded

KIP-101 introduced the leader epoch. It is a number that the controller increments every time a partition gets a new leader. The leader stamps every batch it appends with its current epoch in the batch header's partitionLeaderEpoch field, and every replica keeps a small file, leader-epoch-checkpoint, that records the first offset written in each epoch.

Shell
$ cat /var/kafka/data/orders-3/leader-epoch-checkpoint
0 # version
2 # entries
4 0 # epoch 4 started at offset 0 (A leader)
5 1 # epoch 5 started at offset 1 (B leader, wrote m2)
# A's own file before reconciling
4 0 # A only knows epoch 4, and wrote m1 at offset 1 in it

Notice what this gives us: a short history of who led the partition from which offset. Two replicas that agree on that history agree on their logs up to the point where their histories split.

4How a Restarting Follower Uses the Epochs

Instead of trusting its own high watermark, the restarting follower asks the leader a precise question: 'my latest epoch is 4; where does epoch 4 end in your log?' This is the OffsetsForLeaderEpoch request. The leader looks in its own epoch history: epoch 4 ended where epoch 5 started, at offset 1. The follower truncates everything from offset 1, removing m1, and fetches m2 from the leader. The divergence is gone.

In the data loss case, B restarting asks A where epoch 4, the epoch B knows, ends; A is still in epoch 4 and answers with its log end offset, 2. B keeps m1 instead of truncating it. If A then fails, B becomes leader with m1 intact. In newer versions the same idea is built into fetching: the follower sends its last fetched epoch with each fetch, and if the leader sees a mismatch, it returns diverging epoch information in the fetch response so the follower truncates without a separate request.

Step throughReconciling a restarted follower
Follower ALeader B
read checkpoint: latest epoch 4
OffsetsForLeaderEpoch(epoch 4)
epoch 4 ended where epoch 5 began: offset 1
end offset 1
truncate from offset 1 (drop m1)
fetch from offset 1
m2, epoch 5

7 messages in order. Play them, or step through them one at a time.

The follower cuts exactly where its history diverges from the leader's.

5Why It Matters for Consumers and Clients

With epoch-based truncation, once replicas have reconciled they never hold different records at the same offset. That is what makes a consumer's committed offset meaningful across failovers: offset 1 means the same record on whichever replica becomes leader. Without it, a consumer could resume at an offset that now points to a different record and never notice.

Clients use epochs too. Consumers and followers include the current leader epoch they know in fetch requests. If a broker receives a fetch for an epoch older than its own, it answers FENCED_LEADER_EPOCH, telling the client its metadata is stale; if it receives a newer epoch than it knows, it answers UNKNOWN_LEADER_EPOCH, meaning the broker itself is behind. Either way, a stale leader or follower is detected instead of serving data from an outdated view.

6Follow-up questions

Does the leader epoch fix every kind of data loss?

No. It fixes losses caused by wrong truncation during normal leader changes. It cannot protect records that only lived on replicas that all failed, records acknowledged with acks=1 before replication, or records discarded by an unclean leader election, which knowingly elects a replica that is missing data.

What happens to the consumer's position after truncation?

Consumers only ever read below the high watermark, so they never saw truncated records in a clean election. After an unclean election a consumer's position can be beyond the new leader's log end; the consumer can detect this through the epoch it stored with its committed offset and reset or truncate its position accordingly.

Where does the epoch live in a record batch?

In the partitionLeaderEpoch field of the v2 batch header, set by the leader when it appends the batch. That is how any replica can rebuild its epoch history by scanning its log if the checkpoint file is lost.

7Conclusion

In this answer, we saw that truncating a restarting follower to its own high watermark, which lags the leader's, could lose acknowledged records or leave replicas with different records at the same offset after quick failures. Leader epochs fix this: each leadership change gets a new epoch, batches carry it, leader-epoch-checkpoint records where each epoch began, and a follower asks the leader where its latest epoch ended and truncates only past that point. Reconciled replicas then always agree offset by offset, and clients use epochs to detect stale leaders.

What to say in the interviewAbout 90 seconds aloud

Open with

The problem is where a restarting follower should cut its log. The old rule was to truncate to its own high watermark, but a follower learns the high watermark one fetch late, so its value lags.

Then, if they want more

That caused two real bugs. A follower could truncate a record that was already committed and acknowledged, then get elected leader without it, so the data was lost.

Or, after both replicas crashed, one could come back as leader and write a new record at an offset where the other still had an old record, and the two replicas would disagree forever. Leader epochs fix this.

Every leadership change increments the partition's leader epoch, the leader stamps each batch with its epoch, and every replica keeps leader-epoch-checkpoint, which records the start offset of each epoch.

When a follower restarts, it asks the leader where its own latest epoch ended, using OffsetsForLeaderEpoch, or in newer versions the leader reports divergence in fetch responses, and the follower truncates only after that point.

That way it keeps everything the leader also has and removes exactly what diverged, so reconciled replicas never hold different records at the same offset, which keeps consumer offsets meaningful. Clients also send their leader epoch, so a stale broker answers FENCED_LEADER_EPOCH instead of serving wrong data.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain how leader epochs and the leader-epoch-checkpoint file prevent the lost or...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/4edb677077/