cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

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

toyko
New member | Level 2
Go to solution

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?

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

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

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.

View solution in original post

2 Replies 2

Greg-DB
Dropbox Staff
Go to solution

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

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
New member | Level 2
Go to solution

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.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    toyko New member | Level 2
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?