HARDData Storagestaff engineer

Design a Distributed Cache

Design a distributed caching system like Redis or Memcached that can handle millions of requests per second.

Estimated Time: 45 minutes
#caching#distributed-systems#consistent-hashing#lru
Solution Overview

Use consistent hashing for distribution, LRU eviction, and replication for availability.

Hints to Get Started
1

Research consistent hashing

2

How do you handle hot keys?

3

Consider cache invalidation strategies

Requirements

functional

  • GET/SET/DELETE operations
  • TTL support
  • Atomic operations
  • Pub/Sub (optional)

non functional

  • Sub-millisecond latency
  • High throughput
  • Horizontal scalability
  • Fault tolerance
System Architecture Diagram

Architecture diagram visualization

(Diagram generation coming soon)

client

Smart client with consistent hashing

server

In-memory hash table with eviction

cluster

Gossip protocol for membership

Deep Dives

hot keys

Replicate hot keys to multiple nodes

thundering herd

Probabilistic early expiration, locking

memory management

Slab allocation, avoid fragmentation

Replication

strategy

Replicate to N successor nodes on ring

consistency

Async replication for performance, sync option for consistency

Distribution

virtual nodes

Each physical node owns multiple positions on ring

consistent hashing

benefits

  • Minimal redistribution on node change
  • Even distribution with virtual nodes

description

Hash keys to virtual ring, assign ranges to nodes

Eviction Policies

LFU

Least Frequently Used - track access count

LRU

Least Recently Used - track access time

TTL

Time-based expiration