Mobile Offline-Sync Architect
Expert in building offline-first mobile applications with local databases, conflict resolution, and reliable background synchronization.
Activation Triggers
Activate on: "offline sync", "offline-first app", "local database mobile", "WatermelonDB", "SQLite sync", "CRDT conflict resolution", "background sync", "mobile data persistence", "op-sqlite"
NOT for: Server databases → data-pipeline-engineer | Web caching → pwa-architect | API design → api-architect
Quick Start
- Choose local database — op-sqlite (performance), WatermelonDB (React Native lazy loading), or Realm (object-oriented)
- Design sync schema — add
updated_at, deleted_at, sync_status columns to all synced tables
- Implement conflict resolution — last-write-wins for simple cases, CRDT for collaborative editing
- Build sync engine — pull-push protocol with delta sync (only changed records)
- Add background sync — React Native Background Fetch or WorkManager for periodic sync
Core Capabilities
| Domain |
Technologies |
| Local DB |
op-sqlite, WatermelonDB, Realm, expo-sqlite |
| Sync Protocols |
Delta sync, CRDT (Yjs, Automerge), operational transform |
| Conflict Resolution |
Last-write-wins, merge functions, CRDT automatic merge |
| Background Sync |
react-native-background-fetch, expo-background-fetch |
| Platforms |
PowerSync, ElectricSQL, Replicache, custom sync engines |
Architecture Patterns
Offline-First Sync Architecture
┌─────────────────────────────────┐
│ Mobile App │
│ ┌──────────┐ ┌─────────────┐ │
│ │ UI Layer │──│ Sync Engine │ │
│ └──────────┘ └──────┬──────┘ │
│ │ │
│ ┌────────────────────┴───────┐ │
│ │ Local SQLite DB │ │
│ │ (source of truth offline) │ │
│ └────────────────────────────┘ │
└────────────────┬────────────────┘
│ Background sync
│ (when online)
▼
┌────────────────────────────────┐
│ Server │
│ ┌──────────┐ ┌────────────┐ │
│ │ Sync API │──│ Server DB │ │
│ │ /sync │ │ (Postgres) │ │
│ └──────────┘ └────────────┘ │
└────────────────────────────────┘
Pull: GET /sync?since=<timestamp> → changed records
Push: POST /sync { changes: [...] } → conflicts
Delta Sync Protocol
interface SyncRequest {
lastSyncTimestamp: string;
changes: ChangeSet[]; // Local changes since last sync
}
interface ChangeSet {
table: string;
created: Record[];
updated: Record[];
deleted: { id: string; deleted_at: string }[];
}
interface SyncResponse {
serverTimestamp: string;
changes: ChangeSet[]; // Server changes since client's lastSync
conflicts: Conflict[]; // Records changed on both sides
}
// Conflict resolution strategy
function resolveConflict(local: Record, server: Record): Record {
// Strategy 1: Last-write-wins (simple, data loss possible)
return local.updated_at > server.updated_at ? local : server;
// Strategy 2: Field-level merge (no data loss for non-conflicting fields)
// return mergeFields(local, server, base);
// Strategy 3: CRDT (automatic, no conflicts by design)
// return crdtMerge(local, server);
}
WatermelonDB Sync Implementation
import { synchronize } from '@nozbe/watermelondb/sync';
async function syncDatabase() {
await synchronize({
database,
pullChanges: async ({ lastPulledAt }) => {
const response = await api.get('/sync', {
params: { since: lastPulledAt },
});
return {
changes: response.data.changes,
timestamp: response.data.serverTimestamp,
};
},
pushChanges: async ({ changes, lastPulledAt }) => {
await api.post('/sync', { changes, lastPulledAt });
},
migrationsEnabledAtVersion: 1,
});
}
Anti-Patterns
- Online-first with offline cache — treating offline as an afterthought. Design offline-first: the local DB is the source of truth; sync is background reconciliation.
- Full table sync — downloading entire tables on every sync. Use delta sync with timestamps or change tracking to transfer only modified records.
- Ignoring conflict resolution — assuming conflicts will not happen. Define a clear strategy (LWW, field merge, or CRDT) and handle conflicts explicitly.
- Sync on main thread — blocking the UI during synchronization. Run sync in a background thread or process with progress callbacks.
- No offline indicator — users do not know when they are offline. Show connectivity status and sync state (synced, syncing, pending changes) in the UI.
Quality Checklist
[ ] Local database chosen and configured (op-sqlite, WatermelonDB, Realm)
[ ] Sync schema includes updated_at, deleted_at, sync_status columns
[ ] Delta sync protocol implemented (not full-table)
[ ] Conflict resolution strategy defined and tested
[ ] Background sync configured (periodic + on-reconnect)
[ ] Offline indicator visible in UI
[ ] Pending local changes count displayed
[ ] Sync errors handled gracefully (retry with exponential backoff)
[ ] Data integrity: no data loss during conflict resolution
[ ] Large dataset performance tested (10K+ records)
[ ] Soft deletes used (deleted_at, not hard DELETE)
[ ] Sync works after app kill and restart
1---2license: Apache-2.03name: mobile-offline-sync-architect4description: "Mobile offline-first architecture with local databases, CRDT conflict resolution, and background sync. Activate on: offline sync, offline-first, local database, WatermelonDB, SQLite, CRDT, conflict resolution, background sync, mobile persistence. NOT for: server-side databases (use data-pipeline-engineer), web caching strategies (use pwa-architect), API design (use api-architect)."5allowed-tools: Read,Write,Edit,Bash(docker:*,kubectl:*,terraform:*,npm:*,npx:*)6category: Mobile Development7tags:8 - offline9 - sync10 - sqlite11 - mobile12pairs-with:13 - skill: react-native-architect14 reason: Offline sync is a core mobile architecture concern15 - skill: data-pipeline-engineer16 reason: Server-side data pipeline must support sync protocols17metadata:18 source: https://skillmd.com/skills/curiositech/mobile-offline-sync-architect19---2021# Mobile Offline-Sync Architect2223Expert in building offline-first mobile applications with local databases, conflict resolution, and reliable background synchronization.2425## Activation Triggers2627**Activate on:** "offline sync", "offline-first app", "local database mobile", "WatermelonDB", "SQLite sync", "CRDT conflict resolution", "background sync", "mobile data persistence", "op-sqlite"2829**NOT for:** Server databases → `data-pipeline-engineer` | Web caching → `pwa-architect` | API design → `api-architect`3031## Quick Start32331. **Choose local database** — op-sqlite (performance), WatermelonDB (React Native lazy loading), or Realm (object-oriented)342. **Design sync schema** — add `updated_at`, `deleted_at`, `sync_status` columns to all synced tables353. **Implement conflict resolution** — last-write-wins for simple cases, CRDT for collaborative editing364. **Build sync engine** — pull-push protocol with delta sync (only changed records)375. **Add background sync** — React Native Background Fetch or WorkManager for periodic sync3839## Core Capabilities4041| Domain | Technologies |42|--------|-------------|43| **Local DB** | op-sqlite, WatermelonDB, Realm, expo-sqlite |44| **Sync Protocols** | Delta sync, CRDT (Yjs, Automerge), operational transform |45| **Conflict Resolution** | Last-write-wins, merge functions, CRDT automatic merge |46| **Background Sync** | react-native-background-fetch, expo-background-fetch |47| **Platforms** | PowerSync, ElectricSQL, Replicache, custom sync engines |4849## Architecture Patterns5051### Offline-First Sync Architecture5253```54┌─────────────────────────────────┐55│ Mobile App │56│ ┌──────────┐ ┌─────────────┐ │57│ │ UI Layer │──│ Sync Engine │ │58│ └──────────┘ └──────┬──────┘ │59│ │ │60│ ┌────────────────────┴───────┐ │61│ │ Local SQLite DB │ │62│ │ (source of truth offline) │ │63│ └────────────────────────────┘ │64└────────────────┬────────────────┘65 │ Background sync66 │ (when online)67 ▼68┌────────────────────────────────┐69│ Server │70│ ┌──────────┐ ┌────────────┐ │71│ │ Sync API │──│ Server DB │ │72│ │ /sync │ │ (Postgres) │ │73│ └──────────┘ └────────────┘ │74└────────────────────────────────┘7576Pull: GET /sync?since=<timestamp> → changed records77Push: POST /sync { changes: [...] } → conflicts78```7980### Delta Sync Protocol8182```typescript83interface SyncRequest {84 lastSyncTimestamp: string;85 changes: ChangeSet[]; // Local changes since last sync86}8788interface ChangeSet {89 table: string;90 created: Record[];91 updated: Record[];92 deleted: { id: string; deleted_at: string }[];93}9495interface SyncResponse {96 serverTimestamp: string;97 changes: ChangeSet[]; // Server changes since client's lastSync98 conflicts: Conflict[]; // Records changed on both sides99}100101// Conflict resolution strategy102function resolveConflict(local: Record, server: Record): Record {103 // Strategy 1: Last-write-wins (simple, data loss possible)104 return local.updated_at > server.updated_at ? local : server;105106 // Strategy 2: Field-level merge (no data loss for non-conflicting fields)107 // return mergeFields(local, server, base);108109 // Strategy 3: CRDT (automatic, no conflicts by design)110 // return crdtMerge(local, server);111}112```113114### WatermelonDB Sync Implementation115116```typescript117import { synchronize } from '@nozbe/watermelondb/sync';118119async function syncDatabase() {120 await synchronize({121 database,122 pullChanges: async ({ lastPulledAt }) => {123 const response = await api.get('/sync', {124 params: { since: lastPulledAt },125 });126 return {127 changes: response.data.changes,128 timestamp: response.data.serverTimestamp,129 };130 },131 pushChanges: async ({ changes, lastPulledAt }) => {132 await api.post('/sync', { changes, lastPulledAt });133 },134 migrationsEnabledAtVersion: 1,135 });136}137```138139## Anti-Patterns1401411. **Online-first with offline cache** — treating offline as an afterthought. Design offline-first: the local DB is the source of truth; sync is background reconciliation.1422. **Full table sync** — downloading entire tables on every sync. Use delta sync with timestamps or change tracking to transfer only modified records.1433. **Ignoring conflict resolution** — assuming conflicts will not happen. Define a clear strategy (LWW, field merge, or CRDT) and handle conflicts explicitly.1444. **Sync on main thread** — blocking the UI during synchronization. Run sync in a background thread or process with progress callbacks.1455. **No offline indicator** — users do not know when they are offline. Show connectivity status and sync state (synced, syncing, pending changes) in the UI.146147## Quality Checklist148149```150[ ] Local database chosen and configured (op-sqlite, WatermelonDB, Realm)151[ ] Sync schema includes updated_at, deleted_at, sync_status columns152[ ] Delta sync protocol implemented (not full-table)153[ ] Conflict resolution strategy defined and tested154[ ] Background sync configured (periodic + on-reconnect)155[ ] Offline indicator visible in UI156[ ] Pending local changes count displayed157[ ] Sync errors handled gracefully (retry with exponential backoff)158[ ] Data integrity: no data loss during conflict resolution159[ ] Large dataset performance tested (10K+ records)160[ ] Soft deletes used (deleted_at, not hard DELETE)161[ ] Sync works after app kill and restart162```