1.Overview
A distributed cache stores frequently accessed data in memory across multiple servers to reduce database load and improve response latency.
Typical use cases:
Session storage
User profile caching
Product catalog
API response cache
Configuration data
Leaderboards
Rate limiting
2.Requirements
Functional
GET(key)
SET(key, value, TTL)
DELETE(key)
EXPIRE(key)
Batch operations (optional)
Non-functional
< 2 ms read latency
Millions of QPS
Horizontal scaling
Fault tolerance
High availability
Data expiration
Memory efficient
3. High Level Architecture
Client
|
+------------------+
| Cache Client SDK |
+------------------+
|
Consistent Hashing
+--------+--------+--------+
| Node A | Node B | Node C |
+--------+--------+--------+
Replication
+--------+--------+--------+
|Backup A|Backup B|Backup C|
+--------+--------+--------+
4.Components
a.Cache Client
Responsible for
hashing key
locating node
retry
connection pooling
serialization
Client-side routing avoids an extra proxy hop.
b.Cache Nodes
Each node stores
HashMap<Key, Entry>
Entry
struct Entry {
Value value
Timestamp expireAt
}
c.Metadata Service
Stores
cluster topology
node health
hash ring
replica mapping
Can use
etcd
ZooKeeper
Consul
Monitoring
Hit Rate
Miss Rate
Memory Usage
QPS
Evictions
Replication Lag
5.Partitioning
Consistent Hashing
Ring
NodeA
NodeB
NodeC
key -> nearest clockwise node
Benefits
- only small percentage of keys move when new node added
- horizontal scaling
- balanced distribution
6.Cache Eviction
Memory is limited.
Need eviction policy.
LRU
Least Recently Used
LFU
Least Frequently Used
TTL
Each key has: expireAt
Two methods.
Passive expiration
GET
↓
expired?
↓
delete
Cheap.
Active expiration
Background thread scans expired keys.
Every second
↓
scan
↓
remove
Redis combines both.
7.Cache Consistency
Cache Aside
Most common.
Read
Cache
↓
Miss
↓
Database
↓
Cache
↓
Return
Write
DB
↓
Delete Cache
Pros
Simple.
Cons
Temporary stale data.
Read Through
App
↓
Cache
↓
Database
Application never accesses DB directly.
Write Through
App
↓
Cache
↓
Database
Strong consistency.
Higher latency.
Write Behind
App
↓
Cache
↓
ACK
↓
Flush DB later
Fast.
Risk of data loss.
Top comments (0)