简体   繁体   中英

Proper way to handle race condition for 3 microservices. (Promise.all ?)

How sure I am that promise.all will run the promise request one by one?

Scenario I have 3 services. frontend , uploader , and api frontend is the client, uploader handles google storage related actions, and api handles normal REST stuff.

On my database I have something like:

{
  id: 1,
  directory: 'googlestorage/dir/randomstring.jpg',
  user_id: 100
}

Now in my frontend, I have a function that triggers an update to the directory and deletes the old one so the end result should be.

{
  id: 1,
  directory: 'googlestorage/new_directory/anotherRandomString.jpg',
  user_id: 100
}

In my frontend I need to make sure that the call to the api to update the record on the database should go first before deleting it by calling the fileuploader .

but on my fileuploader I pass the directory to check and validate whether the user is allowed to delete the file or not.

But If I update the database first, then I won't be able to check if the record is still on user's because the data is already updated and will not match the old one.

I also don't want to delete the file first because what if the request to update the database fails?

I tried doing:

await Promise.all([requestUpdateToApi, requestToDeleteFileUploader]);

But it's the same, one will be updated first or will be deleted first before the other.

I wanna do it so if one request fails, the other should not proceed too - is there a good solution for this without touching the database structure?

Actually I can just edit the directory so each files are stored in randomDirectoryName/{userId}/{filename} .

So if I delete the file, I just use the id and the user_id to validate the data then delete the path under the /{userId}/{directoryFromRequest} without worrying about race condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM