Part 2: Designing the Core Sync Engine
How to design idempotent, resilient background synchronization using WorkManager and the Command Pattern. In Part 1 , we established the “Offline-First” mindset: the UI observes the local database. But how do we ensure user actions — likes, comments, or edits — actually reach the server without getting lost in “Lie-Fi”? Welcome to the heart of the machine: The Sync Engine. 1. The Action Outbox: Preserving Intent In a production-grade mobile sync engine design , you don’t just “call an API.” You encapsulate intent. The Outbox Pattern ensures that an action survives process death and device reboots. Kotlin Implementation: The Action Entity @Entity (tableName = "pending_actions" ) data class PendingAction ( @PrimaryKey (autoGenerate = true) val id : Long = 0 , val type : String, // e.g., "ADD_COMMENT", "UPDATE_PROFILE" val payload : String, // Versioned JSON or Protobuf val status : ActionStatus = ActionStatus.PENDING, val retryCount...