A distributed key-value store implementation inspired by etcd, built with Go. This project provides a simple, secure, fast, and reliable distributed key-value store with the following features:
- Distributed consensus using the Raft protocol
- HTTP API for key-value operations
- TTL (Time-To-Live) support for keys
- Thread-safe operations
- Graceful shutdown
- Distributed Consensus: Uses the Raft protocol to ensure consistency across all nodes in the cluster
- HTTP API: RESTful API for key-value operations
- TTL Support: Keys can be set with an expiration time
- Thread Safety: All operations are thread-safe using Go's sync package
- Persistence: Log entries are persisted to disk
- Graceful Shutdown: Clean shutdown of all components
- Go 1.21 or later
go buildTo start a node in the cluster:
./kv-store -id node1 -addr :8080 -peers "node2=localhost:8081,node3=localhost:8082"GET /v1/kv/{key}- Get a value by keyPUT /v1/kv/{key}- Set a value with optional TTLDELETE /v1/kv/{key}- Delete a key-value pair
Example request to set a value with TTL:
curl -X PUT http://localhost:8080/v1/kv/mykey \
-H "Content-Type: application/json" \
-d '{"value": "myvalue", "ttl": "5s"}'The project is organized into the following components:
internal/raft: Raft consensus protocol implementationinternal/api: HTTP API serverstore: Key-value store implementation
The Raft protocol ensures that all nodes in the cluster maintain a consistent state. The implementation includes:
- Leader election
- Log replication
- Membership changes
- Snapshotting
The key-value store provides:
- Thread-safe operations using sync.Map
- TTL support for keys
- Persistence of data
- Clean error handling
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.