skip to content

Stale Data & Invalidation

Cached data goes stale, so the client serves it while revalidating, revalidates conditionally, and invalidates dependent entries after a write or push. Interviewers probe what the user sees meanwhile.

on this pageshow

explore

questions

5

In a mobile sports app, a fan opens a league table cached an hour ago in a crowded stadium: what should the screen show, and when?

level: juniorimportance: must knowfreq 56%

answer

  1. the copy on disk paints first
  2. the refetch runs behind the screen
  3. update rows in place, keep the scroll
  4. failure keeps the copy, adds its age
  5. a spinner only when nothing is stored

basics

~20 s

Show the cached table at once and refetch it in the background (stale-while-revalidate). Fresh rows replace old ones in place without moving the scroll; if the refetch fails, the cached table stays, labelled with its age, with a retry.

solid answer

~50 s

I'd use **stale-while-revalidate**, the same idea as the HTTP extension but applied by the app's own data layer: the screen reads the table from the local store and paints it in the first frame, while a background request asks the server for a newer one. An hour-old table is still useful; a spinner over a blank screen on a congested stadium network is not. When the response lands it is written to the store, and the screen, which observes the store, updates only the changed rows in place without moving the scroll. If the request fails or times out, the cached table stays, a quiet line says `Updated 14:05`, and a retry is offered. A full-screen loading state is only for the case where nothing is cached at all, and an error never wipes content already on screen.

go deeper

for a junior

Remember the order: paint the cached copy first, refetch in the background, update in place. A spinner belongs only to a screen with nothing stored.

for a middle

Explain the failure path as carefully as the success path: what stays on screen, how its age is shown, and when the app retries without hammering a congested network.

for a senior

Show you have handled the moment the data lands: matching rows by stable id, keeping the reader's place, and a ceiling beyond which a stale copy is no longer presented as current.

for a principal

Frame it as a product promise per screen: how old data may be when shown, how it is labelled, and who signs off on those limits across the app.

## What "stale" means for a screen on a phone A **cached copy** is data the app stored on the device after an earlier request: here, the league table the fan last looked at, kept in the app's local store or disk cache. It is **stale** once it is older than the screen's **freshness window**. It may still be right, but the app can no longer assume so. Stale is not the same as wrong: an hour-old table is usually correct for every club that has not played since, and it is far more useful than a blank screen. On a phone, refusing to show it is expensive. A crowded stadium is the textbook bad network: thousands of handsets share a few cells, requests stall for seconds or time out, and a screen that waits for the network before drawing anything shows a spinner for as long as the radio struggles. ## Stale-while-revalidate, step by step **Stale-while-revalidate** is the name of an HTTP cache extension (RFC 5861): within a stated window, a cache may return a stale response immediately while it revalidates in the background, hiding network and server latency. A mobile app applies the same idea in its own data layer, whatever headers the server sends: 1. The screen reads the table from the local store and paints it in the first frame. 2. Because the copy is older than its freshness window, the data layer starts a background request for a newer one. 3. The fan reads, scrolls and taps while that request is in flight; nothing waits on it. 4. When the response arrives, it is written to the store, and the screen, which observes the store, updates. 5. If the request fails, the stored copy is untouched and the screen says how old it is. The fresh-stale-missing decision belongs in one place in the data layer rather than in each screen; this answer is about the policy the user experiences. ## What the user sees when the answer arrives The moment the refetch lands is where many designs go wrong. A good policy: - **Updates in place.** Rows are matched by a stable id (the club), so only rows whose values changed redraw; the list is not rebuilt. - **Keeps the scroll position.** If the fan is reading row 15, row 15 stays under their thumb even if clubs above it swapped places. - **Changes quietly.** A brief highlight on changed rows and a refreshed `Updated just now` line are enough: no toast, no flash, no jump to the top. - **Does nothing visible if nothing changed.** An identical table should not even flicker. ## When the refetch fails: serving stale honestly RFC 5861's companion extension, **stale-if-error**, lets a cache answer with a stale response when the origin fails, for example with a server error or an unreachable host, instead of passing on a hard error. The app's version of that rule: - Keep the cached table on screen; a failed refresh does not make it less true than it was a second earlier. - Show its age in words a fan understands, such as `Updated 14:05` or `1 h ago`, so an old table is never mistaken for a current one. - Offer a retry, and retry on its own when connectivity returns, with growing delays rather than a tight loop on a congested network. - Respect a **ceiling**: each screen sets an age beyond which its copy is no longer presented as current. A table from three matchdays ago is not "a bit stale"; past the ceiling it is shown only with a prominent warning, or not at all. ## The four situations compared | Stored copy | Network | What the screen shows | |---|---|---| | Fresh (inside its window) | any | The copy; no request is sent | | Stale | works | The copy at once, updated in place when the refetch lands | | Stale | fails | The copy, labelled with its age, plus a retry | | None | works or fails | A loading state, then content or an error with a retry | Only the last row justifies a full-screen loading or error state, and even there one rule holds: an error never wipes content that is already on screen. ## Why this is the default on mobile Phones lose and regain the network constantly: lifts, tunnels, flight mode, stadiums at kick-off. Offline-first guidance from a platform vendor recommends presenting local data immediately instead of waiting for the first network call to complete or fail. Stale-while-revalidate turns that recommendation into a rule: how fast the screen appears depends on the disk, not the radio, and the network's only job is to make the stored copy better.

  • Should the app refetch the league table every time the screen opens, even if the cached copy is ten seconds old?
    No. Each screen has a freshness window: inside it the cached copy is shown and no request is sent, which saves battery, data and server load when fans flick between screens. Only once the copy is older than the window does the app show it and revalidate in the background. The window is set per screen from how fast its data changes.
  • The refetch returns while the fan is reading row 15, and three clubs above it have swapped places. What should happen?
    Apply the new data in place, matching rows by club id, so only the changed rows redraw. Keep the row the fan is reading anchored where it is rather than letting the list shift or jump to the top. A short highlight on the moved rows tells the fan something changed without taking their place away.
  • Nothing is cached for this table and the request fails. What does the screen show?
    This is the one case for an honest error state: nothing to show, so say the table could not be loaded, explain that the phone looks offline or the server is unreachable, and offer a retry. Retry automatically when connectivity returns, and once anything has loaded, never replace it with this error again.

A café keeps last night's league table pinned to the wall. You read it the moment you walk in while the owner phones for today's standings; when the call gets through he corrects the changed lines in pen, and if the line is busy the old sheet stays up with its date at the top.

  • Stale-while-revalidate means the user waits for the refetch before seeing anything.
  • A failed background refresh means the cached table is no longer safe to show.
  • Rebuilding the whole list and scrolling to the top on refresh is harmless.
  • The cached copy needs no age label because it is replaced within seconds anyway.
  • The app can only serve stale data if the server sends the stale-while-revalidate header.
open as a page

In a mobile sports fixtures-and-results app, how would you set different staleness rules for the fixture list, the league table and a match screen?

level: middleimportance: must knowfreq 54%

basics

~20 s

Give each screen its own freshness window and ceiling from how fast its data changes and what a wrong value costs: fixtures hours, tables minutes, a live match seconds. Tighten windows by match state, and revalidate conditionally.

open as a page

In a mobile sports app, after a fan follows a team, which cached entries must change, and how do you keep every screen consistent?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Patch entries the device can compute, such as the followed-teams list and the follow button, at once; mark server-derived ones, such as the home fixtures, stale when the server acknowledges the follow, and discard any response requested before it.

open as a page

In a mobile sports app, when a fixture's kick-off time changes, should the server push the new data or only a signal that marks it stale?

level: seniorimportance: should knowfreq 46%

basics

~20 s

Usually a signal: the fixture's id and revision. The app marks dependent entries stale, refetches visible ones now and others when next shown, and keeps expiry because a signal can be delayed or never processed.

open as a page

In a mobile sports app, a new release changes the shape of cached league-table entries: how do you stop it reading incompatible ones?

level: middleimportance: nice to knowfreq 36%

basics

~20 s

Put a data-shape version in each cache key or namespace and bump it when decoding changes incompatibly, so old entries read as misses and are refetched. Treat undecodable entries as misses, and delete stored entity tags with their bodies.

open as a page