Skip to content

Conversation

rkistner
Copy link
Contributor

@rkistner rkistner commented Sep 4, 2025

#276 included some significant optimizations to how write checkpoint lookups are performed. In general, the strategy is:

  1. We watch the checkpoint_events capped collection for new checkpoints.
  2. Each time we get a new checkpoint, we lookup changed buckets since the last checkpoint, changed parameter lookups, and changed write checkpoints.
  3. For each user's stream, we use that as a filter to check whether or not we need to query for further data for that user.

When a new stream is opened, we need to get the initial write checkpoint for the user, before we can use the above approach to efficiently get changes. The implementation in #276 mostly handled that.

However, there was one missed case: If the user does not have any write checkpoint, it would retry the lookup on every new checkpoint. In a case of thousands of concurrent connections and no write checkpoints, we can end up with doing tens of thousands of write checkpoint lookups per second. Even if the write checkpoint collection is empty, this still ends up adding multiple megabytes/s traffic between the instance and the storage database just for sending the query and receiving the empty results.

This fixes the issue by adding a boolean to keep track of whether or not we have done the initial query, instead of using a null check on the write checkpoint.

This change has no effect on users that do have a write checkpoint.

Copy link

changeset-bot bot commented Sep 4, 2025

🦋 Changeset detected

Latest commit: 3269f76

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@powersync/service-module-mongodb-storage Patch
@powersync/service-core Patch
@powersync/service-image Patch
@powersync/service-schema Patch
@powersync/service-module-mongodb Patch
@powersync/service-module-mysql Patch
@powersync/service-module-postgres Patch
@powersync/service-core-tests Patch
@powersync/service-module-core Patch
@powersync/service-module-postgres-storage Patch
test-client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@rkistner rkistner requested a review from simolus3 September 4, 2025 11:55
Copy link
Contributor

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with these changes 👍

@rkistner rkistner merged commit a2b8bb0 into main Sep 4, 2025
21 checks passed
@rkistner rkistner deleted the optimize-write-checkpoint-lookups branch September 4, 2025 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants