Rafting is a learning project that aims to provide a concise implementation of the Raft consensus algorithm in Rust.
- Educational: This project is intended to be a learning resource for understanding the Raft consensus algorithm.
- Minimal: This project implements the essential features of the Raft algorithm, including
- leader election
- log replication
- log consistency
- client interaction with the cluster (using REST)
- in-memory storage
- Tested: This project is tested using a detailed set of unit tests.
- Production-ready: This project is not intended to be used in production.
- Comprehensive: This project is not a complete implementation of the Raft algorithm. It is missing features such as
- membership changes
- log compaction
- snapshotting
- proxying of client requests to the leader
- persistent storage
- gRPC Integration: Utilizes gRPC for Remote Procedure Calls (RPC) between nodes. Protocol definitions are specified in
proto/raft.proto
. Therpc
module houses server and client stubs for gRPC-based interactions with the cluster members. - Raft Module: The internal interactions within the node is orchestrated through tokio. The
raft
module is designed to be agnostic to the RPC mechanism (although the models are reused in places), communicating exclusively throughtokio
channels with therpc
module. - REST API: The
rafting
binary exposes a REST API for interacting with the cluster (leader only). The API is implemented using theaxum
web framework.
Clone the repository:
git clone https://github.com/yourusername/rafting.git
cd rafting
Build and run the project:
cargo build
cargo run
proto/raft.proto
: protobuf file specifying gRPC service definitions.src/rpc
: Module containing server and client stubs for gRPC-based interactions.src/raft
: Module implementing the Raft consensus algorithm.src/main.rs
: Main entry point for the project.
To add more node, add a new entry in the cluster_config.yaml
file.
cargo run -- -n node1 -c config/cluster_config.yaml
cargo run -- -n node2 -c config/cluster_config.yaml
cargo run -- -n node3 -c config/cluster_config.yaml
Optionally, you could run it in release mode by running:
cargo build --release
and then
./target/release/rafting -n node1 -c config/cluster_config.yaml
./target/release/rafting -n node2 -c config/cluster_config.yaml
./target/release/rafting -n node3 -c config/cluster_config.yaml
curl -i -X POST -H "Content-Type: application/json" --request POST --data '{"key":"a","value":"1"}' http://<LEADER_NODE>:<WEB_PORT>/command
curl -i -X POST -H "Content-Type: application/json" --request POST --data '{"key":"b","value":"2"}' http://<LEADER_NODE>:<WEB_PORT>/command
curl -i -X POST -H "Content-Type: application/json" --request POST --data '{"key":"c","value":"3"}' http://<LEADER_NODE>:<WEB_PORT>/command
Contributions to Rafting are welcome! Whether you want to fix a bug, add a feature, or improve documentation, feel free to open an issue or submit a pull request.
Rafting is licensed under the MIT License - see the LICENSE file for details.
- The Raft paper by Diego Ongaro and John Ousterhout - In Search of an Understandable Consensus Algorithm.