Consensus Interactive Lab Docs
Raft Consensus Visualizer • Documentation
Interactive simulation of the Raft leader election algorithm featuring asynchronous network delays, split votes, term changes, and event-loop state machines.
#Consensus / State
#Architecture
#Interactive Simulation
View Source on GitHub ↗ Raft Consensus Visualizer 🌐
A fully client-side, animated visualizer for the Raft Leader Election Algorithm. This project was built to demystify how distributed systems achieve consensus by showing network packets, randomized timeouts, and state transitions in real-time.
🎯 Features
- True Event-Loop Multithreading: The underlying logic operates exactly like a real distributed system. Nodes are decoupled class instances that rely purely on asynchronous JS timeouts (
setTimeout/setInterval) rather than a mastertick()loop. - Simulated Network Latency: A
Networkbroker intercepts all RPCs and artificially delays them by400ms - 800msto simulate realistic, congested network conditions. - Dynamic Animations: Real-time rendering of network packets (
RequestVote,VoteGranted,AppendEntries) flying across the screen using Framer Motion. - Chaos Engineering: Click on the current Leader node to “Kill” it. Watch as heartbeats stop, followers timeout, and a new election gracefully orchestrates a replacement!
- Beautiful UI: A brutalist, sharp-edged, expressive light theme inspired by Claude and Material 3 design systems.
🛠️ Tech Stack
- React 19 + Vite: Blazing fast client-side rendering.
- TypeScript: Strict typing for all Raft State Machine logic and RPC messages.
- Tailwind CSS v4: Zero-config, massive utility-class styling for layout and themes.
- Framer Motion: Handles all coordinate tracking, SVG progress bars, and packet animations seamlessly.
🏗️ Low-Level Design (LLD)
The core algorithm is cleanly decoupled from the React UI and lives entirely inside src/simulation/.
types.ts: Defines the strictRPCMessagetypes and Node states (FOLLOWER,CANDIDATE,LEADER,DEAD).Network.ts: Acts as the message broker. It maintains a registry of all active nodes and handles thesend()andbroadcast()APIs, injecting random latency before delivering payloads.Node.ts: The brain of the operation. Each node maintains its owncurrentTerm,votedFor, and an independent randomized election timer. It receives RPCs from the Network and independently decides whether to step down, grant votes, or transition to a Candidate.ClusterManager.ts: The orchestrator that boots up theNnodes, registers them to the Network, and bridges the Backend Node events (onStateChange,onTimerReset) up to the React Frontend.
🚀 How to Run Locally
- Clone the repository (if you haven’t already).
- Navigate to the project directory:
cd leader-election-web - Install dependencies:
npm install - Start the development server:
npm run dev - Open your browser: Visit
http://localhost:5173. - Click Start Simulation and enjoy!