skip to content

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%

answer

  1. the shape the decoder expects
  2. a version inside the key
  3. an old-shape entry is just a miss
  4. a validator dies with its body
  5. discard a cache, migrate a store

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.

solid answer

~50 s

I'd version the **key**, not just the code: `table:v5:l-1:2026`, where `v5` is the shape the decoder expects. A build that changes the shape incompatibly bumps the constant, so entries written by the old build are simply misses: the table is refetched, and old namespaces are swept in the background. Versions per data type keep the cost small, since a table change does not throw away fixtures. An entry that fails to decode is also treated as a miss and deleted, never a crash. Cached data is discarded rather than migrated because the server can resend it; data that exists only on the device belongs in the persistent store, which is migrated instead. Two traps: a stored entity tag must be deleted with its body, or a `304` confirms data the app no longer has; and the first launch after a bump has an empty cache, which hurts offline.

code

pseudocode · 15 lines
pseudocode
table_shape = 5

function table_key(league, season):
  return "table:v" + table_shape + ":" + league + ":" + season

function read_table(league, season):
  key = table_key(league, season)
  entry = read from cache(key)
  if entry is missing:
    return miss
  table = decode(body of entry)
  if table failed to decode:
    delete from cache(key)     (body, fetched-at and entity tag together)
    return miss
  return table

go deeper

for a junior

Recall that an app update can change the shape of stored data, and that entries written by the previous build may still be on the device.

for a middle

Explain how a version in the key turns incompatible entries into misses, and why a cache is discarded while the persistent store is migrated.

for a senior

Cover the traps: entity tags orphaned from their bodies, older code meeting newer entries after a rollback, and empty screens on the first offline launch.

for a principal

Set the rule for the whole app: who bumps versions, how compatibility is tested against the previous release, and which caches deserve a translation instead of a discard.

## The problem A cache entry is stored in the **shape** the code that wrote it understood: a league-table row with `points`, `played` and a `form` string, say. Release 7.0 changes it: `form` becomes a list of results and a new `deductions` field is required. Cached entries written by 6.x are still on the device when 7.0 first launches. If 7.0 reads one and decodes it with the new shape, the result is a crash, a screen of blanks, or worse, a table that looks fine and is wrong. ## Version the key, not just the code The usual defence is to put a **data-shape version** into each cache key or namespace, for example `table:v5:l-1:2026`. - The version is a constant next to the code that decodes the entry, and it changes whenever that decoding changes incompatibly. - A new build looks up `table:v5:…`; an entry stored as `table:v4:…` is simply a **miss**, and the table is fetched again. - Old namespaces are swept in the background after launch, so they do not hold disk space indefinitely. - **Per-type versions** keep the blast radius small: a table change discards cached tables, not fixtures or team pages. As a second line of defence, an entry that fails to decode is treated as a miss and deleted: never a crash, never a half-filled screen. ## Why a cache is discarded but a database is migrated | | Cache entry | Persistent store | |---|---|---| | Copy elsewhere? | Yes, the server can resend it | Often not: drafts, user data, pending writes | | On a shape change | Versioned key: discard and refetch | Chained migrations, tested from each shipped version | | Cost of losing it | One refetch | Lost user data | Writing and testing migration code for data the server can resend costs more than it saves. The persistent store holding data that exists only on the device is a different matter: its schema is migrated step by step, and that is a separate design. ## The version tracks the stored shape, not the API's The key's version follows what **this app stores**, which is not always what the server sends. The API can stay on one version while the app changes how it stores a table, and the app can map a new API version into an unchanged stored shape. Tie the key to the stored shape and each incompatible change is caught exactly once. Two consequences follow: - A new API version that the app maps into the old stored shape needs no bump, so the cache survives the upgrade. - A refactor that changes the stored shape needs a bump even though no request changed. ## The traps 1. **Orphaned validators.** If the app keeps an entry's stored entity tag after discarding the body it described, the next conditional revalidation sends that tag, the server answers `304 Not Modified`, meaning "you already hold a valid copy", and the app has nothing to show. A tag lives and dies with its entry, ideally inside the same versioned key. 2. **An older build meeting newer entries.** Phones rarely run older code, but it happens: an over-the-air code update rolled back in a cross-platform app, or an older build reinstalled for testing. With versioned keys the old code never finds the newer shape; with one unversioned key it decodes data it does not understand. 3. **The empty first launch.** Every bump costs the user that cache once. A fan who updates on home Wi-Fi and opens the app in a stadium with no signal sees empty screens. Bump only the types whose shape changed and, for the one or two screens that must work offline, consider a one-time translation of the old entry. 4. **"We only added a field."** That is safe for a decoder that tolerates missing fields. A renamed field, a changed type or a new required field is not. ## A rule of thumb - If a change would break the decoder, bump that type's version. - If you believe it would not, prove it with a test that decodes an entry written by the previous release. - Keep everything that describes an entry, its body, its fetched-at time and its entity tag, under the same versioned key, so they appear and disappear together.

  • Why not migrate cached entries the way the local database is migrated?
    Because the server can resend them. Migration code has to be written, tested from every shipped shape and run at launch, which competes with startup time, all to preserve data one refetch restores. Reserve migrations for the persistent store, where data may exist only on the device.
  • Fans who update and then open the app offline see empty screens. How would you soften that?
    Bump versions per data type so only the changed type is discarded, and keep compatible changes, such as an optional new field, on the old version. For the few screens that must work offline, ship a one-time translation from the old shape to the new one, tested against entries written by the previous release.
  • Old-version entries are ignored but never deleted. What happens over time?
    Each release leaves another orphaned namespace on disk. If the entries sit in the OS's purgeable cache folder, the system may delete them when storage runs low, at a time the app does not choose; anywhere else they stay. Sweep old namespaces in the background after launch.

  • Adding or changing cached fields is always safe, so cache keys never need versions.
  • Deleting old-shape entries while keeping their stored entity tags is harmless.
  • Cached entries must be migrated step by step, or the data in them is lost for good.
  • The server API's version number is enough to version what the app caches.
  • A cache entry the new code cannot decode should crash the app so the bug gets noticed.