Offline-first and the local store
There is a spectrum here, and teams routinely commit to a point on it without noticing that the steps are not evenly spaced:
online-only → cached reads → offline reads → offline writes → local-first
└── the cliff ──┘
Everything left of the cliff is caching, and you already know how to do it from Tier 4. The moment the device accepts a write while disconnected, it becomes a write-accepting replica in a partitioned system, and every consequence in Tier 6 arrives at once. That is not a feature increment. It is a different architecture, and this lesson is about the one inversion that makes it tractable.
The inversion
The UI reads from local storage. Always. The network's only job is to update local storage.
Not "read from the network, fall back to cache" — that design has two code paths where one is exercised only when things go wrong, which is precisely the path you least want under-tested. Point every screen at a local database (SQLite on mobile, IndexedDB or SQLite-in-Wasm on the web), and the app behaves identically whether the network is present, absent or merely terrible. The offline path stops being a special case because there is no other path.
You have seen this shape before. It is Lesson 7.04's CQRS, with the read model living on the phone: the local store is a projection, the sync protocol is the thing that keeps it current, and the UI never queries the source of truth directly. Recognising it as CQRS is useful, because it tells you what to expect — the local copy is eventually consistent, and that lag is going to be visible to the user, which is 8.03's whole subject.
The outbox, on a phone
Writes go to the local store and to a durable local queue of pending mutations, in one local transaction. The queue drains when connectivity returns, retrying with backoff.
That is Lesson 6.05's transactional outbox, running on a device, for exactly the same reason it exists on a server: a local commit and a network send are two different systems and no transaction spans them. Write the intent alongside the data, atomically, and let a separate process deliver it. The pattern transfers completely, including the consequence — delivery is at-least-once, so the server side must be idempotent, which is why 8.03's client-generated ids matter so much.
Four things you have now taken on
- Local migrations you do not schedule. A user opens the app after eight months on version 1.2 and your migration chain must run 1.2 → 1.9 in order, on a phone, without an operator. Keep migrations forward-only, small and individually tested, and never assume the previous version was recent.
- The local store can vanish. Browsers evict IndexedDB under storage pressure. Mobile operating systems reclaim space. Users clear data and reinstall. Design for the local database being gone, not merely stale — which means it must be reconstructible from the server, and anything that exists only on the device is data you have chosen to lose.
- Initial sync is a real problem. A fresh device needs enough data to be useful before the user gives up. A full download is often minutes; the fix is a priority order — the screens the user opens first, then the rest in the background — which means somebody has to decide what "first" means. That decision is product work, not infrastructure work, and it is usually discovered late.
- Storage is finite and shared. Your app's local database competes with photos and everything else on the device. Eviction policy — what you drop and when — is Tier 4's cache eviction, running where you cannot observe it.
Partial replication is the hard part
You cannot ship the whole database to every device, so each device gets a subset — and defining that subset is where generic sync engines get difficult and where most bespoke ones get subtly wrong.
The subset is a query: "everything in the projects this user can see". Call it the sync scope. Two properties make it hard:
- It changes. A user joins a team and thousands of records become relevant. The sync protocol has to notice and deliver them.
- It shrinks. The harder direction. When a user loses access to a project, the data is already on their device, and the server must tell them to remove it. "Records you can no longer see" is not something an incremental "what changed since X" feed naturally expresses — a deletion of access is not a deletion of the record. Get this wrong and revoked access leaves the data readable locally forever, which is a security bug that will not appear in any server-side test.
The device is not a trust boundary
Everything in the local store is readable by a determined user: a rooted phone, browser devtools, an unencrypted backup. Encrypt at rest by all means, and understand what it does and does not buy — it protects against a stolen device, not against the device's owner.
The rule that follows is absolute and worth repeating from Lesson 12.02: authorisation is enforced on the server, on every sync request, per record. The client's sync scope is a performance optimisation and a UX affordance. It is never the access control, no matter how carefully the query was written, because the query runs on hardware the attacker owns.
Adding offline support as a feature in a later release. It is not a feature; it is an inversion of where the UI's source of truth lives, and retrofitting it means rewriting the data access of every screen you have shipped — plus discovering, one at a time, every place the code assumed a request could fail loudly rather than simply not happen yet. Teams that decide on day one that the UI reads from a local store pay a modest, steady cost. Teams that decide in year two pay for a rewrite and call the architecture the problem.
The tradeoff sheet
- Problem solved
- An application that becomes useless the moment connectivity does, in a world where connectivity is intermittent by default.
- Rejected, and why
- A loading spinner and a retry is enormously simpler and is honest for anything the user cannot act on offline. Read-only caching covers a large share of the value at a fraction of the complexity.
- The trade
- A second database on a device you do not control, with its own migrations, its own corruption modes, and a sync protocol between the two.
- First to break at 10x
- The sync path, not the local store. At 10x the offline duration or the change volume, reconciliation stops being incremental and clients need a full resync you must be able to serve.
- What it does to the bill
- Nothing on the server per device, and real cost in the sync tier and the support burden of clients whose local state is wrong.
- Day-2 operations
- You cannot log into a phone. Ship structured telemetry about local store health and schema version, or the failures are invisible until a review mentions them.