diff --git a/docs/2023-03-05-incident.html b/docs/2023-03-05-incident.html index c3790f8..c57c275 100644 --- a/docs/2023-03-05-incident.html +++ b/docs/2023-03-05-incident.html @@ -71,6 +71,46 @@ Solution: For some configs, make sure to failed first if it's empty.

+

+ Race condition of series of APIs +

+

For example I have 2 APIs:

+ +

+ API upload is slow, it takes 10s to finish. API submit is fast, only takes + 2s. +

+

+ The problem is submit use data from upload too. When user calls API + upload, image is stored in cache. When user calls API submit, it use + whatever image is stored in cache. +

+

It's when the fun begins.

+

+ Imagine user Trong already upload image. So he is ready to submit. But for + the same time, he re-call API upload to upload another image too. +

+

+ So if API upload is finised first, which is kinda impossible (u know + upload file is not fast right?), everything right. But for most cases, API + submit is finished first. It means Trong's data is submitted with the old + image. Then API upload is finished, it will replace the old image with the + new one.So the old one, aka image in submitted data, is gone. +

+

Chaos right there!

+

+ Solution: Use a lock, if user enter API upload, lock it + to prevent user call other APIs. Rememeber to unlock after finished +

Feel free to ask me via diff --git a/posts/2023-03-05-incident.md b/posts/2023-03-05-incident.md index 116a8ef..0e19cdb 100644 --- a/posts/2023-03-05-incident.md +++ b/posts/2023-03-05-incident.md @@ -9,3 +9,32 @@ Because all configs is read from file. But the port config is empty -> So when service inits, it use that empty port somehow. **Solution**: For some configs, make sure to failed first if it's empty. + +## Race condition of series of APIs + +For example I have 2 APIs: + +- API upload: allow user to upload image +- API submit: submit data to server + +API upload is slow, it takes 10s to finish. +API submit is fast, only takes 2s. + +The problem is submit use data from upload too. +When user calls API upload, image is stored in cache. +When user calls API submit, it use whatever image is stored in cache. + +It's when the fun begins. + +Imagine user Trong already upload image. +So he is ready to submit. +But for the same time, he re-call API upload to upload another image too. + +So if API upload is finised first, which is kinda impossible (u know upload file is not fast right?), everything right. +But for most cases, API submit is finished first. +It means Trong's data is submitted with the old image. +Then API upload is finished, it will replace the old image with the new one.So the old one, aka image in submitted data, is gone. + +Chaos right there! + +**Solution**: Use a lock, if user enter API upload, lock it to prevent user call other APIs. Rememeber to unlock after finished