Explain leases, consensus and Raft
Explain leases, consensus and Raft
A lease grants temporary authority, but an expired holder may resume after a pause and still attempt a write. A fencing token is a monotonically increasing authority number checked by the protected resource. Consensus coordinates agreement despite failures. Raft uses terms, leader election and replicated logs; a majority is necessary for progress in a fixed voting configuration, not sufficient by itself to implement Raft.
package main
import ( "fmt")
type Resource struct { Highest int Value string}
func (r *Resource) write(token int, v string) bool { if token <= 0 || token < r.Highest { return false } r.Highest = token r.Value = v return true}func main() { r := Resource{} fmt.Println(r.write(1, "old")) fmt.Println(r.write(2, "new")) fmt.Println(r.write(1, "stale"), r.Value)}The resource rejects a stale holder after observing a higher token. This sequential example assumes tokens come from an authoritative service and are never reused for different owners. Real resources must enforce the check atomically and durably. A token alone does not detect expiry before a newer token is observed.
Expected output:
truetruefalse newCalculate a majority, not a consensus algorithm
package main
import ( "fmt")
func quorum(n, reachable int) bool { return n > 0 && reachable >= 0 && reachable <= n && reachable >= n/2+1}func main() { fmt.Println(quorum(5, 2), quorum(5, 3), quorum(3, 2))}With five voters, three constitute a majority. In a 3/2 partition only the majority side can potentially elect and commit new entries. Raft also requires vote/log freshness rules, term checks, persistence, commitment rules and correct membership changes. An old leader must not serve stale results as linearizable reads; lease-based reads need timing assumptions. Use an established implementation instead of this arithmetic in production.
Expected output:
false true trueTry it locally
Calculate the majority size for 1, 3 and 5 fixed voters. Print each size. Assume positive voter counts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Distributed services 08: Explain leases, consensus and Raft
Check the lesson objective before moving on.
Why check fencing tokens at the protected resource?
Does three reachable voters out of five prove Raft committed an entry?