Cache freshness and stale data

Data and storage Mid-level Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

A news app shows 2-day-old headlines on launch until the user pulls to refresh. What's a good policy?

Options
Hint 1 of 1

Can you show something now and fix it shortly after?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Show cached headlines at once and refresh in the background if fetchedAt is older than 10 minutes

Correct. Fast first paint, and fresh data arrives quickly.

Why not the others?
  • B Always wait for the network before showing anything. Slow launch and nothing offline.
  • C Delete the cache on every launch. Loses offline support.
  • D Refresh only when the user pulls. That's the current problem.

30-second answer

Store when each piece of data was fetched and pick a policy per data type. Show cached data immediately, then refresh in the background if it's older than its time-to-live (stale-while-revalidate). Refresh on pull-to-refresh regardless. For data that must be current, like a balance, show a 'last updated' time and block actions until fresh data arrives.

Practise saying it · 30 s. No recording, just a timer

Explain cache freshness policies.

Say your answer out loud, then check which of these points you covered:

  • Store fetchedAt
  • Stale-while-revalidate
  • Per data type TTL
  • HTTP caching headers

Deep dive

Keep fetchedAt in Room next to the data, or in a small metadata table.

HTTP caching (Cache-Control, ETag) can avoid re-downloading unchanged data.

Server push or sync tokens beat polling for frequently changing data.

Expire user-specific caches on logout or account switch.

Common mistakes

  • One global TTL for every kind of data.
  • Blocking the UI on a network refresh when cached data is fine to show.
  • Never expiring caches, so users see months-old data.

Senior take & follow-ups

Per-data-type policies with a concrete example impress more than a single rule.

Follow-up questions

Think of your answer first, then open the hint.

  1. What's stale-while-revalidate?

    Show hint

    Show cached data now, refresh in the background, update when new data lands.

  2. How can HTTP help?

    Show hint

    ETag and If-None-Match return 304 when nothing changed.

Suggest a fix if something here is wrong or out of date.