Skip to content

About

A distributed in-memory key-value cache built in Go, featuring TCP-based communication, TTL expiration, LRU eviction, persistence through Write-Ahead Logging (WAL), consistent hashing for sharding, replication, heartbeat-based failure detection, and automatic key rebalancing.

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

Distributed KV Cache

A high-performance, distributed in-memory key-value cache built in Go. Features include consistent hashing, automatic replication, crash recovery, and real-time monitoring.

What is this?

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

Quick Start

Single Node

# Build
go build -o main.exe ./tcp/main.go

# Run
main.exe

# Connect and test
nc localhost 9000
PING
SET mykey 5
hello
GET mykey

3-Node Cluster

cd test
start_cluster.bat

Starts nodes on ports 5000, 5001, 5002 with automatic load balancing.

Commands

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

Command Examples

# 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%

Configuration

Environment Variables

# Cluster mode
set CLUSTER_NODE_ID=node1
set CLUSTER_ROUTING=true

# Observability
set METRICS_ADDR=:8080
set LOG_LEVEL=INFO
set LOG_FORMAT=json

Cluster Config (cluster/cluster.json)

{
    "virtual_nodes": 100,
    "nodes": [
        {"id": "node1", "address": "localhost:5000"},
        {"id": "node2", "address": "localhost:5001"},
        {"id": "node3", "address": "localhost:5002"}
    ]
}

Monitoring

HTTP Metrics

# View metrics
curl http://localhost:8080/metrics

# Health check
curl http://localhost:8080/health

Returns JSON with request rates, latency, cache stats, and memory usage.

Client Examples

Go

conn, _ := net.Dial("tcp", "localhost:5000")
fmt.Fprintln(conn, "SET mykey 5")
fmt.Fprintln(conn, "hello")
// Read response...

Python

import socket
conn = socket.socket()
conn.connect(('localhost', 5000))
conn.sendall(b"SET mykey 5\nhello\n")
print(conn.recv(1024))  # OK

Bash

echo -e "SET mykey 5\nhello" | nc localhost 5000
echo "GET mykey" | nc localhost 5000

Included Client

cd client
go build -o client.exe simple_client.go
client.exe  # Interactive mode

Benchmarking

cd benchmark
go build -o benchmark.exe benchmark.go
benchmark.exe -clients 10 -duration 30 -read 80 -write 15 -delete 5

Example output:

Throughput:      4,159 ops/sec
Latency Average: 120.45 µs
Success Rate:    99.9%

Features

Core

  • Thread-safe in-memory storage
  • LRU eviction (configurable limit: 512MB, 100 keys)
  • TTL expiration with background cleanup
  • Statistics tracking (hits, misses, evictions)

Distribution

  • Consistent hashing with 100 virtual nodes per physical node
  • Automatic request forwarding to correct node
  • Connection pooling for inter-node communication

Reliability

  • 2x replication across nodes
  • Heartbeat monitoring (5s interval)
  • Automatic failover when nodes fail
  • Key rebalancing after topology changes

Persistence

  • Write-Ahead Log (WAL) for all writes
  • Periodic snapshots for fast recovery
  • Automatic replay on startup

Observability

  • HTTP metrics endpoint (request rates, latency, cache stats)
  • Structured logging (JSON or text format)
  • Benchmark tool for load testing

Performance

  • Throughput: 50,000+ ops/sec (single node, 80% reads)
  • Latency: 50-200 µs for local operations
  • Scalability: Linear read scaling with cluster size

Architecture

Client → Node 1 (Hash Ring) → Forward to Node 2 (owns key)
                             ↘ Replicate to Node 3

Each key is:

  1. Hashed to determine owner node
  2. Stored on primary node
  3. Replicated to 1 additional node
  4. Accessible from any node (automatic forwarding)

Project Structure

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

Requirements

  • Go 1.16+
  • Optional: netcat (nc) for testing, curl for metrics

Testing

# 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

Use Cases

  • 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

About

A distributed in-memory key-value cache built in Go, featuring TCP-based communication, TTL expiration, LRU eviction, persistence through Write-Ahead Logging (WAL), consistent hashing for sharding, replication, heartbeat-based failure detection, and automatic key rebalancing.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages