Caching
A cache keeps a copy of frequently used data in fast storage, usually memory, so it can be served without repeating slow work.
Why it matters
Reading from a database on disk, or recalculating a result, is slow and expensive compared with reading memory. When the same data is read again and again, repeating the work is waste.
The idea
Caches exist at many layers: the browser, the CDN, the application (Redis or Memcached) and the database. The usual pattern is cache-aside: read from the cache; on a miss, read the database and store the result with a TTL. The hard parts are keeping the cache in step with the database (invalidation) and choosing what to remove when it's full (eviction, often LRU).
Keeping the books you use every day on your desk instead of walking to the library each time.
Books on your desk don't change by themselves. Data does: when the database changes, your cached copy is stale until it's updated or expires. That's the hardest problem in caching.
How it works
- A request needs product 42. The app asks Redis for the key 'product:42'.
- Hit: Redis has it and returns it in under a millisecond.
- Miss: the app reads the database (perhaps 5–20 ms), returns the result, and stores it in Redis with a TTL of, say, 5 minutes.
- When product 42 changes, the app deletes 'product:42' from Redis, so the next read loads the fresh value.
- When Redis is full, it evicts keys, usually the least recently used ones.
- Measure the hit rate: 95% means only 1 read in 20 reaches the database.
Example
A product page is read 1,000 times for every update. Cache-aside in Redis with a 5-minute TTL serves almost every read from memory, and the database sees a tiny share of the traffic.
Key points
- Cache-aside (lazy loading): check the cache → on a miss, load from the database and fill the cache. The most common pattern.
- Write-through: write the cache and database together (fresh, but slower writes). Write-back: write the cache, save to the database later (fast, but data can be lost).
- Eviction: LRU (least recently used), LFU (least frequently used), TTL expiry.
- Invalidation: delete or update the cache entry when the data changes; the TTL is your safety net.
- Problems to know: stale data, cache stampede (many misses at once), hot keys, cold start.
- Cache only data that is read often and can be slightly out of date.
Common mistakes
- Caching data that changes constantly, or is hardly ever read.
- No TTL, so stale data lives forever if one invalidation is missed.
- No plan for a stampede when a very popular key expires.
- Treating the cache as the source of truth. It can be emptied at any moment.
Not true: Important data: it can live in the cache alone, with no copy in a database.
In fact: A cache can be emptied at any moment: on a restart, or when memory runs out. The database stays the source of truth.
Not true: Cache-aside: the cache is filled when the data is written.
In fact: Cache-aside fills the cache on a read miss: load from the database, then store the value. Filling the cache on every write is write-through.
In the interview
Reads far outnumber writes here, so I'd add a Redis cache using cache-aside with a TTL. On writes I'd delete the cache key so the next read loads fresh data. I'd watch the hit rate, and protect hot keys from stampedes by letting one request rebuild while others wait.
Part of Unit 2: Core concepts: scale, reliability and speed in the free System Design course.