Skip to content

Leases and fencing tokens

A leader that does not know it was replaced

Section titled “A leader that does not know it was replaced”

An election answers who leads now. It cannot tell the old leader. The old leader may have checked a moment ago that it was in charge, prepared a write, and then stalled: a long garbage collection, a machine that was suspended, a network stack that held its packets. While it is stalled, the others stop hearing from it, wait out the timeout and elect another. When the old leader wakes, nothing inside it has changed, so it still believes it leads and sends the write it prepared.

A lease is the usual way to limit a leader’s time. The etcd documentation describes it: when the server grants a lease, it associates a time to live with it, and when the server detects that more than that time has passed, it revokes the lease. A leader holds its leadership for as long as the lease lasts and must renew it. A lease stops a crashed leader from holding on forever. It does not stop a paused one from acting after the lease has run out, because the holder does not know that time has passed. Kleppmann puts it the same way: if a pause outlasts the lease expiry and the client does not realise it has expired, it may go ahead and make an unsafe change.

Mental model. A night watchman is given a key for one shift and told to hand it back by dawn. He falls asleep in the guard room, the shift ends and a colleague is given the key. The watchman wakes up still holding a key that opens the door, and goes in. The key is not enough: the door has to know which shift the key is for.

In the lab, the election timeout acts as the lease: when it runs out without a heartbeat, a follower takes over. Storage is a separate node that accepts writes from whichever node sends them. Keep Election timeout at 300 ms and Pause of the leader at 1,500 ms, and leave Storage checks fencing tokens off.

A leader pauses, a follower is elected, and the old leader wakes up and writes. Change the timeout and the pause, and switch the fencing check on and off.

300 ms
1,500 ms

A fencing token is the term number sent with every write. Storage refuses a write whose token is older than one it has already seen.

2,520 ms
  • N1FollowerTerm 2
  • N2LeaderTerm 2
  • N3FollowerTerm 2
  • SStorageHolds A, highest token 2

N1 learns that term 2 exists and steps down to follower.

Results of the run

Time without a working leader
320 ms
Term at the end
2
Time two nodes believed they led
1,200 ms
Late write from the old leader
Accepted
Value in storage at the end
A
State of each node at the event shown
NodeStateHolds
N1FollowerTerm 2
N2LeaderTerm 2
N3FollowerTerm 2
SStorageHolds A, highest token 2
Every event in words
Every event of the run, in time order
At secondEvent
0 msN1 is the leader of term 1. A follower starts an election after 300 ms without a heartbeat. Storage takes writes from whichever node sends them.
1,000 msN1 checks that it is still the leader, prepares a write to storage, and then pauses for 1,500 ms, for example in a long garbage collection. It sends no heartbeats.
1,300 msN2 hears no heartbeat for 300 ms. It becomes a candidate for term 2 and asks for votes.
1,320 msN3 votes for N2. N2 is the leader of term 2.
1,330 msN2 writes B to storage with fencing token 2.
2,500 msN1 wakes up. It still believes it is the leader, does not check again, and sends the write it prepared, with token 1.
2,510 msStorage accepts the write with token 1, because it does not check tokens. It overwrites B with A.
2,520 msN1 learns that term 2 exists and steps down to follower.

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • Three nodes and one storage. N1 leads term 1. Any message takes 10 ms, and a follower starts an election when no heartbeat arrives within the timeout.
  • N1 checks that it is the leader, prepares a write, and then pauses for the time you choose. It does not check again when it wakes up.
  • N2 times out first and wins with the vote of N3. The other timeouts are longer, so the vote is never split.

Simplifications

  • Real elections can split the vote and need another round. Here only the mechanism of the late write is shown.
  • The fencing token is the term number. A real system gets tokens from a lock service or from a log.

Parameter values are illustrative. They are not benchmarks of any real product.

Follow the log. At 1,000 ms N1 checks that it is the leader, prepares a write and pauses. At 1,320 ms N2 is the leader of term 2, and at 1,330 ms it writes B to storage with token 2. At 2,500 ms N1 wakes up. It does not check again, and at 2,510 ms it sends its prepared write A, carrying token 1. Storage does not check tokens, so it accepts the write, and A overwrites B. At 2,520 ms N1 learns that term 2 exists and steps down. Two nodes believed they led for 1,200 ms, from 1,320 ms to 2,520 ms, and the storage ends holding A, the value of the leader that had been replaced.

Nothing in the election went wrong. Term 2 was elected correctly, by a majority. The failure is that the old leader’s write was still accepted.

Turn on Storage checks fencing tokens. A fencing token is a number that increases every time leadership is granted. In the lab it is the term number. The leader sends it with every write, and the storage remembers the highest it has seen and refuses a write carrying a lower one. Kleppmann describes the mechanism in these terms: a number incremented by the lock service every time a client acquires the lock, with the storage rejecting any write on which the token has gone backwards. The Raft paper has the same rule at the level of servers: a server that receives a request with a stale term number rejects it.

Run it again. Everything up to 2,500 ms is the same. At 2,510 ms, storage has already seen token 2, so it rejects the write with token 1, and the log says so. The new leader’s data stays: the lab ends with B in storage, and the late write from the old leader is shown as Rejected. The two leaders still existed for 1,200 ms. Fencing does not make the old leader stop believing it leads. It stops the belief from doing damage.

Pause of the leader Storage checks tokens Time without a leader Time two nodes believed they led Late write Value in storage
1,500 ms No 320 ms 1,200 ms Accepted A
1,500 ms Yes 320 ms 1,200 ms Rejected B
200 ms No 200 ms None No late write A
200 ms Yes 200 ms None No late write A

The last two rows show the case in which the pause ends before the timeout. No election happens, N1 is still the leader and its write is the right one, so token checking changes nothing.

A longer lease seems safer, since a leader would have to pause longer to be replaced. Set Election timeout to 600 ms with the pause at 1,500 ms and fencing off. The time without a leader grows from 320 ms to 620 ms, and the time two nodes believed they led shrinks from 1,200 ms to 900 ms, since the new leader arrives later while the old one still wakes at the same moment. The late write is still accepted and A still overwrites B. A longer lease costs availability and removes none of the risk: whatever the lease, a pause longer than it breaks the assumption that the holder knows it has expired. A token check is the part that does not depend on the length of the pause.

Check your understanding: fencing tokens

This quiz needs JavaScript to check your answers.

Question 1 of 4

The leader N1 pauses for 1,500 ms and the election timeout is 300 ms. N2 is elected and writes B with token 2. N1 wakes up and writes A with token 1. Storage does not check tokens.

What does storage hold at the end, and why?

Choose one answer.

Question 2 of 4

In the same run, N2 becomes the leader of term 2 at 1,320 ms. N1 learns that term 2 exists and steps down at 2,520 ms.

For how long did two nodes believe they were the leader?

Enter a number and choose its unit.

Question 3 of 4

Storage now checks tokens. What does it do with the write that carries token 1?

Choose one answer.

Question 4 of 4

To be safe, you raise the election timeout from 300 ms to 600 ms. The pause stays at 1,500 ms and storage does not check tokens.

What changes?

Choose one answer.

  • A leader that checked it was in charge can pause, be replaced, wake up and write. An election cannot prevent that, and neither can a lease, because the holder does not know that its time has run out.
  • In the lab, N1 pauses at 1,000 ms, N2 is elected at 1,320 ms and writes B, and N1 wakes at 2,500 ms. Without a check, its write A overwrites B and two nodes believed they led for 1,200 ms.
  • With Storage checks fencing tokens on, storage has seen token 2 and rejects the write with token 1. B stays, but the two leaders still overlap for 1,200 ms.
  • A 600 ms timeout raises the time without a leader to 620 ms and lowers the overlap to 900 ms, and the late write is still accepted without the check.
  • A fencing token only helps when the storage compares tokens and refuses lower ones.

Next: Distributed locks and coordination services.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.