Caching stores copies of frequently accessed data in fast storage (memory) so future requests are served faster. Getting caching right is often the difference between a system that scales and one that doesn't.
Database reads are slow (milliseconds). Memory reads are fast (microseconds). Caching exploits this 1,000x difference by storing hot data in RAM.
Benefits:
The application is responsible for populating the cache.
1. Request comes in
2. Check cache → Cache HIT → Return cached value
3. Cache MISS → Read from DB → Write to cache → Return value
Pros:
Cons:
Best for: General-purpose read caching. The default choice.
The application writes to the cache and the database simultaneously (synchronously).
1. Write to cache
2. Write to database
3. Return success
Pros:
Cons:
Best for: Systems that can't tolerate stale data, with frequent reads of recently written data.
The application writes to the cache immediately and asynchronously flushes to the database.
1. Write to cache
2. Return success immediately
3. [async] Flush to database in background
Pros:
Cons:
Best for: Write-heavy workloads where some data loss is acceptable (analytics, metrics).
The cache sits between the application and database. On a miss, the cache itself fetches from the DB.
Pros: Simpler application code (the cache handles DB reads) Cons: First request for any key always incurs the miss penalty
| Policy | How It Works | Best For | |---|---|---| | LRU (Least Recently Used) | Evict the item not used for the longest time | Most general-purpose workloads | | LFU (Least Frequently Used) | Evict the item used the fewest times | Workloads with stable popularity | | FIFO | Evict the oldest item added | Simple cases | | TTL | Evict items after a fixed time-to-live | Time-sensitive data |
Redis defaults to LRU when memory is full.
"There are only two hard things in Computer Science: cache invalidation and naming things." — Phil Karlton
Strategies for keeping the cache in sync:
When a popular cache entry expires, many requests simultaneously hit the database. Solutions:
A single cache key that gets millions of reads per second can overwhelm even a cache cluster. Solutions: