Replies: 1 comment 1 reply
-
Is there a default TTL? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Background
The PowerSync sync system was initially designed to sync your entire set of data up-front, and keep it up-to-date in a consistent way. This is great for offline-first applications, where you do need the entire set of data available. However, there are many cases where there is too much data to sync everything, so we need mechanisms to only sync the data that the user will need right now.
Status
2025-09-01:
powersync-sqlite-core
- see Sync streams powersync-sqlite-core#112.Client SDK support is pending. See:
Current workarounds
Client parameters is one workaround currently available. It can be used to specify the data the user needs, for example a
project_ids
array.An issue is that it becomes difficult in client applications to track and build these arrays. This is especially true in web applications where the user may have multiple tabs open, with each tab needing different sets of data.
Proposal
To handle these use cases, we are overhauling sync rules to "sync streams". A developer may specify multiple stream definitions, and a user may subscribe to each stream one or more times, with different sets of parameters.
An Example
Streams can be defined similar to sync rules:
Then on the client:
Subscriptions can be unsubscribed directly, or automatically when the app / tab closes. Each has a configurable
ttl
that it stays active after unsubscribing, to effectively keep a warm cache of recently-accessed data.Subscriptions are designed to be automatically managed by UI components, for example with React hooks:
The above example will automatically subscribe to the
issue_comments
stream with the relevant issue id when the component is first rendered. When the component is unmounted, the subscription is cancelled. This doesn't mean that data is immediately deleted, however. Instead, a TTL for subscriptions ensures that if the component becomes active again in the near future, all data will be there already.Stream definition syntax
Sync streams will be supported alongside the legacy Sync Rules / bucket definitions for the foreseeable future, although we'll recommend migrating to sync streams once it's stable.
The syntax is as follows:
Supported syntax in the query is slightly different from bucket definitions. Where bucket definitions had separate
parameter
anddata
queries, sync streams only have a singlequery
. Instead of parameter queries, sync streams support a limited form of subqueries, for example:To a large extent, the same underlying bucket system is used, and the same functionality is available as before with parameter queries, while being closer to plain SQL.
Accessing parameters is slightly different from bucket definitions:
Client-side syntax
The specific syntax will depend on the SDK, but in general each SDK should support:
db.syncStream(name, [params])
returns aSyncStream
instance, which can be used tosubscribe()
to that stream.subscribe()
on aSyncStream
returns aSyncStreamSubscription
, giving you access towaitForFirstSync()
andunsubscribe()
on that subscription.SyncStatus
interface includes a list ofSyncSubscriptionDefinition
s, describing all streams that the client is subscribed to (either due to an explicit subscriptions or because the stream hasauto_subscribe: true
). That interface also reports per-stream download progress.ttl
(time-to-live). After callingunsubscribe()
, or when the page/app closes, the stream will keep on syncing for thettl
duration, effectively acting as a cache. The client will support specifying thettl
, and a method for ignoring thettl
and deleting the data as soon as possible.Protocol changes
The
sync/stream
request format is extended to support specifying which streams to sync.The response format includes info for associating buckets with sync streams, to allow tracking progress for individual streams.
The bucket data and checkpoint system remains unchanged: Each sync stream just maps to one or more buckets, which are synced the same as before. This means current clients can sync streams with
auto_subscribe
without any changes.The specific protocol additions are described in #307.
Beta Was this translation helpful? Give feedback.
All reactions