For about two decades, apps were thin clients: a screen that shows whatever the server's database says. Every tap waits on the network. Local-first flips that. The authoritative copy of the data lives on the user's device, and the cloud becomes a durable relay and backup peer.
The promise is simple: the app works offline by default, responds instantly, and the user keeps control of their data. Here is the stack that makes it work, one layer at a time.
Layer 1: a real local database
The UI reads and writes a local database, never the network. In Flutter that means a fast local store such as Isar or SQLite. On the web it means SQLite compiled to WebAssembly on top of the Origin Private File System (OPFS), which gives near-native disk I/O in a sandbox, and handles gigabytes of relational data.
Your widgets watch a stream from the database, so updates appear instantly with no loading spinner:
StreamBuilder<List<Note>>(
stream: notesRepo.watchAll(), // reads the local database
builder: (context, snap) {
final notes = snap.data ?? const [];
return ListView(children: [for (final n in notes) NoteTile(n)]);
},
)
A write goes to the same local database. Nothing here waits for a server.
Layer 2: a sync engine
Something has to move changes between devices. A sync engine sits between the local database and the remote one, captures local changes, streams them to the server in the background, and pulls down merged updates from other users. The source article names ElectricSQL and Replicache as examples.
Layer 3: conflict resolution with CRDTs
Two people edit the same record on airplanes with no Wi-Fi. When they land, the system must merge both edits without losing data. Conflict-free Replicated Data Types (CRDTs) are built for this. They are commutative, associative and idempotent, so as long as every peer eventually receives the same operations, they all converge on the same state, whatever order the packets arrive in.
There are two flavours:
- State-based: send the whole structure (or a large delta). Simpler, but can be heavy on bandwidth.
- Operation-based: send only the atomic operation, such as "insert character X at index 5". Efficient for frequent real-time sync.
Writing your own CRDT engine is specialized work. Libraries like Automerge and Yjs exist so you don't have to.
Layer 4: the cloud as a peer
The server stops being the master of state. Its jobs become:
- Durability: a secure backup if a device is lost.
- Discovery: helping devices find each other for peer-to-peer sync over WebRTC.
- Authorization: verifying that a user has the right to push a change to a shared dataset.
Because the server doesn't see every action as it happens, authorization has to travel with the data. Each change is cryptographically signed with a private key held on the device, and peers verify the signature when it syncs.
The two hard problems
Partial sync. A phone can't hold a ten-gigabyte production database. Sync shapes (also called dynamic subsets) define which data goes to the device. For example, a farm app might sync only the crops for the farm the user is standing in, and a member manager might cache only active members. The catch: plan the schema so you don't leave a required foreign key behind on the server.
Schema migrations. You can't lock the server and run one migration when thousands of users run five different app versions, each with its own local database. Local schemas must tolerate unknown fields from newer versions, and the migration logic lives in the app and runs when it updates. Some teams use "lenses", bidirectional transformations that map records between schema versions.
Should you adopt it?
Local-first adds real complexity: sync, partial sync and offline migrations. In return you get an app that is instant, private and resilient to network failures. If your users work in basements, on planes, or on bad mobile data, that trade is usually worth it. If every user is always online and the data is small, a plain client-server app may be enough.
The framing to keep: treat the network as an optional optimization, not a requirement.
Further reading: The architecture of local-first development
Where would local-first help your app most, and where would it be overkill?
Top comments (0)