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, t...
  • Greg-DB's avatar
    6 years ago

    [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.