Caching Fundamentals
A cache stores copies of frequently accessed data in a faster storage layer.
Why Cache?
- Reduce latency: Memory access is ~100x faster than disk
- Reduce load: Fewer requests hit your database
- Improve throughput: Serve more requests with the same resources
Cache Strategies
Cache-Aside (Lazy Loading)
- Check cache for data
- On miss, fetch from database
- Store in cache, return to client
Pros: Only caches what's needed Cons: Cache miss penalty, potential stale data
Write-Through
- Write to cache AND database simultaneously
- Reads always come from cache
Pros: Cache always consistent Cons: Write latency, caches unused data
Write-Behind (Write-Back)
- Write to cache only
- Asynchronously persist to database
Pros: Fast writes Cons: Risk of data loss if cache fails
Eviction Policies
When the cache is full, which item do we remove?
| Policy | Description | Use Case |
|---|---|---|
| LRU | Least Recently Used | General purpose |
| LFU | Least Frequently Used | Hot/cold data patterns |
| FIFO | First In, First Out | Simple, predictable |
| TTL | Time To Live | Data with known staleness |
Cache Levels
- L1/L2/L3 CPU Cache: Hardware level, nanoseconds
- Application Cache: In-process memory (e.g., HashMap)
- Distributed Cache: Redis, Memcached — shared across servers
- CDN Cache: Geographic distribution for static assets