In a mobile sports app, after a fan follows a team, which cached entries must change, and how do you keep every screen consistent?
answer
- the follow feeds many screens
- patch what the device can compute
- mark server-derived entries stale
- invalidate on the acknowledgement
- discard responses that began before
basics
~20 sPatch 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.
solid answer
~50 sFollowing changes one record the user owns, but many entries are derived from it. I'd keep a dependency map in the data layer from each kind of write to the entries it affects, usually as tags. Entries the device can compute exactly, like the followed-teams list and the team page's button, are patched in the local store immediately, and observing screens update. Entries the server assembles, like the home screen's personalised fixtures, are **invalidated**, marked stale rather than deleted, when the server **acknowledges** the follow; refetching earlier returns the old answer. A request already in flight when the acknowledgement lands is discarded via a per-key generation counter. An HTTP cache won't do this for me: it must invalidate only the URI the write went to, and may add those named in `Location` or `Content-Location`; nothing there knows about the home screen.
go deeper
Recall that one write can change data shown on several screens, and that cached copies of those screens do not update themselves.
Explain the split between entries the device can patch exactly and entries it must invalidate and refetch from the server.
Show the timing judgment: invalidate server-derived entries on the acknowledgement, guard against in-flight responses with a generation check, and mark stale instead of deleting.
Weigh where dependency knowledge should live, in a client map, in server-sent tags or both, and how it stays correct as teams add screens.
## One write, many dependent entries When the fan taps **Follow** on a team, the app records a write the user owns: the set of followed teams. Entries all over the app are derived from it: - **The followed-teams list:** the user's own data; the app knows its new value exactly. - **The team page:** its follow button, and perhaps a follower count. - **The home screen:** "your fixtures" and "your results", which the server assembles from the follow set. - **Anything else keyed on the follow set:** a shortlist of tables, per-team alert settings. These fall into two groups, handled differently: 1. **Locally derivable entries** are patched in the local store when the write is made: the list gains the team, the button shows "Following", and screens observing the store update at once. Getting the write to the server durably is the outbox's job, and marking it pending or rolling it back on rejection is the optimistic-update design. 2. **Server-derived entries**, such as the personalised home fixtures, cannot be computed reliably on the device. They are **invalidated**: marked stale so the next read revalidates them. ## Invalidate after the acknowledgement, not before Timing matters. If the app marks the home screen stale the moment the fan taps Follow and refetches immediately, the server may not have the follow yet: it is still queued, or in flight. The refetch returns the old home screen, the app stores it as fresh, and the new team's fixtures stay missing until the freshness window runs out. So server-derived entries are invalidated when the server **acknowledges** the write. If the follow waits three hours in the queue on a plane, the home screen keeps its cached content until the acknowledgement lands, optionally with the new team's fixtures merged in from data already on the device. ## A dependency map, owned in one place The app needs an explicit map from each kind of write to what it affects, kept in the data layer rather than scattered across screens: | Write | Patch locally at once | Invalidate on acknowledgement | |---|---|---| | Follow or unfollow a team | followed-teams list, team page button | home fixtures and results, follower count | | Change favourite league | settings entry | home table widget | | Mute a team's alerts | that team's alert setting | nothing else | Tags keep the map maintainable: entries carry tags such as `follows` or `team:t-12`, and a write invalidates by tag instead of listing every key. Do not expect HTTP caching to do this. RFC 9111 requires a cache to invalidate the **target URI** of a successful unsafe request such as `POST` or `PUT`, and allows it to invalidate the URIs named in `Location` or `Content-Location`; anything beyond that is left to mechanisms the standard does not specify. The home screen's URL is not touched by a follow request, so an HTTP cache alone keeps serving the old home screen. ## The race: a response that started before the write A request for the home screen may already be in flight when the acknowledgement arrives. If it lands afterwards and is stored, it overwrites the invalidation with pre-follow data. A **generation counter** per key closes the gap: invalidation increments the counter, every request records the generation it started under, and a response from an older generation is discarded. ``` on follow acknowledged(team): for key in dependents("follow", team): generation[key] = generation[key] + 1 mark stale(key) if key is on screen: revalidate(key) on response(key, data, started_generation): if started_generation < generation[key]: discard data (requested before the write landed) else: store(key, data) (also clears the stale mark) ``` ## Invalidate means "stale", not "gone" Invalidation marks an entry stale; it does not delete it. The two choices differ in what the fan sees: - **Mark stale:** the home screen keeps its cached content while the revalidation runs, exactly as stale-while-revalidate does anywhere else. - **Delete:** a follow turns into a blank screen and a spinner, and, offline, into an empty home screen with nothing to refetch from. - **Clear everything:** correct, but each tap becomes a burst of refetches, blank screens and wasted data. Precise dependencies cost design effort once and spare the user on every write. ## Letting the server name the dependents The server knows best what a write changed. A write response can carry the updated resources themselves, such as the new follow set, or a list of tags to invalidate, so the dependency knowledge sits next to the code that creates the dependency. The client still keeps its own map as a fallback for entries the server does not know it holds.
- Why not simply clear the whole cache after any write?It is correct but expensive. Every tap becomes a burst of refetches, screens fall back to spinners, mobile data is spent re-downloading unchanged fixtures and tables, and offline the app is left with nothing to show. A precise dependency map invalidates only what the write can have changed.
- The follow is queued offline for three hours. What does the home screen show meanwhile?The followed-teams list already shows the team, because it was patched locally. The server-derived home fixtures stay as cached, since the server has not seen the follow; the app may merge in that team's fixtures if they are already on the device. When the acknowledgement lands, the home entries are invalidated and revalidated.
- Could the server tell the client what to invalidate?Yes. The write's response can return the changed resources or a list of invalidation tags, which keeps knowledge of server-side dependencies on the server. The client still needs its own map, because the server cannot know every entry a particular phone has cached or how the app keys them.
- An HTTP cache automatically invalidates every screen that depends on a write.
- Refetching server-derived entries the moment the local write is made returns the new state.
- Invalidating an entry means deleting it, so the screen must show a spinner.
- A response that arrives after an invalidation is always newer and safe to store.
- Clearing the entire cache after each write costs nothing but one refetch.