Building Offline-First Mobile Applications with React Native
Mobile users lose connectivity constantly — in elevators, subways, rural areas, or just spotty networks. After building offline-first features into 15+ production apps, I've refined an architecture that makes offline not an afterthought but a first-class feature.
The Offline-First Philosophy
Offline-first doesn't mean "works without internet." It means the app always works, and connectivity is a bonus that enables syncing. The user should never see a loading spinner waiting for a network request to complete before they can interact with the app.
This mental shift changes everything: data is read from local storage first, API calls happen in the background, and conflicts are resolved gracefully.
The Architecture Stack
Local Database: WatermelonDB or SQLite
For offline-first apps with complex data relationships, WatermelonDB is our go-to. It's built on SQLite but designed for React Native with lazy loading, observable queries, and a sync engine. For simpler key-value needs, MMKV (a fast key-value store from WeChat) handles hundreds of thousands of reads/writes per second.
Sync Engine
The sync engine is the core of offline-first architecture. Our approach:
- Timestamp-based sync: Every record has
created_atandupdated_attimestamps. On sync, the client sends its last sync timestamp and receives all changes since then. - Queue-based mutations: Create, update, and delete operations are queued locally. When connectivity returns, the queue is processed in order.
- Conflict resolution: Last-write-wins for simple cases. For collaborative data, we use field-level merging — if User A changes the title and User B changes the description, both changes apply.
Network Detection
Use @react-native-community/netinfo to detect connectivity state. But don't rely solely on network detection — a device can be "connected" to WiFi but have no internet access. Always design your sync to handle failures gracefully regardless of detected network state.
Caching Strategies
- Cache-first for reads: Always serve data from the local database. Update the cache when fresh data arrives from the API. The user sees instant results, and the data refreshes seamlessly.
- Queue-first for writes: All mutations go to the local database and a sync queue immediately. The UI updates instantly (optimistic update). Background sync pushes changes to the server.
- Image caching: Use
react-native-fast-imagefor automatic image caching with LRU eviction. For critical images (profile photos, product images), pre-cache them during sync.
Handling Edge Cases
- Queue overflow: If a user makes hundreds of offline changes, the sync queue can grow large. Process it in batches to avoid overwhelming the server on reconnection.
- Token expiration: Auth tokens expire during offline periods. The sync engine must handle 401 responses by re-authenticating before retrying the queue.
- Schema migrations: When your app updates and the local database schema changes, migrate existing offline data without data loss. WatermelonDB handles this with versioned schema migrations.
- Conflict UI: For critical conflicts (two users editing the same record offline), show a resolution UI rather than silently picking a winner.
Real-World Impact
Our field service app served technicians working in areas with no cell coverage:
- Zero data loss across 10,000+ offline work orders
- 3x faster perceived performance (no network wait times)
- 95% user satisfaction improvement from the previous online-only version
Building a mobile app that needs to work offline? Let's architect it together.