1️⃣ What is Caching?
- Store frequently accessed data in a faster storage layer.
- Reduces:
- Database load
- Latency
- Expensive recomputation
- Usually uses:
- RAM (
Redis,Memcached) - CDN
- Local memory
- RAM (
📌 Memory access is ~10,000x faster than disk.
2️⃣ Why Use Cache?
Use caching when:
- System is read-heavy
- Queries are expensive
- Low latency is required
- Database CPU/load is high
Example:
- Newsfeed generation
- User profiles
- Trending posts
- API responses
3️⃣ Types of Caching
| Type | Description | Use Case |
|---|---|---|
| External Cache | Separate cache server (Redis) | Most common |
| In-Process Cache | Inside app memory | Ultra low latency |
| CDN Cache | Edge servers near users | Images/videos |
| Client Cache | Browser/mobile cache | Offline support |
4️⃣ Cache Architectures
A. Cache Aside (Most Important ⭐)
Flow:
- Check cache
- Cache miss → DB
- Store in cache
- Return response
✅ Most common in interviews.
B. Write Through
- Write to cache + DB together.
✅ Fresh reads
❌ Slower writes
C. Write Behind / Write Back
- Write to cache first
- DB updated asynchronously later
✅ Very fast writes
❌ Possible data loss
Used in:
- Analytics
- Logging systems
D. Read Through
- Cache itself fetches from DB on miss.
Mostly used in:
- CDN
- Edge caching
5️⃣ Cache Eviction Policies
| Policy | Meaning |
|---|---|
| LRU | Remove least recently used |
| LFU | Remove least frequently used |
| FIFO | Remove oldest item |
| TTL | Expire after fixed time |
📌 Most common:
LRUTTL
6️⃣ Important Cache Problems
A. Cache Stampede / Thundering Herd
Many requests hit DB after cache expiry.
✅ Solutions:
- Request coalescing / single flight
- Cache warming
- Randomized TTL
B. Cache Consistency
Cache and DB contain different values.
✅ Solutions:
- Invalidate cache on write
- Short TTL
- Eventual consistency
C. Hot Keys
One cache key receives huge traffic.
Example:
- Celebrity profile
- Trending post
✅ Solutions:
- Replicate hot keys
- Local in-memory cache
- Load balancing
7️⃣ Common Tools
| Tool | Purpose |
|---|---|
| Redis | Distributed cache |
| Memcached | Simple cache |
| Cloudflare | CDN |
| Amazon CloudFront | CDN |
8️⃣ What to Mention in Interviews
Always Explain:
- Why cache is needed
- What data is cached
- Cache key
- TTL / eviction policy
- Read/write flow
- Consistency handling
- Stampede handling
9️⃣ Interview Default Recommendation
Default Stack
- Cache Type → External cache
- Tool →
Redis - Pattern → Cache Aside
- Eviction →
LRU + TTL
This works for most interviews. 🚀
🔟 Golden Rule
“Don’t add cache blindly.”
Always justify:
- What bottleneck exists?
- How cache solves it?
- What tradeoffs are introduced?
Quick Revision
| Concept | Default Choice |
|---|---|
| Cache Server | Redis |
| Pattern | Cache Aside |
| Eviction | LRU + TTL |
| Main Goal | Faster reads |
| Biggest Problems | Stale data, stampede, hot keys |