Status: User requested implementation kickoff on 2026-04-26. Plan approved for handoff to implementation agent.
Build a Redis-like, multithreaded in-memory cache engine in C++ with mixed burst workload handling, TTL, eviction, memory limits, metrics, persistence, replication, and pluggable serialization. Use a phased architecture: first a correct single-node core with strong concurrency design, then durability and replication, then operational hardening.
- Define API contract for Redis-like CLI commands for MVP (GET, SET, DEL, EXPIRE, TTL, MGET, MSET, INFO, SAVE) and expected error semantics.
- Define non-functional SLOs: p50/p99 latency targets, throughput target under burst traffic, memory ceiling behavior, replication lag targets.
- Finalize eviction policy strategy for mixed workload: start with LRU for predictable behavior, leave extension point for LFU.
- Partition keyspace into fixed shards (for example 64 or 128 shards) using consistent hash modulo to reduce lock contention.
- Per shard, design data structures: hash table for entries, intrusive/access list for LRU, min-heap or timing wheel index for TTL, and per-shard memory accounting.
- Define entry model: key bytes, value bytes/object, metadata (version, expiry timestamp, last-access tick, flags).
- Design serialization abstraction with interface boundary so value encoding can be swapped without touching cache core.
- Use a boss-worker networking model with epoll/kqueue/select abstraction for CLI connections and a bounded worker thread pool for command execution.
- Route each command to target shard(s); single-shard commands use shard mutex/shared_mutex, multi-key commands use deterministic lock ordering to avoid deadlock.
- Add backpressure and admission control: bounded request queue, per-connection limits, overload response path for burst protection.
- Implement background maintenance threads: expiration sweeper, eviction coordinator, snapshot writer, metrics aggregator.
- Enforce max memory with high/low watermarks.
- On memory pressure, trigger eviction loop by shard with fairness; eviction candidates selected via LRU (later optional LFU mode).
- TTL expiration strategy: lazy on read + active periodic sweep to prevent stale buildup.
- Add allocator/memory diagnostics hooks (allocation totals, fragmentation estimate, per-shard usage).
- Implement snapshotting first (point-in-time state dump) with copy-on-write aware coordination to avoid long request stalls.
- Add append-only log (AOF/WAL) for incremental durability; include replay ordering guarantees and checksum validation.
- Recovery path: load latest snapshot, replay WAL tail, validate key version monotonicity.
- Start with primary-replica asynchronous replication.
- Define replication stream protocol: command log with sequence numbers, heartbeat, ACK window.
- Implement initial full sync + incremental catch-up.
- Expose replication health metrics (lag bytes/time, sync status, last applied sequence).
- Metrics endpoint and INFO-like command: hit ratio, ops/sec, latency histograms, eviction/expiration rates, queue depth, worker utilization, memory by shard.
- Structured logging with correlation id per request.
- Admin controls: runtime config for limits, thread counts, sweep intervals.
- Unit tests: shard map correctness, TTL semantics, eviction ordering, serialization compatibility.
- Concurrency tests: race detection, deadlock checks, lock-order enforcement, correctness under parallel writers/readers.
- Fault tests: crash during snapshot/WAL flush, truncated WAL, replica disconnect/reconnect.
- Performance tests: sustained and burst load profiles, tail latency under memory pressure, recovery time benchmarks.
Relevant files
- No existing files in workspace yet; project skeleton and module layout will be created during implementation.
Verification
- Build and run unit/integration test suites on Linux and Windows.
- Execute benchmark scenarios for mixed burst traffic and compare against target SLOs.
- Run durability recovery drills (snapshot plus WAL restore) and validate data correctness.
- Run replication drills (full sync, lag, fail/reconnect) and validate eventual consistency targets.
- Run long soak test with memory cap and TTL churn; verify no unbounded growth and stable p99.
Decisions
- Implementation language: C++.
- Workload target: mixed traffic with bursts.
- Included capabilities: TTL, eviction, memory limit, metrics, persistence, replication, pluggable serialization.
- Access model: Redis-like CLI server interface.
- Recommended sequencing: single-node correctness and performance first, then persistence, then replication.
Further Considerations
- Network protocol scope for v1: strict RESP compatibility versus RESP-like custom subset. Recommendation: start with RESP-like subset for faster delivery, then add strict compatibility.
- Eviction policy choice for v1: LRU only versus runtime switch LRU/LFU. Recommendation: implement LRU first with a policy interface, add LFU after baseline stabilization.
- Durability default mode: snapshot-only versus snapshot plus WAL. Recommendation: ship snapshot first for simplicity, then enable WAL for stronger recovery point objective.