← Week 2: Consensus Algorithms

Day 14: Challenge — Raft Leader Election in Rust

Phase 1 · Jun 2, 2026

← Week 2: Consensus Algorithms

Challenge Overview

Implement the leader election portion of Raft in Rust.

Scope (leader election only, not full log replication):

  1. Three simulated nodes running on Tokio tasks
  2. Each node communicates via Tokio channels (simulating RPCs)
  3. Randomized election timeouts (150–300ms)
  4. RequestVote / RequestVoteResponse messages
  5. Heartbeat AppendEntries from the elected leader
  6. Demonstrate leader election and re-election after leader "crash" (drop the channel)
← Week 2: Consensus Algorithms

Node State Machine

#[derive(Debug, PartialEq)]
enum Role { Follower, Candidate, Leader }

struct Node {
    id: u64,
    role: Role,
    current_term: u64,
    voted_for: Option<u64>,
    // channels to peers
}

State transitions:

  • Follower: reset election timer on any valid message
  • Candidate: send RequestVote; on majority votes → Leader
  • Leader: send periodic heartbeats; on higher term → Follower
← Week 2: Consensus Algorithms

Message Types

enum RaftMessage {
    RequestVote {
        term: u64,
        candidate_id: u64,
        last_log_index: u64,
        last_log_term: u64,
    },
    RequestVoteResponse {
        term: u64,
        vote_granted: bool,
    },
    AppendEntries {
        term: u64,
        leader_id: u64,
        // entries: empty for heartbeat
    },
    AppendEntriesResponse {
        term: u64,
        success: bool,
    },
}
← Week 2: Consensus Algorithms

Test Scenarios

  1. Normal election: all three nodes start; one wins election within 500ms
  2. Re-election: kill the leader (close its channel); remaining nodes elect a new leader
  3. Split vote: use deterministic timeouts to force a split; confirm a second round resolves it
  4. Stale leader: create a partition; old leader sends heartbeats; confirm it steps down when it sees the new term
← Week 2: Consensus Algorithms

Reflection

After completing, answer:

  1. How does randomized timeout prevent perpetual split votes?
  2. What guarantees does your implementation preserve even with simultaneous crashes?
  3. What would you need to add to extend this to full log replication?
  4. How does etcd handle the edge case where a candidate's RequestVote arrives after the election is already decided?
← Week 2: Consensus Algorithms

Week 2 Recap

Concept Key insight
Paxos Two-phase ballot protocol; safety via overlapping quorums
Raft leader election Randomized timeouts; vote restriction ensures log completeness
Raft log replication Append-then-commit; AppendEntries consistency check
Raft snapshots Independent per-node; InstallSnapshot for lagging followers
BFT 3f+1 required; PBFT deterministic; Nakamoto probabilistic
etcd/ZooKeeper Production consensus as a service; leases for distributed locks

Next week: distributed data structures — the algorithmic building blocks of distributed storage systems.