You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

toyko's avatar
toyko
New member | Level 2
6 years ago

using dropbox api to read and write json in production - race conditions?

I'm wondering if there will be "race conditions" if I use the dropbox api to read and write json.  There are no individual users - authorization is via access token.  When a user comes to the site, they'll type in their name and take a quiz.  When they click submit at the end of the quiz, the user's name and score will be saved to a master json file.  My plan was to do this by reading the file, appending the user info, then overwriting the entire file.

Would it be possible for there to be data loss doing it this way?  Say User A and User B both read the file at pretty much the same time.  User A writes then User B writes and so User A's info is lost.  How do I prevent this from happening?

  • [Cross-linking for reference: https://stackoverflow.com/questions/58229661/can-you-use-dropbox-api-to-read-write-json-in-production ]

    Yes, with the setup you describe, you could certainly experience a race condition where some data is lost, if there are two writes around the same time, and you have the app overwrite the file, as the second write wouldn't contain the new data from the first one. 

    For this reason, we generally recommend against using the "overwrite" write mode. 

    You can find information on the different write modes under the `mode` parameter for the /2/files/upload endpoint. (Be sure to click on `WriteMode` to expand the documentation for it.)

    Instead, in this case, it sounds like you would want to use the "update" write mode instead. This mode has the app supply the `rev` value that the app is working from, when uploading the new version of the file. If the `rev` is not the latest one, that would indicate that there was some race condition that may result in lost data, so the Dropbox API would consider the upload a conflict instead of overwriting the file. You can use the `autorename` parameter to determine what happens in the case of a conflict.

    In your app, when you see that a conflict occurred, you can then have some code that intelligently handles it. E.g., in your case, you may want to download the new latest version of the file, re-apply the new local changes to it, and then try to upload that new updated version of the file again.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    [Cross-linking for reference: https://stackoverflow.com/questions/58229661/can-you-use-dropbox-api-to-read-write-json-in-production ]

    Yes, with the setup you describe, you could certainly experience a race condition where some data is lost, if there are two writes around the same time, and you have the app overwrite the file, as the second write wouldn't contain the new data from the first one. 

    For this reason, we generally recommend against using the "overwrite" write mode. 

    You can find information on the different write modes under the `mode` parameter for the /2/files/upload endpoint. (Be sure to click on `WriteMode` to expand the documentation for it.)

    Instead, in this case, it sounds like you would want to use the "update" write mode instead. This mode has the app supply the `rev` value that the app is working from, when uploading the new version of the file. If the `rev` is not the latest one, that would indicate that there was some race condition that may result in lost data, so the Dropbox API would consider the upload a conflict instead of overwriting the file. You can use the `autorename` parameter to determine what happens in the case of a conflict.

    In your app, when you see that a conflict occurred, you can then have some code that intelligently handles it. E.g., in your case, you may want to download the new latest version of the file, re-apply the new local changes to it, and then try to upload that new updated version of the file again.

    • toyko's avatar
      toyko
      New member | Level 2

      This info was *exactly* what I was looking for.  I'm brand new to the Dropbox API and I didn't know about the "update" option.  Thanks.