Caching reduces waiting and the number of network requests, but stale data can destroy trust. A user will wait half a second to see an up-to-date order status, and is far less willing to act on wrong information: paying for an item that is already out of stock, or driving to an address that has changed.

In our projects a cache is almost never "just an optimisation". It is part of the data architecture that determines what people see without a network, how fast a screen opens, and whether data from two accounts gets mixed up on one phone. Below is how we decide what to cache, how to choose a freshness window and which mistakes occur most often.

What to cache and what not to

  • data that changes rarely: reference lists, categories, interface settings;
  • the last successful state for offline reading: the feed, order history, profile;
  • images and assets with a controlled lifetime;
  • drafts and local progress that must not be lost;
  • results of expensive on-device computations.

Be careful with balances, prices, stock levels, payment statuses and anything people use to make financial decisions. Such data can be shown from a saved copy only with a clear timestamp and a mandatory server check before any action. Access tokens and other secrets should not live in a regular cache at all; Keychain and Android Keystore exist for that, as we explained in our article on storing secrets.

A policy for each data type

For every data type, define three things: the source of truth, the freshness window and the behaviour after an error. An account balance, an article list and a user avatar should not share one caching rule. Below is an example of the table we draft at the start of a project; the specific values depend on the product and are our working guidelines rather than a standard.

Data typeStrategyIndicative freshness windowBehaviour offline
Avatars, coversDisk image cacheDays, refreshed when the URL changesShow the saved copy
Feed, catalogueStale-while-revalidateMinutesShow the saved copy with a timestamp
Profile, settingsLocal database as the UI sourceUntil the next syncShow it and allow editing a draft
Price, stock, balanceNetwork first, cache for display onlySecondsShow with a warning, block the action
Payment statusNo cached decisionsAlways from the serverSay the status is unknown

How to build a cache in an app

Android's data layer guidance emphasises clear data sources and repositories that hide the details of data retrieval from the UI. In practice, local storage should not be an accidental optimisation nobody controls. We apply the same principle on iOS; see our article on MVVM and Clean architecture for details.

  1. Define cache layers. Memory for the current session, disk or a database (Room, SQLite, Core Data, SwiftData) for data across launches, and an HTTP cache for server responses.
  2. Use HTTP features. Cache-Control, ETag and conditional requests let you receive a 304 response instead of the full body when data has not changed. OkHttp and URLCache support this without extra code.
  3. Make the repository the single access point. The UI subscribes to data from the repository, which decides when to show the cache and when to go to the network.
  4. Add a schema version. After an app update, the old cache must either migrate or be cleared safely.
  5. Tie the cache to the account. Keys should include the user identifier, and signing out should clear personal data.
  6. Limit size. An unbounded disk image cache eventually takes hundreds of megabytes and hurts how the app is perceived.
  7. Show the state to the user. An "updated 5 min ago" label and pull-to-refresh remove most questions about stale data.

Agree on caching headers with the backend at the API design stage: if the server does not return an ETag or a correct Cache-Control header, the client has to invent its own rules. We covered this in our article on designing an API for a mobile client.

How to show saved data in the interface

Most often the problem is not the cache itself but an interface that does not distinguish between states. The same screen can show fresh data, a saved copy, a partially refreshed list or a refresh error on top of old data. If all these states look identical, people cannot understand why the price in the cart differs from the price in the catalogue.

  • Fresh data needs no label at all.
  • A saved copy gets an unobtrusive caption with the time of the last update.
  • A refresh in progress is shown with a thin indicator, without blocking the content already on screen.
  • A refresh error does not wipe the old data: it is better to keep it and show a short message with a retry button.

Critical actions such as payment or booking follow a separate rule: before confirmation, the app always checks current data on the server, even if the cached data looks fresh.

Cache and memory: the Zomato lesson

Caching affects stability, not only the network. In the Zomato case study published on Android Developers, the team reworked its caching mechanism among other steps and achieved a 60% drop in out-of-memory issues. It is a good reminder that an unbounded in-memory cache is one of the typical causes of crashes on budget devices.

Common mistakes

  • Cache without invalidation. Data is written once and never refreshed.
  • Mixed accounts. After switching users, the previous user's orders appear on screen.
  • No schema version. An app update reads the old format and crashes.
  • Invisible stale state. People see a saved copy but do not know it.
  • Caching errors. An error response or an empty list is stored as a valid result.
  • Unbounded growth. The cache takes more space than the app itself.

Cache verification checklist

Caching works when it hides latency but not the truth. Before a release we go through these scenarios:

  • Sign out and sign in as another user: no foreign data.
  • Change data from another device: the app refreshes it within the defined window.
  • Switch networks during loading: no empty screens or duplicates.
  • Update the app with an old cache version: migration or a clean reset.
  • Work offline: a clear timestamp and an available refresh action.
  • Long-term use: cache size stays within the set limit.

Sources: Android Developers: Data layer architecture; Android Developers: Build an offline-first app; MDN: HTTP caching; Android Developers: Zomato app speed case study.