I'm currently working on an enterprise portal built completely using provider hosted apps, ECMA CSOM and .NET client object model. We are almost at the end of our development phase and we started seeing many weird issues when more and more people started using/testing the portal.
When we investigated all the issues we end up finding that these issues were caused because of multiple calls trying to access a particular item/document/list simultaneously. I'll provide some classic scenarios that happened in our application.
- We have a provider hosted app which user uses to update a document in a document library. When two users simultaneously tried to update a same document one of the calls failed because there will be a save .
- There is a list which contains population of all countries. Once we get population data of more countries we have to insert that in the list. While inserting the population value, we will check if the country item is already there. If yes, update the population value. Else, create a new item for that country.
But when two calls simultaneously try to add a country value which is not in the list it ends up creating duplicates since the both the calls check the list at the same time and assume there is no item for this country.
How can we handle concurrency in above cases? Any insights provided would be of great help.