The High Watermark, Step by Step
Linked from Truncation on Leader Election
Two Numbers Every Replica Keeps
Each replica tracks two separate numbers for each partition. Most confusion about the HW comes from mixing them up.
| Number | What it means | What it's used for |
|---|---|---|
| Log end (log end offset, LEO) | "My log goes up to here." This is how much data the replica actually has. | Deciding where to fetch from next |
| High watermark (HW) | "Everything below here is confirmed safe on every in-sync replica." | Deciding what consumers may read, and (in old Kafka) where to truncate after a crash |
A follower can have messages up to 1200 in its log and still think HW = 1100. It has the data; it just hasn't been told yet that the data is committed. A follower always fetches from its log end, never from its HW. Fetching from the HW would mean downloading messages it already has.
How the Leader Computes the HW
The ISR does not vote on the HW. The leader works it out alone, with one rule:
HW = the smallest log end across the leader and every follower in the ISR.
The leader learns each follower's log end from its fetch requests. "Give me messages from offset N" means "I already have everything before N." There is no separate ack. The follower's next fetch is the ack.
Example: one leader and two followers, all in the ISR. The leader has messages up to 1200; both followers have up to 1100.
Round 1
- Follower A fetches from 1100, so the leader knows A has up to 1100. Follower B does the same.
- The leader computes HW = smallest of (1200, 1100, 1100) = 1100.
- The leader replies to each follower with messages 1100–1200 and "HW = 1100".
- Each follower appends 1100–1200 to its log (log end is now 1200) and saves HW = 1100 in memory.
Round 2
- Follower A fetches from 1200, meaning "I now have everything up to 1200". This fetch is the ack. Follower B does the same.
- The leader recomputes HW = smallest of (1200, 1200, 1200) = 1200.
- With
acks=all, the producer now gets its "success" reply. - The leader replies "HW = 1200", and the followers update their copy. A follower's HW is the leader's HW, capped at its own log end.
Why the smallest and not the largest? The HW must only cover messages that every in-sync replica has. Taking the largest would mark messages as safe that some replica doesn't have yet.
At Ack Time: Same Data, Not Yet the Same HW
The producer is acked once every ISR member has the data. But the followers only learn that the data is committed from the leader's next reply. So right after the ack:
| Replica | Has data up to | Thinks HW is |
|---|---|---|
| Leader | 1200 | 1200 |
| Follower A | 1200 | 1100 (one round behind) |
| Follower B | 1200 | 1100 (one round behind) |
Follower: "give me from 1200" --> Leader: everyone has 1200, so HW = 1200
Leader: acks the producer
Leader: replies "HW = 1200" --> Follower: HW = 1200All three replicas hold the HW in memory. The leader's value is the real one; each follower's is a copy that lags by one fetch round, usually milliseconds.
Where the HW Is Stored
| Where | What |
|---|---|
| Memory | The live value. The leader updates it on every fetch; followers update theirs on every fetch reply. |
Disk: replication-offset-checkpoint | One shared file at the root of each log.dirs directory, with one line per partition. Rewritten every replica.high.watermark.checkpoint.interval.ms (default 5 s), so after a crash it can be up to about 5 s stale. |
Disk: leader-epoch-checkpoint | One file inside each partition directory (e.g. orders-0/). Each line is epoch startOffset, meaning which leader started writing at which offset. This file is what makes safe truncation possible. |
log.dirs/replication-offset-checkpoint
0 <-- format version
3 <-- number of entries
orders 0 1042 <-- topic, partition, HW
orders 1 998
payments 0 77The HW file holds only a bookmark. The messages themselves are in the partition's log segment files, which are appended to as each message arrives.
Scenario 1: One Broker Crashes With a Stale HW File
- The broker crashes. Its HW file says 1000, but the real HW was 1500.
- Messages 1000–1500 are still in its log files. Only the bookmark is behind.
- While it was down, another ISR replica became leader. The broker restarts as a follower.
- It checks with the new leader where its log stops matching (leader epochs), and deletes only from there.
- Its first fetch reply says "HW = 1500", and the bookmark is updated. Nothing is lost.
Scenario 2: The Leader Dies Before Followers Learn the New HW
This is the gap from the table above: both followers have messages up to 1200, but both think HW = 1100. The leader dies, and A becomes the new leader.
- A keeps its whole log. A leader never truncates its own log, so 1100–1200 stays. A's HW starts at its old value, 1100.
- B checks that its log matches A's. It does, so B deletes nothing.
- B fetches from 1200, its log end, not its HW. That tells A that B has everything up to 1200.
- A recomputes HW = smallest of (A: 1200, B: 1200) = 1200, and tells B in the reply.
The HW goes from 1100 to 1200 within one fetch round. Consumers may wait that long to read 1100–1200, but nothing is lost. The new leader's log decides what survives, not anyone's HW. The acked messages are safe because the producer was acked only after every ISR member had them, so whichever one becomes leader has them too.
Old Kafka's Bug, and the Leader-Epoch Fix
Before 0.11, a restarting follower trusted its own HW and truncated its log to it. In Scenario 2, B would have cut 1100–1200 because its HW said 1100. Those were messages the producer had been told were saved. If A then failed too, they were gone for good. Truncating to the HW could also leave two replicas with different messages at the same offset.
The fix (KIP-101, Kafka 0.11; tightened in KIP-279, 2.0): a follower no longer uses its HW to decide what to delete. It sends its latest leader epoch to the new leader (an OffsetsForLeaderEpoch request; newer versions do this check inside the fetch itself). The leader answers with the offset where that epoch ended in its log. The follower deletes only from that point and keeps everything that matches.
When Data Really Can Be Lost
A stale HW only makes a broker unsure for a moment. Real loss comes from messages that never reached any disk. Kafka doesn't fsync each write: it writes to the operating system's page cache, and the OS saves it to disk a moment later. Durability comes from copies on several machines.
| What fails | Data lost? |
|---|---|
| One broker crashes | No. The other ISR replicas have the data and the HW. |
| Every broker's Kafka process crashes, machines stay up | No. The page cache survives a process crash and the OS still writes it to disk. On restart a leader recomputes the HW from the logs. |
| Every replica's machine loses power at the same moment | Possibly the last few seconds, because those writes were only in memory on every copy. The HW file is not the cause. |
| Unclean leader election (an out-of-sync replica becomes leader) | Yes. That replica may be missing acked messages, and the others truncate to match it. |
The HW is a bookmark. The leader computes it as the smallest log end across the ISR, and followers learn it one round late. If the bookmark is stale, Kafka recovers by asking the leader. Messages are lost only when every copy of them is lost at the same moment, or when an out-of-sync replica is allowed to become leader.