Chapter 02 // Physical Device Layer

Client-Side Storage & Engine Sandboxes

Local-first applications require high-throughput, transactional, and crash-resilient disk persistence directly within the browser runtime. Explore the shift from slow asynchronous key-value stores to high-speed embedded SQLite running over Origin Private File System handles.

The Four Persistence Layers

Web engines expose multiple isolated storage primitives with vastly differing performance profiles and durability guarantees.

STORAGE // TIER 1

OPFS (Origin Private File System)

A fast, private filesystem endpoint accessible directly by the origin origin. Inside a Dedicated Web Worker, it exposes FileSystemSyncAccessHandle, enabling direct in-place byte reads and writes at near-native raw SSD speeds.

STORAGE // TIER 2

IndexedDB (IDB)

The historical asynchronous object store in all major browsers. While ubiquitous, it suffers from structured cloning serialization overhead, lack of synchronous transactional loops, and unpredictable transaction aborts during main-thread contention.

STORAGE // TIER 3

WebAssembly SQLite VFS

The official SQLite consortium build compiled to WebAssembly. By implementing a custom Virtual File System (VFS) mapped to OPFS, developers obtain full SQL querying, indexes, WAL mode, and ACID transactions inside the web client.

STORAGE // TIER 4

Persistent Storage Grants

By executing navigator.storage.persist(), an application requests an exemption from automated browser cache eviction routines under disk pressure, ensuring local databases remain preserved across weeks of non-use.

Persistence Engine Throughput Comparison

Real-world benchmarks measuring 10,000 bulk insert and complex joined read transactions across client storage layers on modern desktop hardware.

Storage Mechanism 10k Batch Inserts Indexed Query Latency Memory Footprint Full ACID Support
SQLite-WASM + OPFS (Worker) 142 ms (Direct VFS) 1.2 ms ~18 MB Yes (Write-Ahead Logging)
Raw IndexedDB (Async IDB) 1,280 ms (IPC Overhead) 14.8 ms ~35 MB Partial (Limited savepoints)
LocalStorage (Sync string) Unusable (Quota 5MB) N/A (Full Scan) < 5 MB No
In-Memory JS Heap (Map/Set) 18 ms 0.3 ms High (GC Sensitive) No (Volatile on refresh)

The Safari 7-Day Script-Writable Storage Eviction Quirk

WebKit's Intelligent Tracking Prevention (ITP) applies an aggressive policy: client data written via script by origins not visited within a rolling 7-day window may be marked for automatic purging. For mission-critical local-first software, teams must guide users to install the web app to the home screen (PWA standalone mode) or explicitly invoke the Persistent Storage permission grant.

Zero-Downtime Distributed Schema Migrations

In centralized cloud software, migrations run sequentially on one database. In local-first systems, thousands of devices may run old versions for months while offline.

Strategy // Expand-Contract Distributed Migration Matrix Verified Pattern

PHASE 01

Additive Expansion

New schema fields are strictly additive and nullable. Existing fields are never renamed or deleted while older client builds remain active in the field.

PHASE 02

Dual-Writing

Updated clients read from the new structure but write compatibility projections to legacy fields to ensure older offline peers do not encounter parsing crashes.

PHASE 03

Peer Convergence

As disconnected devices reconnect and download client updates, they apply deterministic, versioned migration patches directly against their local SQLite files.

PHASE 04

Lazy Contraction

Only when telemetry verifies that obsolete client versions have reached 0% quorum are deprecated columns dropped during regular SQLite compaction passes.