4

I have an app that allows users to sign in with google account. A user may be logged in with same account on multiple android devices at the same time. The app on these devices maintain a local database, which needs to be periodically synced. These devices may add or remove or update the entries at the same time.

  1. Data will be stored on User's Google Drive.
  2. Data that needs to be synced is simple (much like key-value pairs with no relations)
  3. In case of update conflicts, the most recent update prevails.
  4. Incremental updates are preferable, although not compulsory.
  5. Assumption - Each entry takes about 150 Bytes, max number of entries per device is 1000, and max no. of devices logged in per user is 3. Thus, maximum amount of data synced is ~500 KB.

I don't want to host an actual server. Since the app is free and the number of users are very large, I cannot go for paid solutions (like Firebase, etc). Further, I am using SyncAdapter for synchronisation (if it helps).

My questions are:

  1. What would be the logic/algorithm to implement it? (which deals with conflicts as well)
  2. Is there any better way other than storing files in Google Drive for sync purposes?

What I have thought so far:

  • I will save a JSON file in user's drive storage, representing entire database.
  • On local database, when entries are deleted by user, instead of deleting the entries, I set the value of deleted_at column as current time stamp.
  • On local database, when entries are added/updated/deleted, I update the updated_at column as current timestamp.
  • Sync will be done as follows:
    1. Fetch the file from Drive.
    2. For each records: If updated_at value from Drive file is more recent, then update record in local db. If updated_at value of local db is more recent, update record in JSON file.
    3. Add all the records that exist in File, but not in local db and viceversa.
    4. Upload and Overwrite the JSON file to Drive.

Please suggest better ways, and what problems will I run into with this approach.

1
  • Good approach. This type of app data synchronization logic is needed in many app scenarios. However, there is always going to be some conflicts while multiple app clients are uploading changes to Drive. Commented May 25, 2023 at 19:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.