LESSON 04 · Leases and fencing

Survive a leader that wakes up late

A lease lets one node lead for a limited time, and a fencing token lets storage reject the leader that slept through its expiry.

SYSTEM VIEW

The agreement path

Diagram view
Time—
Lease holder—
Leaders wrote—

Watch a leader freeze past its lease’s expiry and wake up still writing, then replay it with a token that Storage checks.

  1. Only one process should run this job, so P1 takes a lease from the lease service: 10 seconds as leader unless it renews. At t=8 it checks the lease, sees 2 seconds left, and goes to write.

    Lease
    10 s
    Left at t=8
    2 s

    What changed

    • P1’s 10 s lease
    • P1 leads
    • Check at t=8: 2 s left
  2. Right after its check, P1 freezes for 15 seconds: a garbage-collection pause stops it between two instructions, and it can’t notice. At t=10 its lease expires, as it must, so a crashed holder can’t block everyone forever.

    P1 frozen
    t=8 to 23
    Lease expires
    t=10

    What changed

    • P1 frozen at t=8
    • P1’s lease at t=10
  3. At t=11 the lease service grants the lease to P2, which was waiting. P2 now leads, renewing its lease as it works, and its write reaches Storage at t=16. So far this is failover working: one lease, one leader writing. Frozen, P1 still believes it leads.

    P2’s lease
    from t=11
    Writing
    P2 only

    What changed

    • P2’s 10 s lease
    • P2 leads
    • P2’s write
  4. At t=23 P1 wakes. To P1 no time has passed: its check said 2 seconds remained, so it writes as leader, and Storage takes both writes. Checking right before each write wouldn’t help, since the pause can fall just after the check.

    Check before writing
    pause can follow it
    Longer lease
    slower failover

    What changed

    • P1 wakes at t=23
    • P1’s write
    • Two leaders wrote
  5. Replay it with fencing: each grant now returns a number that only increases. P1’s lease comes with token 71. P1 still freezes at t=8 and its lease still expires at t=10; P2’s grant at t=11 carries the next number, 72.

    P1’s token
    71
    P2’s token
    72

    What changed

    • Grants carry tokens
    • P1 gets 71
    • P2 gets 72
  6. Every write now carries its leader’s token. P2 writes with 72, and the write reaches Storage at t=16. Storage accepts it and remembers 72 as the highest token it has seen, the number every later write is compared with.

    P2’s write
    token 72
    Highest seen
    72

    What changed

    • P2’s write, token 72
    • Storage keeps 72
  7. P1 wakes at t=23 and writes with 71. Storage has already seen 72, so it refuses the write: the fence working as designed. The check lives in the resource, which can judge staleness when the paused process can’t.

    Highest seen
    72
    P1’s write
    71 < 72: refused

    What changed

    • P1 wakes at t=23
    • Write with 71 refused
    • One leader wrote
  8. Fencing works only where the resource stores the highest token and compares it on every write. A third-party API won’t, so send it an idempotency key derived from the job instead, which makes a repeat harmless. etcd revisions and ZooKeeper’s zxid can serve as tokens.

    Storage must
    keep 72, compare
    Third-party API
    idempotency key
    Tokens from
    etcd, ZooKeeper

    What changed

    • Storage compares every write
    • Idempotency keys elsewhere

Space plays · ← → step · Home resets

THE IDEA

How it works

Many jobs need exactly one actor: one process compacting a log, or one scheduler assigning work. Leader election picks that actor, often by having candidates take a lease, a lock that expires unless renewed, from a service backed by a majority. Expiry matters because a crashed holder must not block everyone forever.

Expiry also sets a trap, because a process can be paused at any moment without noticing. Garbage collection, a VM migration, or a stalled disk can freeze it between any two instructions. Say P1 holds a 10-second lease and checks that 2 seconds remain. It then freezes for 15 seconds. The lease expires, and P2 acquires it and writes. P1 wakes, still trusting its check, and writes too, believing it is leader. Checking right before each write does not help, because the pause can fall just after the check. Longer leases only slow failover.

A fencing token closes the hole. Each grant returns a number that only increases, and every write carries it. The storage layer remembers the highest token it has seen and rejects anything lower. P1 got 71 and P2 got 72, so after P2’s write, P1’s write with 71 is refused. The check lives in the resource, which can judge staleness when the paused process cannot. etcd revisions and ZooKeeper’s zxid can serve as tokens.

The resource has to cooperate, storing the highest token and comparing on each write. A third-party API will not do that, so send it an idempotency key derived from the job, which makes a repeat harmless.

DECISION POINT

A compaction leader writes merged files to S3. After a long VM migration, two leaders uploaded different files under the same key, and readers picked up the stale one. S3 will not compare your tokens for you. What do you change?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • A process cannot rule out a pause, so “I hold the lease” can be false by the time it acts.
  • Fencing moves the decision to the resource, which rejects tokens below the highest it has seen.
  • Resources that cannot compare tokens need idempotency keys instead.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

Storage last accepted token 72. Writes then arrive carrying 71, 72, and 74, in that order. Which are accepted?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.