A distributed, highly-available, in-memory key-value store built with the Raft consensus algorithm in Go. This project is a practical implementation of a fault-tolerant state machine, perfect for learning and as a foundation for more complex distributed systems.
Raft Cache is a simplified version of in-memory distributed key-value database. It ensures strong consistency across a cluster of machines by using the Raft protocol to replicate all state changes. If a majority of nodes are available, your data is safe and consistent, even if some nodes fail.
This is a pet-project - well-structured implementation that demonstrates the core concepts of distributed consensus.
-
Raft Consensus: Implements leader election, log replication, and safety mechanisms as defined in the Raft paper.
-
Strong Consistency: All reads and writes are handled by the cluster leader, guaranteeing linearizability.
-
Simple HTTP API: Easy-to-use REST-like interface for GET, SET, and DELETE operations.
-
Cluster Management: Built-in support for dynamically adding and removing nodes from the cluster.
-
In-Memory Storage: Blazing fast read and write performance for the stored data.
Prerequisites
-
Go 1.24+: Make sure you have Go installed on your system.
-
Git: To clone the repository.
git clone https://github.com/masterkusok/raft-cache.git
cd raft-cachego mod downloadgo build -o raft-cache cmd/main.goThe easiest way to test a cluster is by running multiple nodes on your local machine.
./raft-cacheThis starts a node with ID node1, HTTP API on port 8000, Raft communication on port 8081, and initializes a cluster with itself.
./raft-cache --local-id=node-2 --raft-addr=localhost:8082 --raft-dir=temp2/ --max-pool=3 --leader-addr=localhost:8082 --port=:8001 --leader-api-endpoint=http://localhost:8000
./raft-cache --local-id=node-3 --raft-addr=localhost:8083 --raft-dir=temp3/ --max-pool=3 --leader-addr=localhost:8081 --port=:8002 --leader-api-endpoint=http://localhost:8000Those nodes will contact node1 and ask to join the cluster.
You now have a 3-node Raft cluster running! You can kill any single node, and the cluster will continue to operate normally, electing a new leader if necessary.
You can use DB using simple http requests:
curl -X POST http://localhost:8000/api/v1/key/{key}/value/{value}
curl -X GET http://localhost:8000/api/v1/key/{key}
curl -X DELETE http://localhost:8000/api/v1/key/{key}