In corporate boardrooms with Gigabit Wi-Fi, modern web applications feel instantaneous. On an airplane, inside a subway tunnel, or across rural cellular towers, apps built with naive client-server REST paradigms fail immediately: spinners freeze screens, buttons lock up, and incomplete submissions vanish.

When engineering Brinn's mobile client and our legal field applications, offline capability was not a progressive enhancement—it was a core architectural requirement.


The Philosophy of Local-First

In a local-first application:

  1. The local database is the single source of truth for the UI.
  2. All user operations write directly to local storage synchronously.
  3. The network is treated as an intermittent, untrusted background synchronization mechanism.
Architecture Schematic
User Action (e.g. Save Note)
        │
        ▼ (Instant sub-1ms Write)
[Local SQLite DB (CRDT Tables)] ──► UI Re-renders Immediately
        │
        ▼ (Background Sync Queue)
[Sync Coordinator Service]
        │
        ▼ (Check Network Connectivity)
    Online? ──► Send Delta Changes to Server
    Offline? ──► Exponential Backoff & Local Retry Queue

Conflict Resolution: State-Based CRDTs

When two devices edit the same task checklist offline, naive Last-Write-Wins (LWW) timestamp comparisons can overwrite valuable data due to device clock skew.

Instead, we employ Conflict-Free Replicated Data Types (CRDTs):

typescript
export interface LWWRegister<T> {
  value: T;
  timestamp: number; // Lamport logical clock
  peerId: string;
}

export function mergeRegisters<T>(
  local: LWWRegister<T>,
  remote: LWWRegister<T>
): LWWRegister<T> {
  if (remote.timestamp > local.timestamp) {
    return remote;
  }
  if (remote.timestamp === local.timestamp && remote.peerId > local.peerId) {
    return remote;
  }
  return local;
}

By combining Lamport logical clocks with deterministic peer tie-breakers, all participating devices independently converge on the exact same state without central database locking.