GoKV is a distributed fault-tolerant in-memory key-value store built in Go using the Raft consensus algorithm.
It supports leader election, automatic failover, replicated state machines, dynamic cluster membership, persistence, snapshots, and multiple consistency guarantees.
The system is designed to explore real distributed systems concepts while remaining usable as a lightweight local distributed cache / key-value store.
- Raft consensus using HashiCorp Raft
- Automatic leader election
- Leader failover
- Replicated state machine (FSM)
- Quorum-based safety guarantees
- Dynamic cluster membership
- N-node cluster support
- Leader-only writes
- Automatic node recovery after restart
- Write Ahead Log (WAL)
- Snapshot persistence
- Raft persistent logs
- Automatic recovery after crash
- Replay WAL during startup
- Restore FSM state automatically
GoKV supports multiple consistency guarantees.
Reads local state immediately.
GET /kv/userLowest latency.
Read directly from leader.
GET /kv/user?consistency=strongAlways returns latest committed value.
Follower serves local read only when:
applied_index >= leader_commit_index
Otherwise request automatically forwards to leader.
GET /kv/user?consistency=smartBalances correctness and latency.
Add nodes dynamically:
POST /cluster/joinRemove nodes dynamically:
DELETE /cluster/remove/:nodeIDCluster membership changes are replicated through Raft.
Supports:
- Runtime scaling
- Quorum recalculation
- Safe node removal
- Leader failover after topology changes
Client
|
+-----------+-----------+
| |
v v
Leader Node Follower Nodes
|
|
Raft Replication
|
v
FSM (State Machine)
|
v
In Memory KV Store
|
+------+------+
| |
WAL Snapshot
- Go
- Gin
- HashiCorp Raft
- BoltDB
- WAL Persistence
- Snapshot Recovery
GoKV/
├── cmd/server/
├── internal/
│
├── raft/
├── handlers/
├── services/
├── repository/
├── snapshot/
├── wal/
├── cache/
├── cluster/
│
├── raft-data-\*/
├── go.mod
└── README.md
Bootstrap node creates the initial cluster.
NODE_ID=node1 \
RAFT_PORT=7001 \
PORT=8001 \
go run cmd/server/main.goVerify:
curl localhost:8001/node/statusExpected:
{
"id":"node1",
"role":"leader"
}Node 2:
NODE_ID=node2 \
RAFT_PORT=7002 \
PORT=8002 \
go run cmd/server/main.goJoin:
curl \
-X POST localhost:8001/cluster/join \
-H "Content-Type: application/json" \
-d '{
"node_id":"node2",
"raft_addr":"127.0.0.1:7002"
}'Node 3:
NODE_ID=node3 \
RAFT_PORT=7003 \
PORT=8003 \
go run cmd/server/main.goJoin:
curl \
-X POST localhost:8001/cluster/join \
-H "Content-Type: application/json" \
-d '{
"node_id":"node3",
"raft_addr":"127.0.0.1:7003"
}'curl localhost:8001/node/status
curl localhost:8002/node/status
curl localhost:8003/node/statusExpected:
1 leader
N-1 followers
Same term across cluster
Write:
curl \
-X PUT localhost:8001/kv \
-H "Content-Type: application/json" \
-d '{"key":"user","value":"Tom"}'Read:
curl localhost:8002/kv/userStrong read:
curl "localhost:8002/kv/user?consistency=strong"Smart read:
curl "localhost:8002/kv/user?consistency=smart"Delete:
curl \
-X DELETE localhost:8001/kv/userGoKV supports:
Leader crash
↓
Election timeout
↓
Majority vote
↓
New leader elected
Node crashes
↓
Restart process
↓
Load persisted raft state
↓
Rejoin cluster automatically
Remove node
↓
Quorum recalculated
↓
Replication continues
Raft guarantees:
- No split brain
- Majority quorum required
- Leader-only writes
- Log consistency
- Safe failover
- Membership changes replicated through consensus
Raft cluster fault tolerance:
1 node → tolerate 0 failures
3 nodes → tolerate 1 failure
5 nodes → tolerate 2 failures
7 nodes → tolerate 3 failures
Formula:
Failures tolerated = floor((N - 1) / 2)
Raft replicates logs.
FSM applies committed logs into actual state.
Example:
PUT user=shreyas
↓
Replicated Raft Log
↓
FSM Apply()
↓
Memory Store Updated
Without FSM:
Consensus exists
But data never changes
FSM converts consensus into application state.
- Docker Compose deployment
- Metrics endpoint
- Graceful shutdown
- Authentication
- TLS
- Benchmark suite
GoKV explores:
- Consensus algorithms
- Distributed consistency
- Fault tolerance
- Quorum systems
- Replicated state machines
- Persistence and recovery
- Dynamic cluster membership
- Leader election
Built with Go and distributed systems principles.