We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.
Forum Discussion
toyko
6 years agoNew member | Level 2
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-DBDropbox 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.
- toykoNew 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.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,875 PostsLatest Activity: 2 months agoIf you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!