Prod Squad

Caching and CDNs: what to cache, invalidation, latency wins

Caching is the cheapest big speed-up in software, and the source of the classic "why am I seeing old data?" bug. Here's how to reason about both.

6 min readAll levels#caching#cdn#cache invalidation#ttl
Ask about this lesson
On this page

Caching means keeping a copy of something so you don't have to fetch or compute it again. It's one of the biggest latency and cost wins available, and it comes with one famous problem: knowing when the copy is out of date. PMs make caching decisions more often than they realise, because "how fresh does this need to be?" is a product question.

Why interviewers ask this

"Explain caching like I'm a PM" and "Users are seeing stale prices, what happened?" are both fair game. In system design rounds, caching is usually the first performance lever candidates should mention. Interviewers want to hear that you know what's safe to cache, what freshness the product needs, and the tradeoffs of stale data.

The framework

1. Where caches live

flowchart LR
    U["User"] --> B["Browser cache"]
    B --> CDN["CDN edge<br/>near the user"]
    CDN --> APP["App server"]
    APP --> RC["App cache<br/>for example Redis"]
    RC -->|cache miss| DB[("Database")]
  • Browser and app cache: images, scripts, and data stored on the device.
  • CDN (Content Delivery Network): servers around the world holding copies of static files (images, video, CSS, JS), and sometimes whole pages, close to users. Users in Sydney get files from Sydney, not Virginia.
  • Application cache (like Redis): results of expensive queries or computations, stored in fast memory.
  • Database caches: built into the database itself.

2. What to cache

Good candidates are read often, change rarely, and are expensive to compute:

  • Product catalogues, menus, profile pictures, configuration.
  • Results of heavy queries (trending lists, recommendations).
  • Rendered pages for logged-out visitors. Be careful with personalised or sensitive data (never serve one user's cached data to another) and fast-changing, high-stakes data (stock levels at checkout, account balances).

3. Cache invalidation: the hard part

When the source changes, cached copies become stale. Strategies:

  • TTL (time to live): expire after a set time, like 5 minutes. Simple; accepts brief staleness.
  • Invalidate on write: when data changes, delete or update the cached copy. Fresher, more complex.
  • Versioned URLs for files (app.v42.js), so new releases never collide with old caches.

4. Freshness is a product decision

For each piece of data, ask: "How stale is acceptable?" A restaurant's photos can be a day old; its open/closed status maybe a minute; the price at checkout must be exact. Design accordingly: cache for browsing, verify at the moment of commitment.

5. Measure the win

Cache hit rate, latency (p50/p95), database load, and infrastructure cost.

Worked example

Question: "Explain caching like I'm a PM, then tell me how you'd use it for a food delivery app's restaurant pages."

Sample answer:

"Simple version: caching is like keeping your most-used spices on the counter instead of in the basement. You save the trip every time, but if you restock the basement, the counter jar might be old.

For restaurant pages, I'd go piece by piece and ask how fresh each needs to be:

  • Photos and logos: rarely change, so serve them from a CDN with long expiry. Big speed-up, especially on mobile networks.
  • Menus and descriptions: change a few times a day. Cache for, say, 10 minutes, and invalidate when the restaurant edits its menu in the partner app.
  • Open/closed status and delivery time estimates: change constantly, so a very short cache (30 seconds) or none.
  • Price and availability at checkout: never trust the cache. Re-check with the source when the order is placed, and if something changed, tell the user clearly: 'The price of Paneer Tikka changed to ₹280.'

Impact I'd expect: much faster browsing, and far less load on the database at dinner peaks. I'd measure page load p95, cache hit rate and checkout errors caused by stale data, and watch support tickets for 'wrong price' complaints."

Common mistakes

  • Caching everything with one TTL, ignoring different freshness needs.
  • Trusting cached data at the moment of commitment (payment, booking).
  • Caching personalised responses publicly, which can leak data between users.
  • Forgetting invalidation when content is edited.
  • Treating the CDN as optional for global or image-heavy products.

Interviewer tips

What "great" sounds like:

  • "Freshness is a product decision: how stale can each piece of data be?"
  • "Cache for browsing, verify at checkout."
  • "Static assets go on a CDN; that's usually the biggest latency win for distant users."
  • "We'd invalidate on edit, with a TTL as a safety net."
  • "I'd track cache hit rate alongside p95 latency."

Practice questions

Answer one in a mock interview and get graded instantly.

  1. 1.Explain caching to a non-technical stakeholder, then describe how you'd use it in a food delivery app.

    Practice this →
  2. 2.Users are seeing outdated prices after we updated them. What might have happened, and how would you fix it?

    Practice this →
  3. 3.Our app is slow for users in Europe but fast in India. How could a CDN help?

    Practice this →

Finished reading?