RaftKV is a distributed Key-Value store built in Rust, implementing the Raft Consensus Algorithm. It handles leader election, log replication, and consistent data storage across a cluster of nodes.
This project demonstrates Intermediate-to-Advanced Rust patterns, focusing on asynchronous concurrency, type safety, and modular architecture.
- Asynchronous Core: Built on the Tokio runtime. The consensus engine uses non-blocking I/O for high-performance networking and timer management, avoiding costly thread spawning.
- Pluggable Architecture:
- Storage Abstraction: Uses
#[async_trait]to decouple the Raft logic from the underlying storage engine. This allows hot-swapping betweenMemStorage(for testing) and persistentFileStorage. - Network Abstraction: Decoupled network layer allowing for easy mocking and different transport implementations (gRPC, TCP, Channels).
- Storage Abstraction: Uses
- Type-Safe Design: Leveraging Rust's strong type system (Enums & Pattern Matching) to enforce valid state transitions and prevent "stringly typed" errors in command parsing.
- Workspace Structure: organized as a Cargo Workspace with isolated crates (
raft-core,kv-store,api) to enforce separation of concerns.
The project is split into three main crates:
crates/raft-core: The pure consensus logic. It defines theRaftNode,RaftStoragetrait, and RPC types. It is completely agnostic of the web layer.crates/kv-store: A persistent Log Structured Merge (LSM)-lite storage engine. It provides durability by appending to a write-ahead log (WAL).crates/api: The HTTP Edge layer using Actix-Web. It translates REST requests into Raft commands.
- Rust (1.70+)
# Start the server (defaults to port 8080)
cargo run --bin raft-serverSet a Value (Proposes to Raft Leader)
curl -X POST http://localhost:8080/set -d '{"key": "foo", "value": "bar"}' -H "Content-Type: application/json"Get a Value (Strongly Consistent Read)
curl http://localhost:8080/get/fooThe project includes an InMemory Storage adapter (MemStorage) designed for high-speed integration testing of the consensus logic without hitting the disk.
cargo test