A cache stores frequently accessed data in memory for speed, reducing latency and load on origin systems.
Cache Invalidation Strategies
- TTL (Time-to-Live): Data expires after a set duration
- Write-through: Data is written to cache and database simultaneously
- Write-behind: Data is written to cache first, then asynchronously to database
- Explicit invalidation: Cache is manually cleared when data changes
Common Caching Patterns
- Cache-aside: Application checks cache first, then database if miss
- Read-through: Cache automatically loads data from database on miss
- Refresh-ahead: Proactively refresh cache before expiration
CDN (Content Delivery Network)
A specialized form of caching that stores static content geographically closer to users, reducing latency and load on origin servers.
Benefits
- Reduced latency for global users
- Lower bandwidth costs
- DDoS protection
- Improved availability
Considerations
Cache invalidation is notoriously difficult - “There are only two hard things in Computer Science: cache invalidation and naming things.” - Phil Karlton