Realm Sync Overview¶
On this page
What is Realm Sync?¶
Most mobile apps are data-dependent. Eventually, many apps need to share that data with other apps and the outside world. Realm Sync is an out-of-the-box that meets this need.
Consider a few examples:
- A team collaborates in real-time on a shared document across the Internet.
- A front-line worker fills out a report while working somewhere with low or no network connectivity.
- An IoT device uploads its sensor data to the cloud.
Traditionally, mobile app developers use an HTTP client to manually communicate with an endpoint. This means that every mobile app needs to solve the following problems for every client platform:
- a consistent schema of objects
- gracefully handling network failures and slowdowns
- resolving conflicts between changes to the same data on different clients
Realm Sync handles network access and conflict resolution in a background thread of your application, so your application logic stays the same regardless of network conditions. The Realm data model makes it easy to manage a consistent schema of objects in your database and across all client platforms.
Using Realm Sync in Your App¶
How Realm Sync Works¶
Realm Sync is built on 3 main pillars:
Realm Sync is built on the assumption that connectivity will drop. We call this mentality offline-first. After you make changes to the local realm on the client device, the Realm SDK automatically sends the changes to the server as soon as possible. Likewise, the Realm SDK automatically receives changes from the server and integrates those changes into the local realm. In effect, you continue to work with the local realm without moment-to-moment concern for network connectivity or lack thereof.
The offline-first design leads to one of the main benefits of MongoDB Realm: a seamless user experience. Users interact with local data with zero latency regardless of their moment-to-moment connectivity, and because Realm Sync is built into the Realm SDK, you do not need to write your own synchronization protocol and deal with connectivity issues when synchronizing data.
The main difficulty with offline-first data is ensuring that devices that are offline for extended periods are able to resolve potential conflicts with changes made to the same data. Ultimately, every client device must arrive at the same state when all changes have been synced. This is why Realm Sync includes automatic conflict resolution.
Realm's Live objects means that you can update data in one place (a mobile app, for example), and that data is updated everywhere. Live objects always reflect the latest data stored in Realm Database. This concept extends across devices: updates on one device sync to the corresponding objects on other devices. Notifications based on these updates make it easy to update the application UI to react to local and remote changes.
MongoDB Realm's sync conflict resolution engine is deterministic. Changes received out-of-order eventually converge on the same state across the server and all clients. As such, Realm Sync is strongly eventually consistent.
From MongoDB Realm's perspective, changesets may arrive any time that connectivity allows. There is no guarantee that an earlier-timestamped changeset from one client actually arrives before a later-timestamped changeset from another client. As a result, the server has to be able to process events out-of-order. MongoDB Realm keeps a per-realm transaction history to deal with the out-of-order nature of messages.
In simple terms, Realm Sync's conflict resolution comes down to last write wins. Realm Sync also uses more sophisticated techniques like operational transform to handle, for example, insertions into lists.
We recommend following these best practices to ensure optimal Realm app performance:
- Deployment model and geographic regions:
- Use a local Realm deployment model when building a Realm Sync application. Configure your Realm app and MongoDB data source to run within the same geographic region and cloud provider.
- Atlas oplog:
- Realm Sync requires access to the MongoDB Atlas oplog. For best results, keep 48 hours of oplog for a cluster using Sync. For more information, see Atlas oplog.
- Realm Sync Schema Data Consistency
- If you create or modify Realm Sync documents using another tool, such as the
mongoshell or MongoDB Compass, be sure the documents validate against the Sync object schema. For more information, see Realm Sync Schema Data Consistency.
- Realm Sync is the part of realm that handles data synchronization between a Realm Database on one or more clients and MongoDB Atlas.
- Realm Sync enables offline-first app development by handling network loss and recovery automatically.
- Realm Sync has a built-in conflict resolution engine that guarantees strong eventual consistency.
- Realm Sync is included in the client SDKs to simplify app development.