Skip to content

Backend synchronization

At its core, drift is a package to access relational databases. On clients, that would typically be a SQLite3 database, which is what drift is optimized for. More recently, drift also gained stable support for PostgreSQL databases as well. This allows drift to be deployed in fullstack Dart applications, where a server uses drift to talk to a Postgres database and clients use it to manage a sqlite3 database. Thanks to utilities like DialectAwareSqlType, it is also possible to share schema definitions between the two database systems.

But regardless of whether the backend is using drift or not, synchronizing data between frontend and backend services can be complex. It is not something drift is set up to do automatically, but drift can integrate with existing solutions enabling synchronization. This page lists a few options and how they can be integrated into drift. If you have experience with alternatives that you can share, please feel free to open an issue or contribute to this page!

Manual

One approach that requires more work to configure, but also gives you the most flexibility, is to write most of the synchronization logic yourself.

For instance, synchronizing changes made locally in the app could be tracked in additional tables - perhaps with a CREATE TRIGGER statement keeping a log of changes made to database tables you want to sync. Periodically, a background job could then post this log of changes to your backend server.

Dominik Roszkowski has given a talk at Fluttercon 2023 in which he shares the approach used by Visible to sync local changes to the server. Additional approaches are also discussed in this issue and here.

Jan Zimola has published a demo for data sync using the WatermelonDB protocol for synchronization (with a Dart implementation for servers and clients). It uses code generation to automate most of the client logic.

PowerSync

Disclosure: Drift maintainer Simon Binder works at the team maintaining PowerSync. While this page only reflects his personal opinion, please keep that in mind and perhaps consult other sources on sync options for Dart too.

PowerSync is sync solution with stable and official Flutter support. Using a sync service available as a managed cloud offering or for self-hosting, you define buckets of data to make available to individual users. A client SDK for Dart/Flutter connects to this service to automatically sync changes from backend databases in real-time.

While PowerSync automatically collects client-side changes, you're still responsible for uploading these to your backend (which can then validate them). For managed Postgres databases such as Supabase, using row-level security and direct uploads from clients avoids the use of a separate backend.

PowerSync also offers a package for drift support (currently in Alpha) that will make your query streams update in real-time for changes from the backend. This example shows how PowerSync can be integrated into a drift app.

ElectricSQL

This section may be outdated

The Dart package for Electric referenced here refers to an older version of Electric SQL, the current version of Electric does not appear to be available to Dart apps.

ElectricSQL is a solution you can self-host to synchronize PostgreSQL databases with clients. Instead of having to deal with changes manually, a service receives updates from the PostgreSQL server and local sqlite3 databases. This service takes care of all the synchronization logic, with no backend changes and only simple frontend changes being required to integrate this.

There is no official Dart support yet, but there are community bindings which have great support for drift databases. This even works with stream queries - writes happening in the backend are quickly synchronized to the frontend, and will update the UI right away.

This example contains a Flutter app and a simple backend, both using drift and synchronizing their database with ElectricSQL.