Suppose that I have a document
{
maxCountAllowed:100,
currentCount:99
}
And I have an API that users call to increment the currentCount if it's not 100 yet ..
if(document.currentCount < document.maxCountAllowed){
document.currentCount ++;
document.save();
}else{
console.log("sorry maximum count reached");
}
Suppose that 2 or more users called the API at the same time .. both of them will find that the currentCount
is still 99 so the function will proceed and both of them will increment the currentCount
to be 101 or more .. how can I prevent something like this ?
Even if both of them will make it a 100, I still want only one of them to be able to update and the other one gets the error message.