Page updated Jan 16, 2024

Conflict resolution

If data synchronization is enabled via AppSync, there can be different versions of the same object on the client and server. Multiple clients may have updated their respective copies of an object. DataStore will converge different object versions by applying conflict detection and resolution strategies. The default resolution is called Auto Merge. This strategy allows collections to grow, and prefers server-side versions of single-field data. Other strategies include Optimistic Concurrency control and Custom Lambda functions. For more information, see the AWS AppSync documentation on conflict handling.

Custom conflict resolution

To select a different conflict resolution strategy, navigate into your project from a terminal and run amplify update api. Choose Conflict resolution strategy to change the conflict detection and resolution strategies.

? Please select from one of the below mentioned services: `GraphQL` ... ? Select a setting to edit `Conflict resolution strategy` ? Select the default resolution strategy Auto Merge ❯ Optimistic Concurrency Custom Lambda Learn More
1? Please select from one of the below mentioned services:
2 `GraphQL`
3...
4? Select a setting to edit
5 `Conflict resolution strategy`
6? Select the default resolution strategy
7 Auto Merge
8❯ Optimistic Concurrency
9 Custom Lambda
10 Learn More

Per model configuration

Note that this flow will also allow you to change the strategy on each individual GraphQL type, though it is recommended to use the same strategy for your whole schema unless you have an advanced use case:

? Do you want to override default per model settings? Yes ? Select the models from below: ❯◉ Post ◯ PostEditor ◯ User ? Select the resolution strategy for Post model Custom Lambda ? Select from the options below (Use arrow keys) ❯ Create a new Lambda Function Existing Lambda Function
1? Do you want to override default per model settings? Yes
2? Select the models from below:
3❯◉ Post
4PostEditor
5User
6
7? Select the resolution strategy for Post model Custom Lambda
8? Select from the options below (Use arrow keys)
9Create a new Lambda Function
10 Existing Lambda Function

Custom configuration

DataStore has a few optional configurations, such as the ability to specify a custom handler for error messages that take place in any part of the system. You can also specify a custom conflict handler that runs if a mutation is rejected by AWS AppSync during one of the conflict resolution strategies.

Finally you can configure the number of records to sync as an upper bound on items (per-Model) which will be stored locally on the device, as well as a custom interval in minutes which is an override of the default 24 hour "base query" which runs as part of the Delta Sync process.

Example

The code below illustrates a conflict resolution handler for the Post model that retries a mutation with the same title, but the most recent remote data for all other fields. The conflict resolution handler discards conflicts for all other models (by returning the DISCARD symbol imported from @aws-amplify/datastore).

import { DISCARD, PersistentModelConstructor, } from "@aws-amplify/datastore"; DataStore.configure({ errorHandler: (error) => { console.warn("Unrecoverable error", { error }); }, conflictHandler: async (data) => { // Example conflict handler if (data.modelConstructor as PersistentModelConstructor<Todo> === Todo) { const remoteModel = data.remoteModel as Todo; const localModel = data.localModel as Todo; const newModel = Todo.copyOf(remoteModel, (d) => { d.name = localModel.name; }); return newModel; } return DISCARD; }, maxRecordsToSync: 30000, fullSyncInterval: 60, // minutes });
1import {
2 DISCARD,
3 PersistentModelConstructor,
4} from "@aws-amplify/datastore";
5
6DataStore.configure({
7 errorHandler: (error) => {
8 console.warn("Unrecoverable error", { error });
9 },
10 conflictHandler: async (data) => {
11 // Example conflict handler
12 if (data.modelConstructor as PersistentModelConstructor<Todo> === Todo) {
13 const remoteModel = data.remoteModel as Todo;
14 const localModel = data.localModel as Todo;
15
16 const newModel = Todo.copyOf(remoteModel, (d) => {
17 d.name = localModel.name;
18 });
19
20 return newModel;
21 }
22
23 return DISCARD;
24 },
25 maxRecordsToSync: 30000,
26 fullSyncInterval: 60, // minutes
27});
import { DISCARD } from "@aws-amplify/datastore"; DataStore.configure({ errorHandler: (error) => { console.warn("Unrecoverable error", { error }); }, conflictHandler: async (data) => { // Example conflict handler const modelConstructor = data.modelConstructor; if (modelConstructor === Post) { const remoteModel = data.remoteModel; const localModel = data.localModel; const newModel = modelConstructor.copyOf(remoteModel, (d) => { d.title = localModel.title; }); return newModel; } return DISCARD; }, maxRecordsToSync: 30000, fullSyncInterval: 60, // minutes });
1import { DISCARD } from "@aws-amplify/datastore";
2
3DataStore.configure({
4 errorHandler: (error) => {
5 console.warn("Unrecoverable error", { error });
6 },
7 conflictHandler: async (data) => {
8 // Example conflict handler
9 const modelConstructor = data.modelConstructor;
10 if (modelConstructor === Post) {
11 const remoteModel = data.remoteModel;
12 const localModel = data.localModel;
13 const newModel = modelConstructor.copyOf(remoteModel, (d) => {
14 d.title = localModel.title;
15 });
16 return newModel;
17 }
18
19 return DISCARD;
20 },
21 maxRecordsToSync: 30000,
22 fullSyncInterval: 60, // minutes
23});