Mobile Application DevelopmentUnit 712 min read
Sync & Replication: Conflict Handling, Offline-First & Data Consistency
Unit 7 of Mobile Application Development covers how mobile apps handle data synchronization across devices, manage conflicts, and replicate data offline—critical for apps like eSewa or Daraz where users switch between online/offline modes. Learn conflict resolution strategies, replication techniques, and real-world tra
TAKEAWAYS:
- Conflict resolution in mobile apps uses last-write-wins, client-driven, or server-driven strategies, each with trade-offs for offline-first apps.
- Optimistic vs. pessimistic locking determines how apps handle concurrent edits (e.g., Google Docs vs. a bank’s transaction log).
- Replication models (master-slave, peer-to-peer, hybrid) dictate how data is copied and synchronized, with latency and cost implications.
- Offline-first design relies on local storage (SQLite, IndexedDB) and sync queues to resume operations when connectivity returns.
- Eventual consistency (e.g., WhatsApp messages) vs. strong consistency (e.g., Ncell billing) balances user experience and data accuracy.
- Delta synchronization (only syncing changed data) reduces bandwidth usage, critical for low-connectivity areas like rural Nepal.
1. Why Synchronization Matters in Mobile Apps
Mobile apps often operate in intermittent connectivity—users toggle between online/offline modes. Without proper synchronization:
- Data gets lost or duplicated (e.g., a Daraz order placed offline but not synced).
- Conflicts arise when two devices edit the same record (e.g., two users updating the same eSewa transaction).
- Performance degrades if apps fetch full datasets repeatedly (e.g., NEPSE stock data).
Key Challenges
mindmap
root((Synchronization Challenges))
Intermittent Connectivity
"Offline edits pending"
"Sync queues overflow"
Data Conflicts
"Last-write-wins vs. merge strategies"
"Timestamp vs. version vectors"
Bandwidth Constraints
"Delta sync vs. full resync"
Latency
"Real-time vs. batch updates"2. Conflict Resolution Strategies
When two devices modify the same data, apps must resolve conflicts. Three common approaches:
| Strategy | How It Works | Example Use Case | Pros | Cons |
|---|---|---|---|---|
| Last-Write-Wins | The most recent edit (by timestamp) overwrites earlier ones. | WhatsApp messages, Google Keep notes. | Simple to implement. | Loses older edits silently. |
| Client-Driven | Client apps merge changes (e.g., combine text edits in Google Docs). | Collaborative editing (e.g., Figma). | Preserves all changes. | Complex merge logic. |
| Server-Driven | Server arbitrates conflicts (e.g., rejects duplicate orders on Daraz). | Banking apps (NMB, Global IME), eSewa. | Centralized control. | Requires server availability. |
Worked Example: eSewa Payment Conflict
Scenario: User A pays ₹1000 for a bus ticket via eSewa offline. User B also pays ₹1000 for the same ticket online before User A syncs. Conflict Resolution:
- Last-Write-Wins: Server keeps User B’s payment (User A’s ₹1000 is lost).
- Client-Driven: App shows both payments and lets the user choose (e.g., "Select which payment to keep").
- Server-Driven: Server rejects User A’s payment with an error: "Ticket already paid by another user."
Trace Table:
| Step | User A (Offline) | User B (Online) | Conflict Resolution | Outcome |
|---|---|---|---|---|
| 1 | Pays ₹1000 (queued) | — | — | Queue stored locally. |
| 2 | — | Pays ₹1000 (success) | Last-Write-Wins | User A’s payment is overwritten. |
| 3 | Syncs | — | Client-Driven | App prompts: "Duplicate payment?" |
3. Replication Models for Mobile Data
Replication copies data across devices/servers to improve availability. Three models:
A. Master-Slave Replication
- How it works: One master server handles writes; slave servers replicate data for reads.
- Example: Ncell’s customer database (master in Kathmandu, slaves in regional offices).
- Pros: Strong consistency, simple to implement.
- Cons: Single point of failure (master), high latency for remote slaves.
graph LR A["Master Server"] -->|"Writes"| B["Slave 1"] A -->|"Writes"| C["Slave 2"] B -->|"Reads"| D["User 1"] C -->|"Reads"| E["User 2"]
B. Peer-to-Peer (P2P) Replication
- How it works: All devices are equal; changes propagate via gossip protocols (e.g., WhatsApp).
- Example: Pathao driver apps sync ride requests directly between drivers’ phones.
- Pros: No single point of failure, works offline.
- Cons: Eventual consistency, complex conflict resolution.
graph TD A["Device 1"] -- Sync --> B["Device 2"] B -- Sync --> C["Device 3"] C -- Sync --> A
C. Hybrid Replication
- How it works: Combines master-slave (for critical data) and P2P (for non-critical data).
- Example: Daraz’s inventory system (master for orders, P2P for product catalog updates).
- Pros: Balances consistency and scalability.
- Cons: Higher implementation complexity.
Comparison Table:
| Model | Consistency | Offline Support | Example Apps |
|---|---|---|---|
| Master-Slave | Strong | No | Ncell, eSewa |
| P2P | Eventual | Yes | WhatsApp, Pathao |
| Hybrid | Mixed | Partial | Daraz, Google Drive |
4. Offline-First Design Principles
Apps like Khalti or NTC’s mobile ticketing must work offline and sync later. Key techniques:
A. Local Storage Options
| Storage Type | Use Case | Size Limit | Sync Mechanism |
|---|---|---|---|
| SQLite | Structured data (e.g., orders) | ~14 MB | Delta sync |
| IndexedDB | Large datasets (e.g., maps) | ~50% device storage | Batch sync |
| Key-Value Stores | Simple data (e.g., user prefs) | Varies | Full resync |
B. Sync Queues
When offline, apps store pending changes in a sync queue (e.g., a SQLite table or Redis list). On reconnect:
- Fetch server state.
- Merge local changes.
- Push resolved changes to the server.
Example: NTC Train Ticket Sync
sequenceDiagram
participant User
participant App
participant SyncQueue
participant Server
User->>App: Book ticket offline
App->>SyncQueue: Store {ticket_id: 123, status: "pending"}
loop Every 5 mins
App->>Server: Check connectivity
end
Server-->>App: Connection established
App->>SyncQueue: Pop pending ticket
App->>Server: Push {ticket_id: 123, status: "confirmed"}
Server-->>App: AcknowledgeC. Delta Synchronization
Instead of resyncing entire datasets, apps sync only deltas (changes since last sync). Reduces bandwidth by 90%+ for apps like NEPSE’s stock data.
Worked Example: Daraz Order Sync
- Full Sync: 10 MB of product catalog.
- Delta Sync: Only 100 KB (new products/price changes).
Trace Table:
| Step | Action | Data Transferred | Bandwidth Saved |
|---|---|---|---|
| 1 | Initial full sync | 10 MB | — |
| 2 | Daily delta sync | 100 KB | 9.9 MB |
| 3 | Weekly delta sync | 500 KB | 9.5 MB |
5. Consistency Models
Trade-offs between consistency, availability, and partition tolerance (CAP theorem).
| Model | Definition | Example Apps | Trade-off |
|---|---|---|---|
| Strong | All reads return the most recent write. | Banking apps (NMB) | High latency, low availability. |
| Eventual | Conflicts resolve over time (e.g., WhatsApp messages). | WhatsApp, Pathao | Low latency, data may diverge. |
| Causal | Preserves causality (e.g., "A’s edit must appear before B’s"). | Google Docs | Complex to implement. |
6. Real-World Applications
A. eSewa: Optimistic Locking for Payments
- Problem: Users may edit payment details offline.
- Solution: Uses version vectors to track edit sequences.
- If two users edit the same transaction, eSewa shows a merge conflict screen.
- Result: No silent data loss, but requires user intervention.
B. Pathao: P2P Replication for Ride Requests
- Problem: Drivers need real-time ride updates, even offline.
- Solution: Ride requests replicate via gossip protocol between drivers’ phones.
- If two drivers accept the same ride, the server resolves it via last-write-wins (timestamp-based).
- Result: Faster response times in low-connectivity areas.
C. NEPSE: Delta Sync for Stock Data
- Problem: Full stock data syncs (100 MB) are too slow for mobile.
- Solution: Only syncs price changes and new listings (delta sync).
- Result: 95% bandwidth reduction; users see near-real-time updates.
7. Algorithms for Conflict Resolution
A. Merge Algorithm (Client-Driven)
Used in apps like Google Docs to combine edits.
def merge_changes(local_data, server_data, conflict_resolver):
merged = server_data.copy() # Start with server as base
for local_change in local_data:
if local_change["id"] not in merged:
merged.append(local_change)
else:
# Resolve conflict (e.g., take latest timestamp)
server_idx = next(i for i, x in enumerate(merged) if x["id"] == local_change["id"])
if local_change["timestamp"] > merged[server_idx]["timestamp"]:
merged[server_idx] = local_change
else:
conflict_resolver(merged[server_idx], local_change)
return merged
Trace Example:
| Step | local_data | server_data | Merged Data |
|---|---|---|---|
| 1 | [{"id": 1, "text": "Hello", "ts": 100}] | [{"id": 1, "text": "Hi", "ts": 50}] | [{"id": 1, "text": "Hello", "ts": 100}] |
| 2 | [{"id": 2, "text": "World", "ts": 200}] | [{"id": 1, "text": "Hello", "ts": 100}] | [{"id": 1, "text": "Hello"}, {"id": 2, "text": "World"}] |
B. Timestamp-Based Conflict Resolution
Used in WhatsApp to order messages.
flowchart TD A["Compare timestamps"] -->|"Local > Server"| B["Use local change"] A -->|"Server > Local"| C["Use server change"] A -->|"Equal"| D["Prompt user to choose"]
Exam Tip
What Examiners Look For
- Definitions: Clearly distinguish between strong consistency, eventual consistency, and delta sync.
- Trade-offs: For each replication model, explain one advantage and one disadvantage (e.g., "Master-slave is consistent but fails if the master crashes").
- Real-World Mapping: Relate concepts to Nepali apps (e.g., "eSewa uses client-driven conflict resolution for payments").
- Diagrams: Draw before/after sync states (e.g., SQLite database after a merge).
- Code Traces: Show step-by-step execution of a sync algorithm (e.g., merge function table).
Common Pitfalls
- Assuming all apps use strong consistency: Most mobile apps (e.g., WhatsApp) use eventual consistency.
- Ignoring offline queues: Many students forget to mention how apps store pending changes.
- Overcomplicating conflict resolution: Examiners prefer simple strategies (last-write-wins) over complex merge logic unless asked.
High-Score Answer Structure
- Define the concept (e.g., "Delta sync minimizes bandwidth by transferring only changed data").
- Draw a diagram (e.g., before/after sync state).
- Give a Nepali example (e.g., "NTC uses delta sync for train schedules").
- Compare with another method (e.g., "vs. full sync, which wastes bandwidth").
- List pros/cons in a table.
Based on the TU BSc CSIT syllabus for Mobile Application Development, unit 7.
Discussion
Loading…