A cache is just a temp storage that stores recently used data so you can fetch Accessing data from disk takes 1ms Accessing data from cache takes 100 ns
Where to cache?
- External Caching - dedicated service like memcache or redis
- All app servers might share cache
- In-Process Caching
- Skips complexity of redis
- each app server has its own in memory cache
- ultra low latency matters like config data
- CDN
- geographic distribution of servers
- can do edge logic, media, html, videos
- client-side-caching
- http cache stored in memory on device
- downside is you have less control
- not often in interviews
- forte Cache Architecture
- Cache Aside
- Check Cache
- Read from Db as fallback
- keeps cache lean
- downside is that cache miss has latency
- most important
- Write Through
- cache writes to cache, then syncs write to db
- Write To db
- slower writes, pollute cache
- dual write problem
- less common
- Write behind
- Write to cache, then async flush to db
- if cache were to crash, big problems
- useful for analytics or where data loss is acceptable
- Read through
- Check cache
- if we miss cache requests data from db
- cache acts as proxy
Cache Eviction Policies
- LRU - evict items that havent been used recently. Most common and balanced
- LFU - evict items that are used least often
- FIFO - evict oldest item first, simple but rarely right choice
- TTL - each time expires after a set time
Common Issues/ Deep Dives
- Cache Stampede (Thundering Herd)
- When mutliple request try to build cache key, only one should work
- cache warming. instead of waiting for popular keys to expire, proactively refresh them
- Cache Consistency (most common)
- no perfect fix
- invalidate on write (when value is updated, then update it in cache)
- use short ttls
- just accept eventual consistency
- Hot keys
- Taylor Swift key could be receiving lots of reads
- Add instances of caches and shard data across them
- then app server can load balance between the caches
- Add fallback cache
Wrap Up
- When Should we bring it up
- read heavy workload
- expensive queries
- high db cpu
- latency requirements
- How to introduce caching
- identify bottleneck
- decide what to cache
- choose your cache architecture
- set an eviction policy
- address the downsides