Skip to content

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-Z

A distributed cache stores frequently accessed data in memory (RAM) instead of reading from a database every time. This makes reads 100-5000x faster.

Features

FeatureDescription
Consistent HashingEven 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
ReplicationCopy data to backup nodes for fault tolerance
Cache InvalidationWildcard-based cleanup when source data changes
Cluster ManagementLeader election + automatic failover
TCP ProtocolLow-latency network communication
PersistenceOptional 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)

Released under the MIT License.