A high-performance, distributed in-memory key-value cache built in Go. Features include consistent hashing, automatic replication, crash recovery, and real-time monitoring.
This is a distributed cache system similar to Redis or Memcached. It stores key-value pairs in memory across multiple nodes with:
- Automatic data distribution using consistent hashing
- Fault tolerance through 2x replication
- Crash recovery with write-ahead logs and snapshots
- Built-in monitoring via HTTP metrics endpoint
# Build
go build -o main.exe ./tcp/main.go
# Run
main.exe
# Connect and test
nc localhost 9000
PING
SET mykey 5
hello
GET mykeycd test
start_cluster.batStarts nodes on ports 5000, 5001, 5002 with automatic load balancing.
| Command | Usage | Example |
|---|---|---|
SET |
SET key length [EX seconds] |
SET mykey 5 then hello |
GET |
GET key |
GET mykey |
DELETE |
DELETE key |
DELETE mykey |
PING |
PING |
Returns PONG |
STATUS |
STATUS |
Shows cluster health |
STATS |
STATS |
Shows cache statistics |
# Set a key
SET mykey 11
hello world
> OK
# Set with TTL (expires in 60 seconds)
SET tempkey 4 EX 60
data
> OK
# Get a key
GET mykey
> hello world
# Delete a key
DELETE mykey
> OK
# Check cluster status
STATUS
> NODES:3 ALIVE:3 FAILED:0
# View cache stats
STATS
> KEYS:42 MEM:1048576/536870912 HITS:1250 MISSES:50 RATE:96.2%# Cluster mode
set CLUSTER_NODE_ID=node1
set CLUSTER_ROUTING=true
# Observability
set METRICS_ADDR=:8080
set LOG_LEVEL=INFO
set LOG_FORMAT=json{
"virtual_nodes": 100,
"nodes": [
{"id": "node1", "address": "localhost:5000"},
{"id": "node2", "address": "localhost:5001"},
{"id": "node3", "address": "localhost:5002"}
]
}# View metrics
curl http://localhost:8080/metrics
# Health check
curl http://localhost:8080/healthReturns JSON with request rates, latency, cache stats, and memory usage.
conn, _ := net.Dial("tcp", "localhost:5000")
fmt.Fprintln(conn, "SET mykey 5")
fmt.Fprintln(conn, "hello")
// Read response...import socket
conn = socket.socket()
conn.connect(('localhost', 5000))
conn.sendall(b"SET mykey 5\nhello\n")
print(conn.recv(1024)) # OKecho -e "SET mykey 5\nhello" | nc localhost 5000
echo "GET mykey" | nc localhost 5000cd client
go build -o client.exe simple_client.go
client.exe # Interactive modecd benchmark
go build -o benchmark.exe benchmark.go
benchmark.exe -clients 10 -duration 30 -read 80 -write 15 -delete 5Example output:
Throughput: 4,159 ops/sec
Latency Average: 120.45 µs
Success Rate: 99.9%
- Thread-safe in-memory storage
- LRU eviction (configurable limit: 512MB, 100 keys)
- TTL expiration with background cleanup
- Statistics tracking (hits, misses, evictions)
- Consistent hashing with 100 virtual nodes per physical node
- Automatic request forwarding to correct node
- Connection pooling for inter-node communication
- 2x replication across nodes
- Heartbeat monitoring (5s interval)
- Automatic failover when nodes fail
- Key rebalancing after topology changes
- Write-Ahead Log (WAL) for all writes
- Periodic snapshots for fast recovery
- Automatic replay on startup
- HTTP metrics endpoint (request rates, latency, cache stats)
- Structured logging (JSON or text format)
- Benchmark tool for load testing
- Throughput: 50,000+ ops/sec (single node, 80% reads)
- Latency: 50-200 µs for local operations
- Scalability: Linear read scaling with cluster size
Client → Node 1 (Hash Ring) → Forward to Node 2 (owns key)
↘ Replicate to Node 3
Each key is:
- Hashed to determine owner node
- Stored on primary node
- Replicated to 1 additional node
- Accessible from any node (automatic forwarding)
distributed_kv/
├── tcp/main.go # Main server
├── storage/ # Cache with LRU/TTL
├── parser/ # Command parser
├── persistence/ # WAL & snapshots
├── cluster/ # Distribution & replication
├── observability/ # Metrics & logging
├── benchmark/ # Load testing tool
└── test/ # Test scripts
- Go 1.16+
- Optional: netcat (nc) for testing, curl for metrics
# Unit tests
go test ./...
# Integration tests
cd test
go run test_phase6.go # Test replication/failover
test_phase7.bat # Test metrics/logging
full_system_test.bat # Complete system test- Session storage with automatic expiration
- API response caching to reduce database load
- Rate limiting with distributed counters
- Distributed locks for service coordination
- Real-time analytics with high-speed aggregation