A high-performance, fault-tolerant distributed key-value storage system inspired by Amazon's Dynamo whitepaper
Features β’ Quick Start β’ Architecture β’ API β’ Configuration β’ Testing
- Overview
- Features
- System Requirements
- Quick Start
- Architecture
- API Reference
- Configuration
- Testing
- Benchmarks
- Project Structure
- Technical Deep Dive
- Contributing
- License
- Acknowledgments
Distributed Key-Value Store with Tunable Consistency is a distributed key-value store that implements the core principles from Amazon's groundbreaking Dynamo paper. It's designed for scenarios requiring:
- High Availability - The system remains operational even when nodes fail
- Horizontal Scalability - Add more nodes to handle increased load
- Low Latency - Optimized for fast reads and writes
- Eventual Consistency - With tunable consistency levels for flexibility
This project serves as an excellent learning resource for understanding:
- Distributed systems concepts
- Consensus and replication strategies
- Failure detection mechanisms
- Storage engine design
| Feature | Description |
|---|---|
| Consistent Hashing | Data partitioning using a 64-bit hash ring with virtual nodes |
| Replication | Configurable N-way replication across preference list nodes |
| Tunable Consistency | Quorum-based operations (W + R > N guarantees) |
| Gossip Protocol | Peer-to-peer failure detection and membership dissemination |
| Hinted Handoff | Temporary storage for unavailable nodes |
| Read Repair | Automatic consistency healing during read operations |
| Vector Clocks | Causality tracking for conflict detection |
| Last-Write-Wins | Timestamp-based conflict resolution |
| Feature | Description |
|---|---|
| Append-Only Writes | All writes are sequential appends for durability |
| In-Memory Index | O(1) key lookups with single disk seek |
| CRC Checksums | Data integrity verification |
| Compaction | Space reclamation from deleted/overwritten keys |
| Crash Recovery | Automatic index rebuild from log on restart |
| Feature | Description |
|---|---|
| RESTful API | Simple HTTP/JSON interface |
| Admin Endpoints | Cluster status, statistics, ring visualization |
| Graceful Shutdown | Clean shutdown with data persistence |
| Configurable | CLI flags, config files, or environment variables |
| Requirement | Minimum | Recommended |
|---|---|---|
| Go Version | 1.21+ | 1.22+ |
| RAM | 512MB per node | 2GB per node |
| Disk | SSD recommended | NVMe for best performance |
| OS | Linux, macOS, Windows | Linux (production) |
| Network | 1Gbps | 10Gbps for high throughput |
# Clone the repository
git clone https://github.com/bhushanasati25/Distributed-Key-Value-Store-with-Tunable-Consistency.git
cd Distributed-Key-Value-Store-with-Tunable-Consistency
# Install dependencies
go mod download
# Build the binary
make build
# Verify installation
./bin/dynamo --version./bin/dynamo \
--node-id=node1 \
--port=8001 \
--data-dir=./data/node1Terminal 1:
./bin/dynamo --node-id=node1 --port=8001 --gossip-port=7001 --data-dir=./data/node1Terminal 2:
./bin/dynamo --node-id=node2 --port=8002 --gossip-port=7002 --data-dir=./data/node2 \
--seeds=127.0.0.1:7001Terminal 3:
./bin/dynamo --node-id=node3 --port=8003 --gossip-port=7003 --data-dir=./data/node3 \
--seeds=127.0.0.1:7001,127.0.0.1:7002Or use the convenience script:
make cluster # Starts 3 nodes
make stop-cluster # Stops all nodes# Store a value
curl -X PUT http://localhost:8001/kv/user:1001 \
-H "Content-Type: application/json" \
-d '{"value": "{\"name\": \"John\", \"email\": \"john@example.com\"}"}'
# Retrieve a value
curl http://localhost:8001/kv/user:1001
# Delete a value
curl -X DELETE http://localhost:8001/kv/user:1001
# Check cluster status
curl http://localhost:8001/admin/status | jq βββββββββββββββββββ
β Client App β
ββββββββββ¬βββββββββ
β
β HTTP/REST
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββ
β Load Balancer β
ββββββββββββββββββββββββββ¬ββββββββββββββββββββ
β
ββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Node 1 βββββββββββββΊβ Node 2 βββββββββββββΊβ Node 3 β
β (Coordinator) β Gossip β (Replica) β Gossip β (Replica) β
βββββββββββββββββββ€ βββββββββββββββββββ€ βββββββββββββββββββ€
β βββββββββββββββ β β βββββββββββββββ β β βββββββββββββββ β
β β Hash Ring β β β β Hash Ring β β β β Hash Ring β β
β βββββββββββββββ β β βββββββββββββββ β β βββββββββββββββ β
β βββββββββββββββ β β βββββββββββββββ β β βββββββββββββββ β
β β Bitcask β β β β Bitcask β β β β Bitcask β β
β β Storage β β β β Storage β β β β Storage β β
β βββββββββββββββ β β βββββββββββββββ β β βββββββββββββββ β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
ββββββββββββ βββββββββββββββ ββββββββββββββββ βββββββββββββββ
β Client ββββββΊβ Coordinator ββββββΊβ Hash Ring ββββββΊβ Preference β
β β β Node β β Lookup β β List β
ββββββββββββ βββββββββββββββ ββββββββββββββββ βββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββ€
β β
βΌ βΌ
βββββββββββββββββ βββββββββββββββββ
β Replicate β β Replicate β
β to Node A β β to Node B β
βββββββββββββββββ βββββββββββββββββ
β β
βββββββββββββββ¬ββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββ
β Quorum Check β
β (W acks) β
βββββββββββββββββ
β
βΌ
βββββββββββββββββ
β Response β
β to Client β
βββββββββββββββββ
0
β
ββββββββ΄βββββββ
/ \
/ \
N3-v2 β β N1-v1
/ \
/ \
N2-v1 β β N2-v2
β β
β βββββββββββββ β
β β Ring β β
β β 64-bit β β
β β Hash β β
β β Space β β
β βββββββββββββ β
β β
N1-v2 β β N3-v1
\ /
\ /
\ /
β N1-v3 N2-v3 β
\ /
\ /
ββββββββ¬βββββββ
β
2βΆβ΄
PUT /kv/{key}
Content-Type: application/json
{
"value": "your data here",
"consistency": "quorum" // optional: "one", "quorum", "all"
}Response (200 OK):
{
"status": "ok",
"key": "mykey",
"version": 1702934567890123456
}GET /kv/{key}?consistency=quorumResponse (200 OK):
{
"key": "mykey",
"value": "your data here",
"version": 1702934567890123456
}Response (404 Not Found):
{
"error": "Not Found",
"code": 404,
"message": "key not found"
}DELETE /kv/{key}Response (200 OK):
{
"status": "ok",
"key": "mykey"
}GET /admin/statusResponse:
{
"node_id": "node1",
"address": "127.0.0.1:8001",
"uptime": "2h 15m 30s",
"keys": 15420,
"storage": {
"active_keys": 15420,
"deleted_keys": 123,
"data_file_size_bytes": 52428800,
"total_reads": 1250000,
"total_writes": 450000
},
"cluster": {
"size": 3,
"nodes": [
{"id": "node1", "address": "127.0.0.1:8001", "state": "alive"},
{"id": "node2", "address": "127.0.0.1:8002", "state": "alive"},
{"id": "node3", "address": "127.0.0.1:8003", "state": "alive"}
]
}
}GET /admin/ringGET /admin/statsGET /admin/keysGET /health| Level | Replicas Required | Use Case |
|---|---|---|
one |
1 | Maximum availability, single-node testing |
quorum |
β(N+1)/2β | Balanced consistency/availability (default) |
all |
N | Maximum consistency, lowest availability |
| Flag | Type | Default | Description |
|---|---|---|---|
--node-id |
string | hostname | Unique identifier for this node |
--address |
string | 127.0.0.1 | IP address to bind to |
--port |
int | 8080 | HTTP API port |
--gossip-port |
int | 7946 | UDP port for gossip protocol |
--data-dir |
string | ./data | Directory for persistent storage |
--seeds |
string | "" | Comma-separated seed node addresses |
--replication |
int | 3 | Replication factor (N) |
--read-quorum |
int | 2 | Read quorum (R) |
--write-quorum |
int | 2 | Write quorum (W) |
--vnodes |
int | 150 | Virtual nodes per physical node |
--config |
string | "" | Path to JSON config file |
--version |
bool | false | Show version and exit |
{
"node_id": "production-node-1",
"address": "10.0.1.10",
"port": 8080,
"gossip_port": 7946,
"data_dir": "/var/lib/distributed-kvstore",
"seed_nodes": [
"10.0.1.11:7946",
"10.0.1.12:7946"
],
"max_file_size": 104857600,
"sync_writes": false,
"compact_interval": 300,
"replication_factor": 3,
"read_quorum": 2,
"write_quorum": 2,
"virtual_nodes": 150
}All config options can also be set via environment variables with the DYNAMO_ prefix:
export DYNAMO_NODE_ID=node1
export DYNAMO_PORT=8001
export DYNAMO_REPLICATION_FACTOR=3# Run all unit tests
make test
# Run with verbose output
go test -v ./internal/...
# Run specific package tests
go test -v ./internal/storage/...
go test -v ./internal/ring/...# Requires building first
make build
# Run integration tests
make test-integration
# Or manually
go test -v -tags=integration ./test/integration/...make coverage
# View coverage report in browser
open coverage.html# Built-in load tester
make load-test
# Custom parameters
go run test/load/benchmark.go \
-target=http://localhost:8001 \
-requests=10000 \
-concurrency=50 \
-write-ratio=0.3# Start cluster
make cluster
# Write some data
for i in {1..100}; do
curl -X PUT "http://localhost:8001/kv/key$i" -d "{\"value\":\"value$i\"}"
done
# Kill a node (simulates failure)
kill $(lsof -t -i:8002)
# Verify reads still work (quorum of 2 from remaining nodes)
curl http://localhost:8001/kv/key50
# Restart killed node
./bin/dynamo --node-id=node2 --port=8002 --gossip-port=7002 \
--data-dir=./data/node2 --seeds=127.0.0.1:7001- Hardware: MacBook Pro M1, 16GB RAM
- Cluster: 3 local nodes
- Data: Random 1KB values
| Metric | Single Node | 3-Node Cluster |
|---|---|---|
| Write Throughput | 15,000 ops/sec | 5,000 ops/sec |
| Read Throughput | 45,000 ops/sec | 12,000 ops/sec |
| P50 Latency | 0.5ms | 2ms |
| P99 Latency | 3ms | 15ms |
| P99.9 Latency | 8ms | 35ms |
Note: Cluster throughput is lower due to quorum requirements and network round-trips.
Distributed-Key-Value-Store-with-Tunable-Consistency/
βββ cmd/
β βββ dynamo/
β βββ main.go # Application entry point
β
βββ internal/
β βββ api/
β β βββ server.go # HTTP server setup
β β βββ handlers.go # Request handlers
β β βββ middleware.go # Logging, CORS, recovery
β β
β βββ config/
β β βββ config.go # Configuration management
β β
β βββ gossip/
β β βββ membership.go # Cluster membership list
β β βββ detector.go # Failure detection
β β βββ protocol.go # UDP gossip protocol
β β
β βββ replication/
β β βββ coordinator.go # Distributed operations
β β βββ quorum.go # Quorum management
β β βββ handoff.go # Hinted handoff
β β
β βββ ring/
β β βββ hash_ring.go # Consistent hashing
β β βββ vnode.go # Virtual nodes
β β
β βββ storage/
β β βββ engine.go # Storage interface
β β βββ bitcask.go # Bitcask implementation
β β βββ index.go # In-memory index
β β βββ bitcask_test.go # Unit tests
β β
β βββ versioning/
β βββ vector_clock.go # Vector clock operations
β βββ resolver.go # Conflict resolution
β
βββ pkg/
β βββ types/
β βββ types.go # Shared types & interfaces
β
βββ test/
β βββ integration/
β β βββ cluster_test.go # Multi-node tests
β βββ load/
β βββ benchmark.go # Load testing tool
β
βββ scripts/
β βββ start_cluster.sh # Start 3-node cluster
β βββ stop_cluster.sh # Stop cluster
β
βββ go.mod # Go module definition
βββ go.sum # Dependency checksums
βββ Makefile # Build automation
βββ config.example.json # Example configuration
βββ .gitignore
βββ LICENSE
βββ README.md
// Hash function: MurmurHash3 (64-bit)
func hash(key string) uint64 {
h := murmur3.New64()
h.Write([]byte(key))
return h.Sum64()
}
// Virtual nodes for even distribution
for i := 0; i < virtualNodes; i++ {
vnodeKey := fmt.Sprintf("%s#vnode%d", nodeID, i)
ring.AddToken(hash(vnodeKey), nodeID)
}
// Key lookup: binary search for successor
func GetNode(key string) string {
h := hash(key)
idx := sort.Search(len(tokens), func(i int) bool {
return tokens[i].Hash >= h
})
return tokens[idx % len(tokens)].NodeID
}For a replication factor of N = 3:
| Configuration | Guarantees |
|---|---|
| W=2, R=2 | Strong consistency (W + R > N) |
| W=1, R=1 | Eventual consistency (faster, less safe) |
| W=3, R=1 | Write-heavy workload optimization |
| W=1, R=3 | Read-heavy workload optimization |
ββββββββββββββ¬ββββββββββββ¬ββββββββββ¬ββββββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββββ
β CRC32 β Timestamp β Key Len β Value Len β Deleted β Key β Value β
β (4 bytes) β (8 bytes) β(4 bytes)β (4 bytes) β(1 byte) β(N bytes)β(M bytes)β
ββββββββββββββ΄ββββββββββββ΄ββββββββββ΄ββββββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββββ
βββββββββββββββββββββββββββββββββββββββ
β β
βΌ β
βββββββββββ 5s timeout βββββββββββ β
β ALIVE βββββββββββββββββββΊβ SUSPECT β β
βββββββββββ βββββββββββ β
β² β β
β β 30s β
β βtimeout β
β Heartbeat βΌ β
β received ββββββββββ β
βββββββββββββββββββββββββ DEAD ββββββ
ββββββββββ
β
β Removed from
β hash ring
βΌ
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Clone your fork
git clone https://github.com/YOUR_USERNAME/Distributed-Key-Value-Store-with-Tunable-Consistency.git
cd Distributed-Key-Value-Store-with-Tunable-Consistency
# Install dev dependencies
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# Run linter
make lint
# Run tests before committing
make test-all- Follow standard Go conventions
- Run
go fmtbefore committing - Add tests for new functionality
- Update documentation as needed
This project is licensed under the MIT License - see the LICENSE file for details.
- Amazon Dynamo Paper - The foundational research that inspired this project
- Bitcask Paper - Storage engine design inspiration
- Consistent Hashing - Karger et al.'s seminal work
- SWIM Protocol - Gossip-based failure detection