Overview
PowerSync supports defining sync priorities, which allows you to control the sync order for different data. This is particularly useful when certain data should be available sooner than others. In Sync Streams, priorities are assigned to streams and PowerSync manages the underlying buckets internally. (In legacy Sync Rules, priorities were assigned to buckets explicitly.)AvailabilityThis feature was introduced in version 1.7.1 of the PowerSync Service, and in the following SDK versions:
Why Use Sync Priorities?
PowerSync’s standard sync protocol ensures that:- The local data view is only updated when a fully consistent checkpoint is available.
- All pending local changes must be uploaded, acknowledged, and synced back before new data is applied.
How It Works
Each bucket is assigned a priority value between 0 and 3, where:- 0 is the highest priority and has special behavior (detailed below).
- 3 is the default and lowest priority.
- Lower numbers indicate higher priority.
- Sync Streams
- Sync Rules (Legacy)
In Sync Streams, you assign priorities directly to streams. PowerSync manages buckets internally, so you don’t need to think about bucket structure. Each stream with a given priority will have its data synced at that priority level.Clients can also override the priority when subscribing:When different components subscribe to the same stream with the same parameters but different priorities, PowerSync uses the highest priority for syncing. That higher priority is kept until the subscription ends (or its TTL expires). Subscriptions with different parameters are independent and do not conflict.
Syntax and Configuration
- Sync Streams
- Sync Rules (Legacy)
In Sync Streams, set the
priority option on the stream definition:Priorities must be static and cannot depend on row values within a parameter query.
Example: Syncing Lists Before Todos
Consider a scenario where you want to display lists immediately while loading todos in the background. This approach allows users to view and interact with lists right away without waiting for todos to sync.- Sync Streams
- Sync Rules (Legacy)
lists stream syncs first (priority 1), allowing users to see and interact with their lists immediately. The todos stream syncs afterward (priority 2), loading in the background.Behavioral Considerations
- Interruption for Higher Priority Data: Syncing lower-priority data may be interrupted if new data for higher-priority streams/buckets arrives.
- Local Changes & Consistency: If local writes fail due to validation or permission issues, they are only reverted after all data has synced.
- Deleted Data: Deleted data may only be removed after all priorities have completed syncing. Future updates may improve this behavior.
- Data Ordering: Lower-priority data will never appear before higher-priority data.
Special Case: Priority 0
Priority 0 buckets sync regardless of pending uploads. For example, in a collaborative document editing app (e.g., using Yjs), each change is stored as a separate row. Since out-of-order updates don’t affect document integrity, Priority 0 can ensure immediate availability of updates. Caution: If misused, Priority 0 may cause flickering or inconsistencies, as updates could arrive out of order.Consistency Considerations
PowerSync’s full consistency guarantees only apply once all priorities have completed syncing. When higher-priority data is synced, all inserts and updates at that priority level will be consistent. However, deletes are only applied when the full sync completes, so you may still have some stale data at those priority levels. Consider the following example: Imagine a task management app where users create lists and todos. Some users have millions of todos. To improve first-load speed:- Lists are assigned Priority 1, syncing first to allow UI rendering.
- Todos are assigned Priority 2, loading in the background.
Client-Side Considerations
PowerSync’s client SDKs provide APIs to allow applications to track sync status at different priority levels. Developers can leverage these to ensure critical data is available before proceeding with UI updates or background processing. This includes:waitForFirstSync(priority: int). When passing the optionalpriorityparameter to this method, it will wait for specific priority level to complete syncing.SyncStatus.priorityStatusEntries()A list containing sync information for each priority that was seen by the PowerSync Service.SyncStatus.statusForPriority(priority: int)This method takes a fixed priority and returns the sync state for that priority by looking it up inpriorityStatusEntries.
Example
Using the above we can render a lists component only once the user’s lists (with priority 1) have completed syncing, else display a message indicating that the sync is still in progress:- Flutter: Supabase To-Do List
- Kotlin:
- Swift: Supabase To-Do List