Lesson 4 � Intermediate

Caching Strategies

Caching matlab frequently accessed data ko fast storage mein rakhna taaki baar baar database na jaana pade. Ye system ki performance ka game changer hai.

? 20 min✓ Intermediate✓ Load balancing basics

Caching hota kya hai?

WHAT

Caching ek technique hai jisme hum frequently accessed data ko fast storage (memory) mein store karte hain. Agar same data baar baar chahiye toh slow database se na leke fast cache se le lo. Response time 100ms se 1ms ho jaata hai.

WHEN

Jab data frequently access ho raha ho (read-heavy), jab database slow ho, jab response time critical ho. Facebook feed, Twitter timeline, product catalog � sab caching use karte hain.

WHERE

Redis, Memcached, CDN (Cloudflare, CloudFront), Browser cache, Application-level cache � har layer pe caching hoti hai.

# Without Cache: Slow
User Request ✓ API Server ✓ Database (200ms) ✓ Response
Total: 200ms

# With Cache: Fast
User Request ✓ API Server ✓ Cache (1ms) ✓ Response
Total: 1ms

# Cache Hit: Data cache mein mila ✓ Fast response
# Cache Miss: Data cache mein nahi ✓ Database se lo, cache mein store karo

# Real numbers:
- Database read: 5-200ms
- Redis read: 0.1-1ms
- Cache hit ratio target: >95%
Mental Model: Caching jaise fridge mein rakhna. Agar tumhe baar baar chai chahiye toh kitchen mein bana ke fridge mein rakh lo. Jab chahiye fridge se nikal lo � baar baar kitchen mein jaake mat banao. Fridge = Cache, Kitchen = Database.

Caching Patterns

# 1. Cache-Aside (Lazy Loading) � Most Common

Application pehle cache check karta hai:
- Cache HIT ✓ Direct cache se data lo
- Cache MISS ✓ Database se lo ✓ Cache mein store karo

# Pros: Only requested data cache hota hai
# Cons: First request slow (cache miss)
# Use: General purpose, read-heavy apps

# 2. Write-Through

Har write operation ke saath cache bhi update:
- Write ✓ Cache + Database dono update
- Read ✓ Cache se seedha

# Pros: Data always consistent
# Cons: Write slow (donon jagah write)
# Use: Session storage, user preferences

# 3. Write-Behind (Write-Back)

Write sirf cache mein, database baad mein:
- Write ✓ Cache update ✓ Database baad mein (async)
- Read ✓ Cache se

# Pros: Write bahut fast
# Cons: Data loss risk (cache crash)
# Use: Analytics, logs, non-critical data

# 4. Read-Through

Cache khud database se data fetch karta hai:
- Application sirf cache se baat karta hai
- Cache internally database se data laata hai

# Pros: Application code simple
# Cons: Cache layer complex
# Use: Managed caching services

Caching Tools

# Redis vs Memcached

| Feature | Redis | Memcached |
|----------------|--------------------------|------------------------|
| Data Types | Strings, Lists, Sets, | Strings only |
| | Hashes, Sorted Sets | |
| Persistence | Yes (RDB/AOF) | No |
| Pub/Sub | Yes | No |
| Clustering | Yes (built-in) | Yes (client-side) |
| Memory | More efficient | Less efficient |
| Use Case | Session, leaderboards, | Simple caching, |
| | real-time analytics | page caching |

# When to use what?
- Simple key-value caching ✓ Memcached
- Complex data + persistence ✓ Redis
- Both are great for most use cases

CDN Caching

CDN (Content Delivery Network) static content ko cache karta hai globally distributed servers pe. User ko nearest server se data milta hai.

# CDN Flow

User (India) ✓ Request for image.jpg
CDN Edge Server (Mumbai) ✓ Cache mein hai? 
 YES ✓ Seedha return (10ms)
 NO ✓ Origin server se fetch (200ms) ✓ Cache mein store ✓ Return

# What to cache in CDN?
- Images, CSS, JS files (static)
- Video thumbnails
- API responses (GET requests)
- HTML pages (for blogs, news)

# CDN Providers:
- Cloudflare (free tier bhi hai)
- AWS CloudFront
- Akamai
- Fastly

# Cache Invalidation:
- TTL (Time To Live) ✓ Auto expire after X seconds
- Manual purge ✓ CDN dashboard se force refresh
- Versioning ✓ file?v=2 ✓ new URL = new cache
Key Insight: CDN caching sabse effective hai for static content. Ek image 1GB origin server se aati hai toh 200ms lagta hai. CDN se 10ms. Aur agar 10 million users access karein toh origin server overload hoga � CDN load le leta hai.

Cache Invalidation

Cache invalidation sabse mushkil problem hai in computer science. Data change ho gaya but cache mein purana data hai � stale data milega user ko.

# Cache Invalidation Strategies

1. TTL (Time To Live)
 ✓ Data X seconds mein expire ho jaayega
 ✓ Simple but data stale ho sakta hai
 ✓ Example: Redis SET key value EX 3600 (1 hour)

2. Event-Based Invalidation
 ✓ Data change hone pe event publish karo
 ✓ Cache listener sun ke cache delete karta hai
 ✓ Example: User profile update ✓ invalidate user cache

3. Version-Based
 ✓ Har data ke saath version number
 ✓ Cache miss pe version check karo
 ✓ Example: /api/users/123?v=2

4. Write-Through + Invalidation
 ✓ Write ke saath cache invalid karo
 ✓ Next read fresh data lega

# Famous quote: "There are only two hard things in CS:
# Cache invalidation and naming things."

Exercise

Question: Caching pattern jisme application pehle cache check karta hai aur miss hone pe database se data lake cache mein store karta hai � uska naam kya hai? (2 words)

Question: Production systems mein cache hit ratio kitna hona chahiye minimum? (percentage mein)

Common mistakes

Lesson complete?

Caching strategies samajh aa gayi✓ Ab Database Scaling seekhte hain � Sharding aur Replication kaise kaam karti hai.