In an offline-capable mobile workout app, how should a repository arbitrate between the server and the local store as the single source of truth?
answer
- one owner per kind of data
- the screen reads the store
- the network writes the store
- a refresh is a side effect
- offline, the store keeps answering
basics
~20 sMake the local store the source of truth: screens observe the repository, which reads from the store, and server responses are written into the store and reach the screen only through it. Offline, the screen keeps working from what is stored.
solid answer
~40 sFor a workout app used in a gym basement, the plan repository exposes an observable stream of today's plan read from the local store. When it refreshes, it fetches from the server, maps the response and writes it into the store; the store's change notification is what updates the screen. The network result is never handed straight to the caller, so there is one path for data and no screen can show a server copy that disagrees with the store. Offline, nothing changes for the screen: it still observes the store, and the failed refresh surfaces as an error alongside the stored plan. Different data may have different sources of truth (a live username-availability check can rely on the network), but for data that must work offline, the local store is the recommended choice.
code
pseudocode · 11 linesfunction observe_plan(day):
return local_store.observe_plan(day) # the only path to the screen
function refresh_plan(day):
response = server.get_plan(day) # may fail with no signal
if response.failed:
refresh_status.set(error(response.reason))
return # stored plan stays on screen
plan = map_to_app_model(response.body)
local_store.write_plan(day, plan) # observers of the store update
refresh_status.set(idle)go deeper
Recall the one-line rule: the screen reads the local store, and the network only writes into it. Be ready to say why that keeps the app usable without signal.
Walk through the read path and the refresh path separately, including what happens when the fetch fails. Explain why returning the response to the caller as well creates two owners.
Show the arbitration duties: mapping, transactional writes, not wiping unsent local data, and picking a different owner where offline makes no sense. Diagnose a screen that disagrees with another screen.
Treat the choice of owner per kind of data as a documented team rule, and weigh the storage, migration and testing cost of putting more data in the local store against the offline experience it buys.
## What "single source of truth" means inside one client A **single source of truth** for a kind of data is the one place that owns it: only the owner changes it, it exposes the data in a form callers cannot modify, and everyone else asks the owner to make changes. In a mobile client the owner is reached through a **repository**, the data-layer class that hides where the data actually lives. For an offline-capable app the question is which of the repository's sources is the owner. A workout app has two sources for today's plan: the **server**, which is the system of record across the user's devices, and the **local store** on the phone. Inside the client, the recommended answer for data that must work offline is the local store. The server is still where data ends up being agreed, but the screen never reads it directly. ## The read path and the refresh path The design separates two paths that a naive repository merges: 1. **Read path.** The screen's state holder subscribes to `observePlan(today)`. The repository returns the store's observable query. Whatever the store holds right now appears immediately, even with no signal. 2. **Refresh path.** Separately, the repository decides to fetch (on screen open, on a pull to refresh, after a sync). It calls the server, maps the response into app models and writes them into the store. 3. **Propagation.** The store notifies its observers. Every screen observing that data, the plan screen and any other, receives the new value through the read path. 4. **Failure.** If the fetch fails, the store is untouched, so the screen keeps its content; the repository reports the failure as a status the screen can show. The screen sees exactly one stream. It does not know, or need to know, whether the last change came from the server, from a sync, or from a set the user logged a second ago. ## Why not return the network response directly Returning the fresh response to the caller *and* writing it to the store looks faster. It creates two paths and the bugs that come with them: - **Divergence.** The calling screen shows the server copy while a second screen reads the store; if the write fails or is transformed differently, the two disagree. - **Races.** A response that arrives late can overwrite newer local data on the calling screen, even though the store is right. - **Offline gaps.** A screen built around "wait for the response" has nothing to show in the basement; one built around the store always does. - **Duplicated logic.** Every caller has to decide whether to prefer the response or the store, which is exactly the decision the repository exists to make once. ## Comparing candidate sources of truth | Source of truth | Works offline | Survives the app being killed | Consistent across screens | Fits | |---|---|---|---|---| | The server | No | Not applicable | Only if every screen refetches | Live checks with no offline value, such as username availability | | An in-memory cache in the repository | While the app runs | No | Yes, within the session | Short-lived flow state, such as a sign-up in progress | | The local store | Yes | Yes | Yes | Plans, logged sets, history: anything the user needs offline | Different repositories may pick different owners. What matters is that each kind of data has exactly one, and that the choice is written down. ## Where the arbitration happens "Arbitrate" means the repository resolves differences between its sources before anything reaches the store: - It maps server models into app models, so the store holds one shape. - It decides whether a server snapshot replaces stored rows or is merged with them. Sets logged offline and not yet accepted by the server must not be wiped by a refresh; how those pending writes are marked and delivered belongs to the offline-sync design, but the repository is the place that must respect them. - It writes related changes together, for example a plan and its exercises in one transaction, so observers never see half an update. - It keeps the bookkeeping the read path needs, such as when each dataset was last fetched. ## Pitfalls - A view model that keeps its own list and "merges" network results into it, creating a second owner. - A repository whose `getPlan()` returns the network result when online and the store when offline: two behaviours, two sets of bugs. - Clearing the store before a refresh "to avoid stale data", which turns a failed refresh into an empty screen. - Declaring the local store the owner of everything, including data such as a one-time server check that has no meaning offline. The interview signal is the sentence "the screen reads the store, the network writes the store", plus the ability to say what happens on failure and when a different owner is the right call.
- Must every repository in a mobile app use the local store as its source of truth?No. Each repository chooses its owner. Data the user needs offline belongs in the local store; a short sign-up flow can keep its state in an in-memory cache owned by the repository; a live check, such as whether a username is free, can treat the server as the truth because a stored answer is meaningless. The rule is one owner per kind of data, chosen deliberately.
- If the screen only observes the store, how does it learn that a refresh failed?The repository exposes a refresh status alongside the data stream, or the refresh call returns a result the state holder turns into state. The screen then shows the stored plan together with a "couldn't refresh" notice. A failed refresh never empties the store, so the user keeps working with what they have.
A whiteboard in a gym office: the coach updates the board when they can reach it, and athletes only ever read the board. When the coach is unreachable, the board still shows the last plan, and nobody is reading a different plan from a text message.
- The repository should hand the fresh network response straight to the screen.
- An offline-capable screen must wait for the network before showing anything.
- A second copy in the view model is harmless if it refreshes often.
- Every repository must use the local store as its source of truth.
- Clearing the store before a refresh is the safe way to avoid stale data.