Caching Strategies
Caching is the process of storing copies of files or data in a temporary storage location (cache) so that they can be accessed more quickly. It is one of the most effective ways to improve the performance and scalability of a system.
1. Where to Cache?
Caching can happen at multiple layers of an application stack.
Client-Side Caching (Browser)
Browsers store static assets (images, CSS, JS) locally to avoid downloading them on every page visit. Controlled via HTTP headers like Cache-Control and ETag.
CDN (Content Delivery Network)
A CDN is a network of geographically distributed servers. It caches static content closer to the user to reduce latency.
- Example: Cloudflare, AWS CloudFront.
- Use Case: Serving images, videos, and frontend bundles.
Application Server Caching
Storing the result of expensive computations or database queries in memory (RAM).
- Tools: Redis, Memcached.
- Use Case: Storing user sessions, API responses, or frequently accessed database records.
Database Caching
Databases often have their own internal buffers to cache frequently accessed rows or index pages in memory.
2. Caching Patterns
Cache-Aside (Lazy Loading)
The application code is responsible for loading data into the cache.
- App checks Cache.
- If Hit: Return data.
- If Miss: App queries Database -> Updates Cache -> Returns data.
- Pros: Only requested data is cached.
- Cons: First request is slow (cache miss).
Write-Through
Data is written to the cache and the database simultaneously.
- Pros: Data in cache is always fresh.
- Cons: Slower writes; cache might contain data that is never read.
Write-Back (Write-Behind)
Data is written only to the cache initially, and then asynchronously written to the database later.
- Pros: Very fast writes.
- Cons: Risk of data loss if the cache crashes before syncing to the DB.
3. Cache Eviction Policies
When the cache is full, how do we decide what to delete?
- LRU (Least Recently Used): Discard items that haven't been used for the longest time. (Most common).
- LFU (Least Frequently Used): Discard items that are used least often.
- FIFO (First In First Out): Discard the oldest items first.
- TTL (Time To Live): Items expire automatically after a set time (e.g., 1 hour).
4. The Hardest Problem: Invalidation
"There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton
Deciding when to remove or update data in the cache is difficult. If you update a user's profile in the DB but forget to update the cache, the user sees old data.
programming/distributed-systems programming/database-basics programming/load-balancing