Sync changes in batch once in a minute, an hour, a day

I’m developing an app where user can do numerous updates to the same data within a short period of time. On top of that, I would like to reduce syncing for not premium users to once per day. My motivation is to reduce the number of requests to the server.
Similar topics were raised already in this forum:
How can I avoid syncing on every commit?
How to optimize Realm Sync for performance
and the only solution suggested was to handle that manually and put data into synced realm once a while.
In my case that adds a lot of complexity to my code. And it seems it should be easily avoided if I would pause the sync and resume it once in a some period, then pause again.

I have done my experiments already and here is what I got (I’m using React Native).
Option 1. This is just for upload, but I suppose it can be coupled with download callback. It has a drawback though – resume does not happen instantly – it waits for the next change in realm:

    realm?.syncSession?.resume();
    realm?.syncSession?.addProgressNotification(
      'upload', 
      'forCurrentlyOutstandingWork',
      (transferred, transferable) => {
        if (transferred >= transferable) {
          realm?.syncSession?.pause();
        }
      }
    );

Option 2. This also has a drawback – requests to the server are sent even when there were no local changes:

    realm?.syncSession?.resume();
    realm?.syncSession?.uploadAllLocalChanges().then(() => {
      console.log('pausing transferring (fulfilled)');
      realm?.syncSession?.pause();
    }, (reason) => {
      console.log(`pausing transferring (rejected: ${reason})`);
      realm?.syncSession?.pause();
    });

Please advice and share your ideas. Do you know of any plans to support this “delayed” sync out of the box?

1 Like

The realm guys can correct me if I am wrong, but from what I understand, pausing/resuming won’t affect the number of requests at all. Each commit, will still count, even if the sync is paused, so I don’t think this will be a valid strategy for cutting sync costs.

What I will have to do for my app is to use a “draft object”, i.e. a local copy of the object to edit. This will be stored in a local realm and I can do thousands of commits without triggering requests to the server. When done editing, I will copy the draft object over to the synced realm which should result in a single commit.

Totally agree that this adds a lot of complexity, but it is the only solution I have found so far.

1 Like

I have done another experiment (compared usage data) and unfortunately it seems you are correct about “pausing won’t affect the number of tracked requests”. However, pausing/resuming should (I couldn’t check that easily) affect Sync Runtime time, which is also billable.

I would appreciate any additional thoughts and opinions regarding this topic.

1 Like

Would the sync time contribute significantly to your costs? I haven’t gone live with sync yet, but when I did the math, it didn’t look like this was something worth optimizing for.

In my app I have roughly 15k DAU and an average of 10 min of app usage per user per day. In this case this would mean that the monthly usage would be roughly 15 000 * 10 * 30 = 4 500 000 minutes. At a cost of $0.08/1 000 000min, this would only be $0.36/month… i.e. not worth pausing sync for in my case.

For me it will definitely be the Realm requests I need to be careful of, not sync time and probably not data transfer either.

1 Like