CacheDist
Distributed In-Memory Key-Value Cache Engine in Go

Project at a Glance
- Problem: Understanding how production distributed caches like Redis handle concurrency, horizontal key distribution, memory eviction, replication, and node failures requires going beyond using them as black-box dependencies.
- Deliverables: A distributed in-memory key-value cache engine built from scratch in Go 1.22+, featuring 256-shard concurrent storage, consistent hashing with virtual nodes, O(1) LRU eviction, lazy + active TTL expiration, synchronous gRPC + Protobuf replication with write quorum, heartbeat health checks with auto-recovery, a Client SDK, HTTP/JSON API, CLI, and Prometheus + Grafana observability running in a 3-node Docker Compose cluster.
- Core Stack: Go 1.22+, gRPC, Protobuf, HTTP/JSON, Prometheus, Grafana, Docker / Docker Compose.
- Practical Value: Built solo in 2026 as an open-source systems engineering study (explicitly scoped as a learning/portfolio project rather than production-ready software), validated with Go microbenchmarks, a 100,000-operation 3-node distributed load test, and a live node-failure chaos test.
Project Overview
CacheDist is a distributed in-memory key-value cache built from scratch in Go, inspired by Redis internals. Its primary goal is hands-on mastery of core distributed systems primitives: partitioning keys across nodes without global reshuffling, eliminating single-mutex lock contention under parallel load, enforcing synchronous write durability via quorum, and recovering automatically when a cluster node drops offline.
The repository includes a multi-node cache server (cmd/server), a manual testing CLI (cmd/cli), a consistent-hashing Client SDK (sdk/), Protobuf definitions (proto/), and a ready-to-run 3-node Docker Compose stack bundled with Prometheus scraping and a pre-configured Grafana dashboard.
Interface Specimen

Pre-configured Grafana Dashboard: Real-time observability tracking cache hits, latency histograms, eviction counts, and memory usage across the 3-node cluster during distributed load testing.
System Architecture
+-----------------------------------------------------+
| CLIENT APP |
+--------------------------+--------------------------+
|
v
+-----------------------------------------------------+
| CACHE CLIENT SDK |
| Consistent Hash Ring Routing |
+----------+---------------+---------------+----------+
| | |
v v v
+-----------+ +-----------+ +-----------+
| NodeA | | NodeB | | NodeC |
| :7001 |<->| :7002 |<->| :7003 |
| 256 shards| | 256 shards| | 256 shards|
+-----------+ +-----------+ +-----------+
^ ^ ^
+---------------+---------------+
gRPC inter-node (port 800x)
- HTTP/JSON Client API (
:700x): Exposes/get,/set,/delete,/stats, and/metricsfor direct integration,curlinspection, and Prometheus scraping. - gRPC + Protobuf Inter-Node Mesh (
:800x): Handles low-overhead binary RPCs for synchronous write replication and peer-to-peer cluster coordination. - Consistent Hash Ring (
internal/cluster&sdk/): Maps keys to nodes using FNV-1a hashing with virtual nodes so adding or removing a node only relocates a minimal subset of the keyspace.
Core Storage & Concurrency Engine
- 256-Shard Parallel Storage (
internal/cache):- Early iterations with a single global mutex suffered from severe lock contention under concurrent goroutines.
- Partitioning each node into 256 internal shards (
h % numShards), each guarded by its ownsync.RWMutex, allows concurrent reads and writes across different shards to execute in parallel without blocking one another.
- O(1) LRU Eviction:
- Implemented with a custom doubly linked list paired with a hashmap.
- Every
GETpromotes the accessed entry to theHEAD, while capacity overflow evicts directly from theTAILin constant time.
- Dual-Strategy TTL Expiration:
- Supports per-key time-to-live (in seconds) using both lazy deletion on
GETreads and an active background cleanup goroutine to reclaim expired keys even if never read again.
- Supports per-key time-to-live (in seconds) using both lazy deletion on
Replication, Quorum & Fault Tolerance
- Synchronous gRPC Replication & Write Quorum: Writes are synchronously replicated across peers (replication factor:
2) before returning an acknowledgement to the client. A write succeeds only whenceil(replication_factor / 2) + 1nodes confirm (with replication factor 2, both primary and replica must ACK). - Health Checks & Auto-Recovery: Cluster nodes exchange heartbeats every 5 seconds. Unresponsive nodes are automatically removed from the active hash ring and re-integrated once they recover.
- Prometheus & Grafana Observability: Every node exposes hit rate, latency histograms, eviction counts, and memory usage at
/metrics, giving direct visibility into bottlenecks and failover behavior.
Benchmarks & Chaos Testing
All measurements below are recorded directly from the repository's benchmark and test suites:
Single-Node In-Process Microbenchmarks
Get(Parallel): ~7.3M ops/s (165.6 ns/op, 16 B/op)Set(Parallel): ~8.4M ops/s (153.7 ns/op, 111 B/op)Mixed (80/20): ~12.0M ops/s (100.5 ns/op, 34 B/op)
(Note: Single-node figures measure in-process engine performance without network syscalls or serialization overhead; real-world networked throughput is captured in the distributed cluster load test below.)
3-Node Docker Distributed Load Test
- Configuration: 3-node Docker cluster, replication factor
2, Client SDK with 10 concurrent goroutines. - Workload: 100,000 total operations (70%
GET, 20%SET, 10%DELETE). - Results: 1,163.47 ops/sec, 0.00% error rate, ~8.6ms average latency per request (including inter-node gRPC replication).
Chaos Test (Node Failure Resilience)
- Scenario: Active background traffic from 5 concurrent workers (4,756 total operations) while abruptly terminating a live node via
docker stop node2. - Results: 0.00% error rate while the node was down as the SDK automatically failed over to replicas, followed by automatic node re-join and resynchronization upon restart without manual intervention.
Key Engineering Takeaways
- Lock Contention Profiling: Moving from a single mutex to 256 sharded
RWMutexlocks demonstrated firsthand how concurrency bottlenecks hide until tested under parallel load. - Consistency vs. Latency Trade-Off: Enforcing synchronous write quorum guarantees replica durability before ACK, at the measurable cost of inter-node network round-trip latency on every write.
- Observability First: Instrumenting Prometheus histograms and Grafana dashboards turned opaque cluster stalls and failover timing into concrete, debuggable telemetry.
- Container Networking Realities: Running a multi-node cluster inside Docker exposed
127.0.0.1vs. container-hostname advertisement pitfalls, leading to cleaner peer discovery and address advertisement configuration.