Skip to the content.

What is Caching?

Caching = Storing frequently used data in fast storage (RAM, etc.) β†’ So that future requests can be served faster, without recomputing or re-fetching from slow source.


Simple real-life analogy

πŸ”Ή You Google β€œWeather Delhi” β†’ Google caches result in memory β†’ Next person asking gets instant answer (no recompute)


How caching helps in system design?

Benefit How it helps
⚑ Speed up responses Data served from RAM / nearby cache β†’ ms instead of seconds
πŸ”₯ Reduce load on DB Less hits to DB / backend β†’ saves cost, improves scalability
🌍 Scale better Allows handling more users with same infra
πŸ’Έ Cost savings Less DB queries β†’ saves compute + bandwidth

Where can we cache? (Types)

Cache type Example
Browser cache Images, CSS, JS
CDN cache Static files (Cloudflare, Akamai)
Application cache In-memory (Redis, Memcached)
Database cache Query result caching

Best practices of caching βœ…

Best practice Why?
Cache only hot data Caching everything wastes memory
Set TTL (expiry) Prevent stale data
Invalidate/update cache when source changes Keep data fresh
Consider consistency needs For critical apps, don’t serve outdated data
Use cache-aside pattern Fetch from DB when cache miss, then update cache
Limit cache size Avoid memory explosion (use LRU β€” Least Recently Used eviction)

When NOT to use cache unnecessarily ❌

Don’t cache if… Why?
Data changes too frequently Cache will be invalidated constantly β†’ No benefit
Data is rarely accessed again Caching wastes memory
You need very strong consistency Cache might show old data (bad for banking etc.)
Memory/storage cost > performance benefit For small apps, caching infra cost may not be worth it

Quick interview summary (you can say this)

Caching = fast storage for frequent data. Speeds up reads, reduces load, scales system. Use wisely β†’ only hot, not too-changing data, with expiry + invalidation. Avoid over-caching or stale data risk.


Tiny real-world example

πŸ”Ή E-commerce site β†’ Product details β†’ Cache top 100 most viewed products in Redis (refresh every 10 mins)

βœ… Faster load βœ… Less DB hit βœ… Good trade-off (products don’t change every second)


Common caching patterns πŸ‘‡

1️⃣ Cache-aside (Lazy loading)

Most common & easiest pattern

How it works:

Step Action
1 Check cache for product:123
2 Not found β†’ Fetch from DB
3 Save result to cache
4 Return result to user

πŸ“Œ Example (E-commerce)

// Pseudo code
if (cache.has(productId)) return cache.get(productId);
else {
   product = db.get(productId);
   cache.set(productId, product);
   return product;
}

βœ… Simple βœ… Cache only when needed ❌ First user waits (cold start)


2️⃣ Write-through

How it works:

Step Action
1 Save product to DB
2 Immediately save to cache too

πŸ“Œ Example (User profile update)

db.save(profile);
cache.set(userId, profile);

βœ… Cache always fresh ❌ Writes are slower (2 writes: DB + cache)


3️⃣ Write-back (Write-behind)

How it works:

Step Action
1 Save to cache
2 Cache queues/batches DB writes

πŸ“Œ Example (Analytics counters)

βœ… Very fast writes ❌ Risk of data loss if cache crashes before sync


4️⃣ Read-through

How it works:

Step Action
1 App asks cache
2 Cache checks + fetches DB on miss

πŸ“Œ Example (Transparent caching)

βœ… Simplifies code ❌ More complex cache config


Quick comparison table

Pattern When to use? Pro Con
Cache-aside Common fetch-heavy apps Simple First access slow
Write-through Data needs to be fresh Always synced Slower writes
Write-back High write apps (analytics) Fast writes Risky (data loss)
Read-through Transparent cache mgmt Simple usage Complex setup

In short

Cache-aside = fetch + cache on miss (default) Write-through = write DB + cache together (freshness) Write-back = write cache first β†’ DB later (fast write) Read-through = app only talks to cache (transparent)