✏️ Explanatory Question

How do you decide where to place a cache in your architecture?

👁 8 Views
📘 Detailed Answer
🟢 Easy
No previous question
No next question
💡

Answer with Explanation

Caches can sit at several layers, and each catches different traffic.

  • Browser or client - free, zero latency, but you cannot invalidate it. Use Cache-Control with short TTLs or content hashed filenames.
  • CDN edge - best for static assets and media; cuts both origin load and bandwidth cost.
  • Reverse proxy or API gateway - caches whole responses for identical anonymous requests.
  • Application in-process - nanosecond access, but each instance has its own copy, so hit rates are lower and invalidation is harder.
  • Distributed cache (Redis, Memcached) - shared across all instances, the usual workhorse. About 1 ms away.
  • Database buffer pool - already there; make sure the working set fits in RAM.

Then choose a strategy: cache-aside is the default; write-through keeps the cache fresh at the cost of write latency; write-behind is fastest but risks data loss.

Always state the eviction policy (usually LRU), the TTL, and your invalidation plan - stale data bugs are almost always invalidation bugs. Mention cache stampede protection too, via request coalescing or staggered TTLs.

No previous question
No next question