Realm Sync Overview¶
To sync data across devices, you can enable Realm Sync for your Realm app. Realm Sync automatically handles complex and tedious issues like data serialization, network recovery, and conflict resolution, so you can focus on the actual business logic of your app.
To understand how best to use Realm Sync, it's important to understand the problem Sync aims to address.
In order to use Realm Sync, your Atlas cluster must use MongoDB version 4.4. When setting up your cluster, select MongoDB 4.4 from the dropdown menu under Additional Settings.
The Connectivity Problem¶
Realm Database is a local, mobile database in which your app can store data. Eventually, most apps need to share that data with the outside world. 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, developers use an HTTP client in a mobile app to manually communicate with a REST API over the Internet. 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.
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 some of the main benefits of MongoDB Realm:
- Seamless User Experience:
- Users interact with local data with zero latency regardless of their moment-to-moment connectivity.
- Live Objects Across Devices:
- MongoDB Realm's live objects 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.
- It's Automatic:
- 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 sync protocol is the same across platforms. MongoDB Realm handles passing the objects across the network automatically, so there is no need to map from one platform's database to another or serialize and deserialize objects in order to send and receive them over the network.
Despite these advantages, offline-first design introduces one problem: if client devices can remain offline while changes are made locally and remotely, they must share those changes when those client devices come back online. To make matters more complicated, every client device must arrive at the same state when all changes have been synced. This is why Realm Sync includes automatic conflict resolution.
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.
- 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.