This project is an implementation of the Raft distributed consensus algorithm in Go. Raft is a consensus algorithm designed for managing replicated logs, ensuring data consistency across multiple nodes in a distributed system through leader election, log replication, and other mechanisms.
This implementation is structured as a series of lab assignments, progressively building the complete Raft algorithm functionality:
- Implement Raft's leader election and heartbeat mechanisms
- Followers transition to Candidates when election timeout occurs
- Candidates become Leaders after receiving majority votes
- Leaders send periodic heartbeats to maintain leadership
- Implement mechanism for Leaders to replicate log entries to Followers
- Leaders receive client requests and append them as log entries
- Use
AppendEntriesRPC to replicate log entries to other nodes - Implement log consistency checks to ensure Follower logs match Leader
- Implement persistence of Raft node state for crash recovery
- Persistent state includes
currentTerm,votedFor, andlog - Write state changes to stable storage promptly
- Implement log snapshotting to prevent unbounded log growth
- Create snapshots of current state when log grows too large
- Leaders can send snapshots to lagging Followers for fast catch-up
Use Go's testing tools to run tests for each part:
# Navigate to the Raft source directory
cd src/raft
# Run Part A tests
go test -run PartA -race
# Run Part B tests
go test -run PartB -race
# Run Part C tests
go test -run PartC -race
# Run Part D tests
go test -run PartD -race
# Run all tests
go test -raceThe -race flag enables race detection, which is crucial for concurrent programming.
raft.go: Contains core Raft algorithm implementation including theRaftstruct, RPC handlers (RequestVote,AppendEntries), and state transition logicutil.go: Provides utility functions, primarily for debug loggingpersister.go: Provides persistent storage interfaceconfig.go: (Test code) Contains testing framework for simulating network environments and node behavior
- Leader Election: Robust leader election with randomized timeouts
- Log Replication: Reliable log replication with consistency guarantees
- Fault Tolerance: Handles network partitions and node failures
- Persistence: State persistence for crash recovery
- Log Compaction: Efficient log management through snapshots
The implementation follows the Raft paper specifications:
- Safety: Never returns incorrect results under non-Byzantine conditions
- Availability: Functional as long as majority of servers are operational
- Performance: Does not depend on timing for consistency
- Simplicity: Designed for understandability and practical implementation