1.Leader/follower roles, heartbeats and leases
In 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.
Key features
- A heartbeat is a periodic "I'm still alive" signal; followers (or a monitor) infer leader failure only after missing some number of expected heartbeats within a timeout window.
- A lease is a time-bounded grant: "you are leader until time T," which expires automatically if not renewed — the leader must actively renew it before T to keep the role.
- Leases turn "did the leader crash?" (hard to know for certain) into "has the lease expired?" (a simple clock check) — this is why leases, not raw heartbeats alone, are the safer building block for leader-only actions.
- A leader that assumes it still holds the lease past its expiry (e.g. paused by a long garbage-collection pause) can keep acting as leader even though everyone else has already elected a new one — clock and pause assumptions matter, not just the lease mechanism itself.
- Follower roles are typically passive: replicate the leader's log/state and be ready to take over, rather than independently deciding anything the leader is responsible for.
Heartbeat vs lease, as an aliveness mechanism
- 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)
Command
leader: holds the lease, sends heartbeats,
renews the lease before it expires
follower: watches for heartbeats / lease expiry,
replicates leader state, stands by
lease: "leader until time T" — expires
automatically unless renewed before TA 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.