Distributed Cache
Most engineers use Redis without understanding what makes it hard to build correctly: concurrent access requires careful lock discipline, TTL expiration needs a background worker that doesn't block reads, and LRU eviction requires an O(1) structure that survives concurrent writes. This project implements all of this from scratch — TCP server, RESP-compatible protocol, 256-shard mutex concurrency, O(1) LRU via doubly-linked list + hashmap, write-ahead log persistence, and a background TTL expiry worker. The point isn't to replace Redis; it's to understand exactly how it works.
Built a Redis-equivalent distributed cache from scratch in C++
- ›TCP server, RESP protocol, LRU eviction, TTL expiration, WAL persistence — all from scratch
- ›Thread-safe concurrent access with 256-shard mutex design — no global lock
- ›12 RESP-compatible commands, epoll event loop, configurable thread pool
A Redis-inspired distributed key-value store built from scratch in C++. Implements TCP server with RESP protocol, 256-shard mutex concurrency, O(1) LRU eviction (doubly-linked list + hashmap), lazy TTL expiry with background worker, and write-ahead log persistence. A serious study in how distributed caching actually works at the implementation level — every design decision is explicit.
Each claim is paired with evidence that a recruiter can request or verify.
Metrics marked with context indicate the measurement scope (local benchmark, prototype, qualitative estimate, etc.).
Understanding distributed caching at the implementation level — not just the API level. Most engineers know how to use Redis but not how LRU eviction, TTL expiration, and concurrent access actually work together. This project builds it from scratch in C++ to close that gap: every design decision is made explicitly rather than inherited from a library.
- No external cache libraries — implement all data structures from scratch
- Thread-safe concurrent access without a global lock (global locking serializes all requests)
- LRU eviction must be O(1) — not O(log n) with a heap, not O(n) with an array
- TTL expiration must not block read/write operations — background worker with lazy expiry on access
TCP server accepts connections on a configurable port, parses RESP commands from the wire, and dispatches to the cache layer. Cache: 256 shards, each with its own mutex and LRU doubly-linked list + unordered_map. Key assignment: hash(key) % 256. LRU: doubly-linked list (head = most recently used, tail = eviction candidate) + hashmap(key → list_node). O(1) get: lookup in map, move node to head. O(1) set: create node at head, evict tail if over capacity. TTL: stored per-key as expiry_time; background thread scans expired keys every 100ms; lazy expiry on GET checks timestamp. Persistence: write-ahead log (append-only) + periodic full snapshots. Thread pool: 8 worker threads, queue-based task dispatch.