Sync strategies for offline apps
Offline-first sounds simple until you try to merge two divergent states. Then it becomes a distributed systems problem wearing a mobile app costume.
The three main approaches: last-write-wins (simple, lossy), operational transforms (complex, proven by Google Docs), and CRDTs (elegant, still maturing for complex data).
For my notes app, I went with a hybrid: CRDTs for text fields, LWW for metadata, and a manual conflict UI for the rare edge case where neither works. It's not pure, but it ships.
The hardest part isn't the algorithm — it's testing. You need to simulate network partitions, clock skew, and users who somehow edit the same note on three devices simultaneously.
Key lesson: design your data model for mergeability from day one. Retrofitting sync onto an existing schema is pain. Append-only logs and immutable IDs are your friends.
If you're building offline-capable apps, start with the sync story. Everything else is UI.