⚡ Level 4 — Caching
Caching is one of the highest-impact optimizations in system design.
4.1 What is Caching?
Caching involves storing frequently-accessed data in a fast access layer to reduce latency and database load.
Visualizing Cache Speed Improvement
4.2 Caching Layers
Caching can happen at multiple levels in a modern system.
4.3 Cache Writing Strategies
Cache-Aside (Lazy Loading)
The application is responsible for reading from and writing to the cache.
💻 JS Example: Cache-Aside Pattern
javascript
async function getUserWithCache(userId) {
const cacheKey = `user:${userId}`;
// 1. Try to get from cache
const cachedUser = await redis.get(cacheKey);
if (cachedUser) {
console.log("Cache Hit!");
return JSON.parse(cachedUser);
}
// 2. If miss, get from database
console.log("Cache Miss, querying DB...");
const user = await db.users.findById(userId);
// 3. Store in cache for next time (with 1 hour TTL)
if (user) {
await redis.setex(cacheKey, 3600, JSON.stringify(user));
}
return user;
}4.4 Cache Pitfalls
Cache Stampede (Thundering Herd)
When a popular key expires, thousands of concurrent requests might hit the database at once.
🛠️ Solution: Mutex Locking (JS Logic)
javascript
async function getWithLock(key) {
let val = await redis.get(key);
if (!val) {
// Acquire a simple lock (or wait for one)
if (await acquireLock(key)) {
val = await db.fetch(key);
await redis.set(key, val);
await releaseLock(key);
} else {
// Wait and retry
return getWithLock(key);
}
}
return val;
}4.5 Redis vs Memcached
| Feature | Redis | Memcached |
|---|---|---|
| Data Types | Complex (Lists, Sets, Hashes) | Strings only |
| Persistence | Yes (optional) | No (In-memory only) |
| Replication | Yes (Primary-Replica) | No |
| Best For | Leaderboards, Pub/Sub, Objects | Simple Page Caching |
✅ Checklist Before Moving On
- [ ] I can explain cache-aside vs write-through
- [ ] I know what LRU eviction means
- [ ] I can describe cache stampede and how to fix it
- [ ] I know when to use Redis vs Memcached
➡️ Next: Level 5 — Messaging & Queues
