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

📌 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

TypeDescriptionUse Case
External CacheSeparate cache server (Redis)Most common
In-Process CacheInside app memoryUltra low latency
CDN CacheEdge servers near usersImages/videos
Client CacheBrowser/mobile cacheOffline support

4️⃣ Cache Architectures

A. Cache Aside (Most Important ⭐)

Flow:

  1. Check cache
  2. Cache miss → DB
  3. Store in cache
  4. 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

PolicyMeaning
LRURemove least recently used
LFURemove least frequently used
FIFORemove oldest item
TTLExpire after fixed time

📌 Most common:

  • LRU
  • TTL

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

ToolPurpose
RedisDistributed cache
MemcachedSimple cache
CloudflareCDN
Amazon CloudFrontCDN

8️⃣ What to Mention in Interviews

Always Explain:

  1. Why cache is needed
  2. What data is cached
  3. Cache key
  4. TTL / eviction policy
  5. Read/write flow
  6. Consistency handling
  7. 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

ConceptDefault Choice
Cache ServerRedis
PatternCache Aside
EvictionLRU + TTL
Main GoalFaster reads
Biggest ProblemsStale data, stampede, hot keys