Leader/follower roles, heartbeats and leases
coreintermediateIn a leader/follower design, exactly one node (the leader) is allowed to make a certain class of decisions — accept writes, assign work, or order events — while the rest (followers) accept the leader's decisions instead of racing to make their own. Because the leader can crash or lose its network connection at any moment, the system needs a way to keep proving "the current leader is still alive and in charge": a heartbeat is the leader (or a health checker) repeatedly signaling aliveness, and a lease is a time-bounded grant of leadership that automatically expires unless renewed — so followers know exactly how long they can trust the current leader before it is safe to consider the role vacant.
Think of it as
A lease is like a hotel key card programmed to stop working at checkout time. The guest (leader) doesn't have to hand the card back for the hotel to know the room is available again — the card simply stops opening the door once the clock passes checkout, unless the guest walks to the front desk and renews it first. Nobody has to detect that the guest left; the expiry does the work. A heartbeat is the simpler version: the guest calling the front desk every few minutes just to say "still here" — if the calls stop, the front desk assumes the room is free after a timeout, even though it can't be fully sure the guest didn't just lose phone signal.
What we're doing: Trace one lease renewal cycle and the timeout that would trigger a new election.
- 2
- The lease is granted with a hard expiry time, not an indefinite claim — this is what makes "leader is gone" detectable by a clock instead of a guess.
- 6
- The crash happens between renewals; followers have no way to know yet — they can only wait for the already-granted lease to run out.
- 7
- Only once the lease expires (not the moment of the crash) do followers treat the role as vacant and start a new election — a deliberate delay that trades detection speed for safety.
Why this works: This shows the actual timing gap every lease-based design accepts: there is always a window between a real failure and the system noticing, sized by the lease duration — a shorter lease detects failure faster but demands more frequent renewal traffic and network overhead.
Treating "missed one heartbeat" as proof the leader is dead
Wrong
Better
What you see: Elections trigger constantly under normal network jitter or GC pauses, with leadership flapping between nodes even though no node has actually failed.
Why: A single missed heartbeat is common under ordinary network delay or a garbage-collection pause and does not mean the leader is down — a lease with a deliberate expiry window (built from multiple missed intervals, not one) avoids triggering elections on transient noise.
- Node A → Followers: heartbeat, renews (lease expires t=13s)
- Node A → Followers: heartbeat, renews (lease expires t=16s)
- Node A → Node A: crashes (t=9s — followers don't know yet)
- Followers → Followers: lease expires, t=16s (no renewal received)
- Followers → Node B: new election (B wins, lease until t=27s)
Heartbeat vs lease, as an aliveness mechanism
Remember: A heartbeat proves recent aliveness; a lease proves aliveness for a bounded, expiring window — leases are safer for leader-only actions because "has it expired?" is a simple clock check, not an inference from a missed signal.
See also: failover and split brain · coordination systems · primary replica and sync vs async

