Skip to content

MongoDB Atlas Device Sync for offline-first mobile apps

Use Atlas Device Sync (formerly Realm Sync) to keep a mobile app's local Realm database synchronized with Atlas — flexible sync, conflict resolution, and permissions.

Atlas Device Sync is MongoDB’s offline-first replication layer between Realm — the embedded mobile database — and Atlas in the cloud. Writes happen against the local Realm even when the device is offline; the SDK queues them and reconciles with the server when connectivity returns. It is the right tool when you need to ship a mobile app that keeps working in a parking garage and converges to a single source of truth when it gets back online. This article covers the sync model, the permissions and subscription system, and the conflict-resolution behaviour you need to understand before you ship.

How to verify

From the Atlas UI, confirm the App Services app is configured for Device Sync and that subscriptions are active:

realm-cli login --api-key="..." --private-api-key="..."
realm-cli pull --remote="<app-id>"
realm-cli logs --type=sync --tail
realm-cli sync --status

The sync status should show the number of connected devices, the bootstrap state, and any sync errors. Logs tell you whether reads/writes are succeeding per user.

What’s happening

Realm is a cross-platform mobile database (iOS, Android, Kotlin Multiplatform, .NET, JavaScript). It stores objects in a local file, with reactive APIs that update the UI when underlying data changes. Atlas Device Sync layers a sync engine on top: every local write produces a change set, the SDK streams change sets to Atlas, and a server-side translator turns them into MongoDB writes against the linked Atlas cluster.

There are two sync modes. Partition-based sync (legacy) requires a single partition key on every synced document; each device subscribes to documents matching its partition value. Flexible sync (current default) uses MongoDB query subscriptions — the device subscribes to “Tasks where assignee == me AND priority > 2”, the server streams matching documents, and the subscription updates as documents enter or leave the result set.

Conflicts are inevitable in offline-first systems. Two devices edit the same document offline; both reconnect. Atlas Device Sync resolves conflicts deterministically using operational-transformation-style merge rules: object property updates merge per-property (last writer wins per property), list inserts merge order-preservingly, counter increments merge additively. No application-side conflict resolution is needed for the standard data types — but you must design your schema so the default rules match your intent.

The procedure

  1. Stand up an Atlas cluster and an App Services app. From the Atlas UI, create an App Services app linked to the cluster you want to sync from.

  2. Define the schema. Either generate it from existing Atlas documents (Development Mode) or write it explicitly. Every synced object needs a primary key.

    {
      "title": "Task",
      "primaryKey": "_id",
      "properties": {
        "_id": "objectId",
        "title": "string",
        "assigneeId": "objectId?",
        "priority": "int",
        "owner": "string"
      }
    }
  3. Enable Flexible Sync. Configure which fields can be queried in subscriptions — only those fields can appear in client subscription queries.

    {
      "queryable_fields_names": ["owner", "priority", "assigneeId"]
    }
  4. Add a permissions rule. A user should only sync documents they own. Permissions are applied server-side at sync time.

    {
      "roles": [{
        "name": "owner",
        "apply_when": { "owner": "%%user.id" },
        "document_filters": { "read": true, "write": true }
      }]
    }
  5. From the mobile SDK, open the synced Realm and add subscriptions.

    let config = user.flexibleSyncConfiguration { subs in
      subs.append(QuerySubscription<Task>(name: "myTasks") { $0.owner == user.id })
    }
    let realm = try await Realm(configuration: config)
  6. Handle errors. Schema mismatches, permission denials, and bootstrap failures surface as sync errors that the SDK delivers via a callback. Log them, retry transient ones, and fall back to client reset for unrecoverable ones.

Operational notes

  • Flexible sync subscriptions count against limits. Each subscription is server-side state; do not subscribe to every shape variation. Keep the subscription set small and well-named.
  • Schema migrations are not free. Adding a required field requires every existing client to either be upgraded or perform a client reset (which discards local data and re-syncs from server). Plan additions as optional first, then tighten later.
  • Permissions evaluate per-document. A user changing role does not retroactively revoke access to documents already synced; force a client reset or rotate the user’s identity to ensure cleanup.
  • The link to the Atlas cluster is one-way for writes from Atlas Device Sync. A direct write to the Atlas collection (bypassing Device Sync) does not produce sync events; clients see it on next read, but conflict semantics with offline-pending changes are weaker.
  • Network protocols matter. Atlas Device Sync uses WebSockets over TLS; corporate proxies that buffer or terminate WebSockets can break sync. Test on the network environments your users will actually encounter.

Stack Harbor designs Atlas Device Sync rollouts for clients shipping field-service apps, retail point-of-sale, healthcare data collection, and other offline-first workloads — the schema, the permissions model, the bootstrap UX, and the monitoring that catches sync drift before users do. See /en/services/managed-operations/ for the engagement model around mobile-first MongoDB deployments.