Design a Local-First Sync Engine (Linear)
Deliver zero-latency UX with a local IndexedDB/SQLite store, optimistic mutations, monotonic delta logs, rebase conflict handling, and partial sync.
In this lesson (4 bite-sized sections)
Why local-first feels instantaneous
Classic web apps treat the browser as a thin terminal: every click fires an HTTP request, shows a spinner for 120ms, and waits for the server to confirm before updating the screen. A local-first sync engine (Linear, Figma, Replicache) inverts that loop. The UI reads from and writes to an embedded local store (IndexedDB in browsers, SQLite on desktop and mobile) synchronously, delivering sub-16ms frame updates and full offline editing. Networking moves out of UI components into a background sync engine.
Optimistic mutations and the persistent outbox
When a user drags an issue to "In Progress," the engine writes two records atomically in the local database: an optimistic mutation applied to the local projection so the board updates immediately, and an immutable mutation intent appended to a persistent FIFO outbox queue.
Every queued mutation carries a client-generated UUID and the entity's expected base version. A background sync worker streams pending outbox entries to the server over WebSocket or HTTP. If a mutation is rejected by server-side authorization or validation, the engine drops it from the outbox, restores the authoritative server state, replays any remaining valid queued edits, and alerts the user without losing unrelated offline work.
Local-first client store and monotonic delta sync: an architecture diagram of 6 components and 6 connections. Full description follows.
- UI Components (< 16ms): client.
- Local Store (SQLite / IDB): cache.
- Persistent Outbox Queue: queue or log.
- Sync API + Validator: service.
- Authoritative DB + Delta Log: database.
- WebSocket Poke Fan-Out: load balancer.
- UI Components (< 16ms) calls synchronously Local Store (SQLite / IDB) (sync read + optimistic write).
- Local Store (SQLite / IDB) calls synchronously Persistent Outbox Queue (tx enqueue).
- Persistent Outbox Queue sends asynchronously to Sync API + Validator (push mutations (UUID)).
- Sync API + Validator calls synchronously Authoritative DB + Delta Log (commit + assign sync_id).
- Authoritative DB + Delta Log sends asynchronously to WebSocket Poke Fan-Out (new sync_id poke).
- WebSocket Poke Fan-Out feeds back into Local Store (SQLite / IDB) (pull deltas since cursor).
Delta sync protocol and rebasing offline edits
Downloading the full workspace on every reconnect wastes bandwidth and battery. Instead, the server stamps every committed change in a workspace with a monotonic sequence number (sync_id) inside an append-only delta log (assigned inside the database transaction so no gaps skip committed records). Each client saves its last_sync_id locally and fetches GET /sync?since=last_sync_id whenever a lightweight WebSocket poke announces newer commits.
Conflict resolution uses client-side rebasing modeled like Git: keep two logical layers (confirmed server state at last_sync_id plus the unacknowledged local outbox). When incoming deltas arrive, advance the confirmed state first, remove any mutations the server already acknowledged by UUID, and deterministically re-apply remaining unconfirmed mutations on top. Simple scalar fields (issue status, priority) resolve with field-level last-write-wins using server commit order or Hybrid Logical Clocks so concurrent edits to different fields never clobber each other, while collaborative text descriptions embed a CRDT (such as Yjs).
Partial sync and cold-start bootstrapping
An enterprise workspace with 500K historical issues cannot be downloaded on first login without blowing browser IndexedDB limits and multi-second load budgets. Production engines implement partial sync (scoped shapes): cold start downloads a compact prebuilt bootstrap snapshot covering only the active working set (teams, users, open sprint issues) plus the tiny delta tail since that snapshot's sync_id.
| Sync Scope | Cold-Start Cost | Offline Depth | Storage Footprint |
|---|---|---|---|
| Classic REST fetch | Fastest (1 page) | None without cache | Minimal |
| Full-workspace sync | High on large orgs | Entire history | Grows linearly with org age |
| Partial scoped sync | Fast (active snapshot + tail) | Active projects + on-demand scopes | Bounded by LRU scope budget |
Archived tickets and cold projects load lazily by subscribing to an extra scope on demand and evicting unused scopes under an LRU budget. If a device stays offline longer than the server's delta-log retention window (for example 30 days), the server returns a RESYNC_REQUIRED response telling the client to pull a fresh snapshot while keeping its unpushed outbox intact.
Sources: Ink & Switch, Local-First Software · Linear sync engine scaling · Replicache sync protocol · MDN IndexedDB API
Apply
Your turn
The task this lesson builds to.
Architect a Linear-style local-first sync engine for an issue tracker supporting offline edits, instant UI updates, multi-device sync, and workspaces with 500K issues.
Think about
- How do the local database and persistent outbox give sub-16ms optimistic UX without losing edits on crash?
- How do monotonic server sequence numbers and client rebasing converge concurrent offline edits?
- Why does a 500K-issue workspace require partial sync snapshots and retention-window resync?
Draft your design answer here Save your draft, compare against the model answer, and track progress.
Practice
Make it stick
A second problem on the same idea, so it survives past today.
Specify how to enforce fine-grained permission revocation in a local-first sync engine when a user is removed from a confidential team while their laptop is offline and later reconnects.
Think about
- Why is filtering only future outgoing deltas insufficient when the removed user already holds local rows?
- How should the server signal which locally cached team records must be purged on reconnect?
- What happens to offline outbox mutations targeting the confidential team that the user queued before reconnecting?
Draft your design answer here Save your draft, compare against the model answer, and track progress.