Introduction
Distributed Cache is a lightweight, in-memory cache system built from scratch in TypeScript. It demonstrates core concepts used in production caches like Redis, Memcached, and Amazon DynamoDB.
What is it?
Client → Consistent Hashing → Cache Node (RAM)
├── Node 0: keys A-F
├── Node 1: keys G-M
└── Node 2: keys N-ZA distributed cache stores frequently accessed data in memory (RAM) instead of reading from a database every time. This makes reads 100-5000x faster.
Features
| Feature | Description |
|---|---|
| Consistent Hashing | Even data distribution with minimal redistribution |
| Eviction (LRU/LFU/FIFO) | Automatically remove old entries when cache is full |
| TTL (Time To Live) | Auto-expire entries after a set time |
| Replication | Copy data to backup nodes for fault tolerance |
| Cache Invalidation | Wildcard-based cleanup when source data changes |
| Cluster Management | Leader election + automatic failover |
| TCP Protocol | Low-latency network communication |
| Persistence | Optional file-based storage |
Performance
Throughput: 120,000+ ops/sec (in-memory)
Latency: <0.01ms avg, <0.03ms p99
Distribution: 36/35/29% across 3 nodes (even)When to use this?
Learning distributed systems:
- Understand how Redis/Memcached work internally
- Learn consistent hashing, replication, and eviction
Building e-commerce apps:
- Cache product catalog, user sessions, shopping carts
- Integrated with Medusa.js (real benchmark: 120K ops/sec)