Amplify DataStore overview - Amplify DataStore: Use Cases and Implementation

Amplify DataStore overview

Amplify DataStore allows your developers to focus on modeling their data and access patterns, adding authorization rules, and business logic where necessary. The infrastructure deployment and management are a function of these developer inputs. It is automatically deployed, monitored, and managed by the Amplify Framework including code generation, command line interface (CLI) rollouts, and runtime logic.

All DataStore operations are local first. This means that when you run a query, it returns results from the local system, which can be sorted and filtered. The same is true for mutations or observations to data. So, no network latencies or constraints on the backend are present. Operations are run for your developers to integrate into your application. However, they still have control to act upon things when the local store integrates back with the rest of the system, such as reacting to data updates or write conflicts when merges take place.

Syncing takes place without any needed effort, including real-time updates when the device is online. These updates are immediately present in any of your queries running locally or on data being observed. Similarly, devices transitioning from offline to online states perform delta synchronization on your behalf. This gives you control to perform logical actions if your mutations conflict with writes that took place to the backend.

Your developers don’t need to worry about "subscribing to web sockets on the server" or "querying for the latest changes". Instead, they use the DataStore API against their local data and all these things happen automatically. So, all clients in the system behave in an eventually consistent manner and will converge to the latest records that are written to your cloud database.

Being an on-device persistent repository, DataStore allows developers to interact with local data while synchronizing it to the cloud. It leverages GraphQL to facilitate the data modeling process, providing authorization rules and business logic when needed. This is offered to developers through the Amplify CLI as well as by using the GraphQL Transformer.

Once the schema is defined, domain native structures called models, are generated. Your developers can then use the DataStore API to save, query, update, delete, or observe changes. The models are then passed to a Storage Engine, which is responsible for managing a repository of models. The engine contains a Storage Adapter, which provides a bridge for popular implementations, such as SQLite and IndexedDB.

Local storage model

Figure 1 – Local storage model

To sync the models to the cloud, developers can use the Amplify CLI to create an AWS AppSync backend with Amazon DynamoDB tables that match the schema created at the application. Even though DynamoDB is the default database used by DataStore, the developer can choose another database to store the data by creating a custom AWS AppSync Resolver and data sources. Once the application starts interacting with the DataStore API operations, DataStore starts an instance of its Sync Engine, which will then interface with the Storage Engine to identify updates from the model repository.

To manage updates from the local repository to the cloud and vice versa, both the Sync Engine and the DataStore API subscribe to events published by the Sync Engine using the Observer pattern. This is how the Sync Engine knows how to communicate data changes generated by the application to the cloud. Similarly, this also allows developers to use the DataStore API to identify data changes that happened on the cloud, generated by other users that are manipulating applications that share the same underlying backend.

Cloud sync process

Figure 2 – Cloud sync process

If data synchronization to the cloud is enabled through AWS AppSync, multiple versions of the same object can exist on the client and on the server. This creates a need for the object to be resolved on the cloud, so a single version exists. When concurrent updates are sent from multiple clients at the same time, developers can configure conflict resolution to create strategies to define how the data is going to be stored. At this point, DataStore will apply conflict detection and resolution strategies to the data. Developers can choose between Automerge, Optimistic Concurrency, and Lambda.

Amplify DataStore supports iOS, Android, Flutter, and client JavaScript frameworks. This means that developers can build a single data schema design for multi-platform applications. From that schema, the Amplify CLI can generate models that fit solutions built in all these platforms.