Device Sync Service

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-05-02

Problem Statement

Design a Device Synchronization Service that allows users to keep their data in sync across multiple devices (e.g., phones, tablets, laptops).


The system should ensure that whenever a user updates, deletes, or creates data (such as preferences, notes, messages, or documents) on one device, those changes are propagated

reliably and efficiently to all other devices associated with the same user account.


It should handle conflict resolution, network disruptions, and scalable synchronization for millions of users and devices.


🎯 Objective


Build a backend system that supports:


•Real-time synchronization of user data across devices.
•Offline support — local changes should sync when connectivity is restored.
•Conflict detection and resolution between device states.
•Incremental sync (only changes are synced, not full data reload).
•Versioning and data consistency.
•Scalability to support millions of devices per region.


✅ Functional Requirements


1. Device Registration


•Each user can register one or more devices (mobile, tablet, desktop, etc.).
•Each device has a unique ID and maintains its last sync timestamp.


2. Data Synchronization


•Device sends local changes (create/update/delete) to the server.
•Server merges updates and broadcasts deltas to other devices of the same user.
•Sync should be bi-directional (server ↔ client).


3. Change Tracking


•Maintain a change log for every user’s data.
•Sync only deltas since the last successful sync timestamp.
•Support partial data categories (e.g., sync settings only).


4. Conflict Resolution


•Detect when two devices modify the same record concurrently.
•Apply conflict resolution policies, such as:
•Last writer wins (based on timestamp)
•Merge resolution rules (depending on data type)
•User prompt for manual resolution (in apps like notes/doc editors)


5. Offline and Retry


•Devices can queue local updates offline.
•On reconnection, queued updates are sent and reconciled with the server.
•Sync should resume gracefully after network failure.


6. Notifications


•Push updates via WebSocket, Firebase, or APNs when data changes.
•Devices receive incremental updates in near real-time.


⚙️ Non-Functional Requirements


Attribute with Goal


Scalability

Handle 100M+ devices syncing concurrently


Availability

99.9% uptime


Latency

<200ms for sync response under normal load


Consistency

Eventual consistency across devices


Reliability

Ensure no data loss during sync interruptions


Security

Encrypt user data during transfer and at rest


Extensibility

Support new data types in future (e.g., contacts, tasks)


🧩 Core Components


Component with Description


Sync API Gateway

Entry point for sync requests (HTTP/WebSocket)


Sync Orchestrator

Manages delta computation, conflict resolution, and change propagation


Change Log Service

Stores incremental updates and timestamps for each device


Device Registry

Tracks registered devices and their last sync state


Notification Service

Pushes change notifications to connected devices


Storage Service

Persists user data (structured or document-based)


Auth Service

Validates user and device credentials


Queue/Stream Processor

Handles async updates and broadcasts changes (Kafka, Pub/Sub)


🧱 High-Level Architecture

1 2 +-------------------------+ 3 | User Devices | 4 | (Mobile / Web / PC) | 5 +-----------+--------------+ 6 | 7 +------+------+ 8 | Sync API GW | 9 +------+------+ 10 | 11 +-------------+-------------+ 12 | | 13+-------v--------+ +--------v--------+ 14| Sync Orchestrator | | Notification Svc | 15+-------+--------+ +--------+--------+ 16 | | 17+-------v--------+ +-------v--------+ 18| Change Log Svc | | Device Registry | 19+-------+--------+ +-------+--------+ 20 | | 21+-------v--------+ +-------v--------+ 22| Storage Service | | Auth Service | 23+-----------------+ +----------------+ 24 25 +--------------------------+ 26 | Message Bus (Kafka/PubSub) | 27 +--------------------------+


🔄 Data Flow Example


1. Initial Device Sync


1.Device sends sync_request(user_id, last_sync_time)
2.Server checks Change Log Service for deltas since last_sync_time
3.Returns changed records to client
4.Client applies updates and stores new last_sync_time


2. Update Propagation


1.Device A updates a note → sends to Sync Service
2.Server:
•Updates master record in DB
•Appends change to Change Log
•Publishes event to message bus
3.Notification Service pushes delta to Device B and C
4.Devices apply updates locally


3. Conflict Resolution


•If Device A and B modify the same record concurrently:
•Sync Orchestrator detects conflicting timestamps.
•Applies last-write-wins or merges content.
•Conflict entry logged for audit.


🧱 Data Model (Simplified)


Device

1 2{ 3 "device_id": "d123", 4 "user_id": "u456", 5 "last_sync_time": "2025-10-24T10:00:00Z", 6 "device_type": "mobile", 7 "status": "active" 8}

ChangeLog

1 2{ 3 "change_id": "c789", 4 "user_id": "u456", 5 "entity_type": "note", 6 "entity_id": "n321", 7 "operation": "UPDATE", 8 "delta": "{ 'title': 'Updated title' }", 9 "timestamp": "2025-10-24T10:05:00Z" 10}


⚡ Scalability & Optimization


| Challenge | Solution |

| ------------------------------ | ---------------------------------------- |

| High write load (many updates) | Use Kafka/PubSub to fan out updates |

| Frequent sync requests | Use incremental sync with delta tracking |

| Read latency | Cache recent changes in Redis |

| Conflict management | Keep vector clocks or version numbers |

| Mobile data limits | Compress payloads, sync diffs only |

| Offline sync | Queue local ops and replay later |

| Fault tolerance | Retry with exponential backoff |


🔒 Security


•Use OAuth2 or JWT tokens for authentication.
•All communications over HTTPS / WSS.
•Encrypt data at rest (AES-256).
•Device registration requires verification.
•Role-based access for admin and system components.


🧰 Tech Stack (Example)


| Layer | Technology |

| ----------------- | ------------------------------------- |

| **API Layer** | Go / Node.js / Java Spring Boot |

| **Queue/Streams** | Kafka / Google PubSub |

| **Storage** | DynamoDB / MongoDB / PostgreSQL |

| **Cache** | Redis |

| **Notification** | Firebase Cloud Messaging / WebSockets |

| **Auth** | OAuth2 / JWT |

| **Deployment** | Kubernetes / Docker |

| **Monitoring** | Prometheus + Grafana |



🧠 Key Challenges


| Challenge | Description |

| --------------------- | ------------------------------------------------------- |

| **Data Conflicts** | Multiple device writes need deterministic resolution. |

| **Offline Sync** | Handle large change sets when device comes back online. |

| **Scalability** | Billions of deltas across users/devices. |

| **Real-Time Updates** | Minimize delay between update and broadcast. |

| **Partial Failures** | Must ensure no lost updates during sync failures. |


✅ Goal:

Design a fault-tolerant, scalable, and real-time Device Sync Service that keeps user data consistent and synchronized across multiple devices — even under high concurrency, intermittent connectivity, and large-scale user activity.

Solution

Comments